Class CustomCollectionEditor
- All Implemented Interfaces:
- PropertyEditor
By default registered for Set, SortedSet and List, to automatically convert any given Collection to one of those target types if the type does not match the target property.
- Since:
- 1.1.3
- Author:
- Juergen Hoeller
- See Also:
- 
Constructor SummaryConstructorsConstructorDescriptionCustomCollectionEditor(Class<? extends Collection> collectionType) Create a new CustomCollectionEditor for the given target type, keeping an incomingnullas-is.CustomCollectionEditor(Class<? extends Collection> collectionType, boolean nullAsEmptyCollection) Create a new CustomCollectionEditor for the given target type.
- 
Method SummaryModifier and TypeMethodDescriptionprotected booleanReturn whether to always create a new Collection, even if the type of the passed-in Collection already matches.protected ObjectconvertElement(Object element) Hook to convert each encountered Collection/array element.protected Collection<Object>createCollection(Class<? extends Collection> collectionType, int initialCapacity) Create a Collection of the given type, with the given initial capacity (if supported by the Collection type).This implementation returnsnullto indicate that there is no appropriate text representation.voidConvert the given text value to a Collection with a single element.voidConvert the given value to a Collection of the target type.Methods inherited from class java.beans.PropertyEditorSupportaddPropertyChangeListener, firePropertyChange, getCustomEditor, getJavaInitializationString, getSource, getTags, getValue, isPaintable, paintValue, removePropertyChangeListener, setSource, supportsCustomEditor
- 
Constructor Details- 
CustomCollectionEditorCreate a new CustomCollectionEditor for the given target type, keeping an incomingnullas-is.- Parameters:
- collectionType- the target type, which needs to be a sub-interface of Collection or a concrete Collection class
- See Also:
 
- 
CustomCollectionEditorpublic CustomCollectionEditor(Class<? extends Collection> collectionType, boolean nullAsEmptyCollection) Create a new CustomCollectionEditor for the given target type.If the incoming value is of the given type, it will be used as-is. If it is a different Collection type or an array, it will be converted to a default implementation of the given Collection type. If the value is anything else, a target Collection with that single value will be created. The default Collection implementations are: ArrayList for List, TreeSet for SortedSet, and LinkedHashSet for Set. - Parameters:
- collectionType- the target type, which needs to be a sub-interface of Collection or a concrete Collection class
- nullAsEmptyCollection- whether to convert an incoming- nullvalue to an empty Collection (of the appropriate type)
- See Also:
 
 
- 
- 
Method Details- 
setAsTextConvert the given text value to a Collection with a single element.- Specified by:
- setAsTextin interface- PropertyEditor
- Overrides:
- setAsTextin class- PropertyEditorSupport
- Throws:
- IllegalArgumentException
 
- 
setValueConvert the given value to a Collection of the target type.- Specified by:
- setValuein interface- PropertyEditor
- Overrides:
- setValuein class- PropertyEditorSupport
 
- 
createCollectionprotected Collection<Object> createCollection(Class<? extends Collection> collectionType, int initialCapacity) Create a Collection of the given type, with the given initial capacity (if supported by the Collection type).- Parameters:
- collectionType- a sub-interface of Collection
- initialCapacity- the initial capacity
- Returns:
- the new Collection instance
 
- 
alwaysCreateNewCollectionprotected boolean alwaysCreateNewCollection()Return whether to always create a new Collection, even if the type of the passed-in Collection already matches.Default is "false"; can be overridden to enforce creation of a new Collection, for example to convert elements in any case. - See Also:
 
- 
convertElementHook to convert each encountered Collection/array element. The default implementation simply returns the passed-in element as-is.Can be overridden to perform conversion of certain elements, for example String to Integer if a String array comes in and should be converted to a Set of Integer objects. Only called if actually creating a new Collection! This is by default not the case if the type of the passed-in Collection already matches. Override alwaysCreateNewCollection()to enforce creating a new Collection in every case.- Parameters:
- element- the source element
- Returns:
- the element to be used in the target Collection
- See Also:
 
- 
getAsTextThis implementation returnsnullto indicate that there is no appropriate text representation.- Specified by:
- getAsTextin interface- PropertyEditor
- Overrides:
- getAsTextin class- PropertyEditorSupport
 
 
-