|
|
|
[
Permlink
| « Hide
]
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.
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 ...
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); } } } Hello Jukka, I've added a thread-safe version for this to the head revision. You may try this out yourself. Thanks a lot!
|
|||||||||||||||||||||||||||||||||||||