History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: XSTR-386
Type: New Feature New Feature
Status: Closed Closed
Resolution: Fixed
Assignee: Joerg Schaible
Reporter: Jukka Lindström
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
XStream

Add SingleValueConverter that uses PropertyEditor into the utility classes of xstream

Created: 07/Mar/07 10:27 AM   Updated: 25/Feb/08 05:01 PM
Component/s: None
Affects Version/s: None
Fix Version/s: 1.3


 Description  « Hide
A huge number of PropertyEditors has been created and you could help the usage of these through adding a SingleValueConverter that utilizes a PropertyEditor. A Simple version that we've used is like this:

import java.beans.PropertyEditor;
import com.thoughtworks.xstream.converters.SingleValueConverter;

public class PropertyEditorValueConverter implements SingleValueConverter {
    final private PropertyEditor editor;
    final private Class type;

    public PropertyEditorValueConverter(PropertyEditor editor, Class type) {
        this.editor = editor;
        this.type = type;
    }

    public Object fromString(String str) {
        editor.setAsText(str);
        return editor.getValue();
    }

    public String toString(Object obj) {
        editor.setValue(obj);
        return editor.getAsText();
    }

    public boolean canConvert(Class type) {
        return this.type.isAssignableFrom(type);
    }
}


Here's a test case:
import java.awt.Color;
import junit.framework.TestCase;
import sun.beans.editors.ColorEditor;

public class PropertyEditorValueConverterTest extends TestCase {
    PropertyEditorValueConverter converter = new PropertyEditorValueConverter(new ColorEditor(), Color.class);

    public void testMatchesClass() throws Exception {
        assertTrue(converter.canConvert(Color.class));
        assertFalse(converter.canConvert(Object.class));
    }

    public void testConverting() throws Exception {
        assertConvertWorks(Color.black);
        assertConvertWorks(Color.blue);
        assertConvertWorks(Color.yellow);
    }

    private void assertConvertWorks(Color testObject) {
        String string = converter.toString(testObject);
        assertEquals(testObject, converter.fromString(string));
    }
}


 All   Comments   Change History      Sort Order: Ascending order - Click to sort in descending order
Jukka Lindström - 07/Mar/07 10:33 AM
Just after commiting this I remembered that property editors are not thread safe.. I'll commit a thread safe version tomorrow (where the constructor argument is class for the property editor and the fromString / toString methods instantiate the class.


Joerg Schaible - 07/Mar/07 12:31 PM
Looks interesting. Might be enough if you sync on the editor instance in the two serialization methods. Depends on the use case. An alternative approach would be a pool of those editors like it is done in the ThreadSafeSimpleDateFormat class in c.t.x.core.util ...

Jukka Lindström - 08/Mar/07 05:45 AM
Yes, synchronizing should be enough. here's a version where editor is instantiated for each case. All editors have empty constructors (as per class conctract) so should work nicely.

public class PropertyEditorValueConverter implements SingleValueConverter {
    final private Class<? extends PropertyEditor> editorType;
    final private Class convertedType;

    public PropertyEditorValueConverter(Class<? extends PropertyEditor> editorType, Class type) {
        this.editorType = editorType;
        this.convertedType = type;
    }

    public Object fromString(String str) {
        PropertyEditor editor2 = createEditor();
        editor2.setAsText(str);
        return editor2.getValue();
    }

    public String toString(Object obj) {
        PropertyEditor editor2 = createEditor();
        editor2.setValue(obj);
        return editor2.getAsText();
    }

    public boolean canConvert(Class type) {
        return this.convertedType.isAssignableFrom(type);
    }

    private PropertyEditor createEditor() {
        try {
            return editorType.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Joerg Schaible - 19/Sep/07 07:20 PM
Hello Jukka, I've added a thread-safe version for this to the head revision. You may try this out yourself. Thanks a lot!

Joerg Schaible - 25/Feb/08 05:01 PM
Closing issues before next release.