01: /*
02: * Copyright (C) 2007, 2008 XStream Committers.
03: * All rights reserved.
04: *
05: * The software in this package is published under the terms of the BSD
06: * style license a copy of which has been included with this distribution in
07: * the LICENSE.txt file.
08: *
09: * Created on 20.09.2007 by Joerg Schaible
10: */
11: package com.thoughtworks.xstream.converters.extended;
12:
13: import com.thoughtworks.xstream.converters.SingleValueConverter;
14: import com.thoughtworks.xstream.core.util.ThreadSafePropertyEditor;
15:
16: import java.beans.PropertyEditor;
17:
18: /**
19: * A SingleValueConverter that can utilize a {@link PropertyEditor} implementation used for a
20: * specific type. The converter ensures that the editors can be used concurrently.
21: *
22: * @author Jukka Lindström
23: * @author Jörg Schaible
24: * @since 1.3
25: */
26: public class PropertyEditorCapableConverter implements
27: SingleValueConverter {
28:
29: private final ThreadSafePropertyEditor editor;
30: private final Class type;
31:
32: public PropertyEditorCapableConverter(
33: final Class propertyEditorType, final Class type) {
34: this .type = type;
35: editor = new ThreadSafePropertyEditor(propertyEditorType, 2, 5);
36: }
37:
38: public boolean canConvert(final Class type) {
39: return this .type == type;
40: }
41:
42: public Object fromString(final String str) {
43: return editor.setAsText(str);
44: }
45:
46: public String toString(final Object obj) {
47: return editor.getAsText(obj);
48: }
49:
50: }
|