Top Description Fields Constructors Methods
org.springframework.beans.propertyeditors

public Class CustomCollectionEditor

extends PropertyEditorSupport
Class Inheritance
Imports
java.beans.PropertyEditorSupport, java.lang.reflect.Array, java.util.ArrayList, .Collection, .LinkedHashSet, .List, .SortedSet, .TreeSet, org.springframework.lang.Nullable, org.springframework.util.Assert, .ReflectionUtils

Property editor for Collections, converting any source Collection to a given target Collection type.

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.

Author
Juergen Hoeller
Since
1.1.3
See Also
java.util.Collection, java.util.Set, java.util.SortedSet, java.util.List

Field Summary

Modifier and TypeField and Description
private final Class<? extends Collection<E>>
private final boolean

Constructor Summary

AccessConstructor and Description
public
CustomCollectionEditor(Class<? extends Collection<E>>
the target type, which needs to be a sub-interface of Collection or a concrete Collection class
collectionType
)

Create a new CustomCollectionEditor for the given target type, keeping an incoming null as-is.

public
CustomCollectionEditor(Class<? extends Collection<E>>
the target type, which needs to be a sub-interface of Collection or a concrete Collection class
collectionType
,
boolean
whether to convert an incoming null value to an empty Collection (of the appropriate type)
nullAsEmptyCollection
)

Create a new CustomCollectionEditor for the given target type.

Method Summary

Modifier and TypeMethod and Description
protected boolean
alwaysCreateNewCollection()

Return whether to always create a new Collection, even if the type of the passed-in Collection already matches.

protected Object

Returns:

the element to be used in the target Collection
convertElement
(Object
the source element
element
)

Hook to convert each encountered Collection/array element.

protected Collection<Object>

Returns:

the new Collection instance
createCollection
(Class<? extends Collection<E>>
a sub-interface of Collection
collectionType
,
int
the initial capacity
initialCapacity
)

Create a Collection of the given type, with the given initial capacity (if supported by the Collection type).

public String
getAsText()

Overrides java.beans.PropertyEditorSupport.getAsText.

Implements java.beans.PropertyEditor.getAsText.

This implementation returns null to indicate that there is no appropriate text representation.
public void
setAsText(String
The string to be parsed.
text
)

Overrides java.beans.PropertyEditorSupport.setAsText.

Implements java.beans.PropertyEditor.setAsText.

Convert the given text value to a Collection with a single element.
public void
setValue(Object
The new target object to be edited. Note that this object should not be modified by the PropertyEditor, rather the PropertyEditor should create a new object to hold any modified value.
value
)

Overrides java.beans.PropertyEditorSupport.setValue.

Implements java.beans.PropertyEditor.setValue.

Convert the given value to a Collection of the target type.
Inherited from java.beans.PropertyEditorSupport:
addPropertyChangeListenerfirePropertyChangegetCustomEditorgetJavaInitializationStringgetSourcegetTagsgetValueisPaintablepaintValueremovePropertyChangeListenersetSourcesupportsCustomEditor

Field Detail

collectionTypeback to summary
private final Class<? extends Collection<E>> collectionType
Annotations
@SuppressWarnings:rawtypes
nullAsEmptyCollectionback to summary
private final boolean nullAsEmptyCollection

Constructor Detail

CustomCollectionEditorback to summary
public CustomCollectionEditor(Class<? extends Collection<E>> collectionType)

Create a new CustomCollectionEditor for the given target type, keeping an incoming null as-is.

Parameters
collectionType:Class<? extends Collection<E>>

the target type, which needs to be a sub-interface of Collection or a concrete Collection class

Annotations
@SuppressWarnings:rawtypes
See Also
java.util.Collection, java.util.ArrayList, java.util.TreeSet, java.util.LinkedHashSet
CustomCollectionEditorback to summary
public CustomCollectionEditor(Class<? extends Collection<E>> 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:Class<? extends Collection<E>>

the target type, which needs to be a sub-interface of Collection or a concrete Collection class

nullAsEmptyCollection:boolean

whether to convert an incoming null value to an empty Collection (of the appropriate type)

Annotations
@SuppressWarnings:rawtypes
See Also
java.util.Collection, java.util.ArrayList, java.util.TreeSet, java.util.LinkedHashSet

Method Detail

alwaysCreateNewCollectionback to summary
protected 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
convertElement
convertElementback to summary
protected Object convertElement(Object element)

Hook 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:Object

the source element

Returns:Object

the element to be used in the target Collection

See Also
alwaysCreateNewCollection()
createCollectionback to summary
protected Collection<Object> createCollection(Class<? extends Collection<E>> collectionType, int initialCapacity)

Create a Collection of the given type, with the given initial capacity (if supported by the Collection type).

Parameters
collectionType:Class<? extends Collection<E>>

a sub-interface of Collection

initialCapacity:int

the initial capacity

Returns:Collection<Object>

the new Collection instance

Annotations
@SuppressWarnings:rawtypes, unchecked
getAsTextback to summary
public String getAsText()

Overrides java.beans.PropertyEditorSupport.getAsText.

Implements java.beans.PropertyEditor.getAsText.

This implementation returns null to indicate that there is no appropriate text representation.

Returns:String

Doc from java.beans.PropertyEditorSupport.getAsText.

The property value as a string suitable for presentation to a human to edit.

Returns null if the value can't be expressed as a string.

If a non-null value is returned, then the PropertyEditor should be prepared to parse that string back in setAsText().

Annotations
@Override
@Nullable
setAsTextback to summary
public void setAsText(String text) throws IllegalArgumentException

Overrides java.beans.PropertyEditorSupport.setAsText.

Implements java.beans.PropertyEditor.setAsText.

Convert the given text value to a Collection with a single element.

Parameters
text:String

Doc from java.beans.PropertyEditorSupport.setAsText.

The string to be parsed.

Annotations
@Override
setValueback to summary
public void setValue(Object value)

Overrides java.beans.PropertyEditorSupport.setValue.

Implements java.beans.PropertyEditor.setValue.

Convert the given value to a Collection of the target type.

Parameters
value:Object

Doc from java.beans.PropertyEditorSupport.setValue.

The new target object to be edited. Note that this object should not be modified by the PropertyEditor, rather the PropertyEditor should create a new object to hold any modified value.

Annotations
@Override