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

public Class CustomMapEditor

extends PropertyEditorSupport
Class Inheritance
Imports
java.beans.PropertyEditorSupport, java.util.LinkedHashMap, .Map, .SortedMap, .TreeMap, org.springframework.lang.Nullable, org.springframework.util.Assert, .ReflectionUtils

Property editor for Maps, converting any source Map to a given target Map type.
Author
Juergen Hoeller
Since
2.0.1
See Also
java.util.Map, java.util.SortedMap

Field Summary

Modifier and TypeField and Description
private final Class<? extends Map<K, V>>
private final boolean

Constructor Summary

AccessConstructor and Description
public
CustomMapEditor(Class<? extends Map<K, V>>
the target type, which needs to be a sub-interface of Map or a concrete Map class
mapType
)

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

public
CustomMapEditor(Class<? extends Map<K, V>>
the target type, which needs to be a sub-interface of Map or a concrete Map class
mapType
,
boolean
ap whether to convert an incoming null value to an empty Map (of the appropriate type)
nullAsEmptyMap
)

Create a new CustomMapEditor for the given target type.

Method Summary

Modifier and TypeMethod and Description
protected boolean
alwaysCreateNewMap()

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

protected Object

Returns:

the key to be used in the target Map
convertKey
(Object
the source key
key
)

Hook to convert each encountered Map key.

protected Object

Returns:

the value to be used in the target Map
convertValue
(Object
the source value
value
)

Hook to convert each encountered Map value.

protected Map<Object, Object>

Returns:

the new Map instance
createMap
(Class<? extends Map<K, V>>
a sub-interface of Map
mapType
,
int
the initial capacity
initialCapacity
)

Create a Map of the given type, with the given initial capacity (if supported by the Map 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 Map 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 Map of the target type.

Inherited from java.beans.PropertyEditorSupport:
addPropertyChangeListenerfirePropertyChangegetCustomEditorgetJavaInitializationStringgetSourcegetTagsgetValueisPaintablepaintValueremovePropertyChangeListenersetSourcesupportsCustomEditor

Field Detail

mapTypeback to summary
private final Class<? extends Map<K, V>> mapType
Annotations
@SuppressWarnings:rawtypes
nullAsEmptyMapback to summary
private final boolean nullAsEmptyMap

Constructor Detail

CustomMapEditorback to summary
public CustomMapEditor(Class<? extends Map<K, V>> mapType)

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

Parameters
mapType:Class<? extends Map<K, V>>

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

Annotations
@SuppressWarnings:rawtypes
See Also
java.util.Map, java.util.HashMap, java.util.TreeMap, java.util.LinkedHashMap
CustomMapEditorback to summary
public CustomMapEditor(Class<? extends Map<K, V>> mapType, boolean nullAsEmptyMap)

Create a new CustomMapEditor 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 Map type or an array, it will be converted to a default implementation of the given Map type. If the value is anything else, a target Map with that single value will be created.

The default Map implementations are: TreeMap for SortedMap, and LinkedHashMap for Map.

Parameters
mapType:Class<? extends Map<K, V>>

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

nullAsEmptyMap:boolean

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

Annotations
@SuppressWarnings:rawtypes
See Also
java.util.Map, java.util.TreeMap, java.util.LinkedHashMap

Method Detail

alwaysCreateNewMapback to summary
protected boolean alwaysCreateNewMap()

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

Default is "false"; can be overridden to enforce creation of a new Map, for example to convert elements in any case.

See Also
convertKey, convertValue
convertKeyback to summary
protected Object convertKey(Object key)

Hook to convert each encountered Map key. The default implementation simply returns the passed-in key as-is.

Can be overridden to perform conversion of certain keys, for example from String to Integer.

Only called if actually creating a new Map! This is by default not the case if the type of the passed-in Map already matches. Override alwaysCreateNewMap() to enforce creating a new Map in every case.

Parameters
key:Object

the source key

Returns:Object

the key to be used in the target Map

See Also
alwaysCreateNewMap
convertValueback to summary
protected Object convertValue(Object value)

Hook to convert each encountered Map value. The default implementation simply returns the passed-in value as-is.

Can be overridden to perform conversion of certain values, for example from String to Integer.

Only called if actually creating a new Map! This is by default not the case if the type of the passed-in Map already matches. Override alwaysCreateNewMap() to enforce creating a new Map in every case.

Parameters
value:Object

the source value

Returns:Object

the value to be used in the target Map

See Also
alwaysCreateNewMap
createMapback to summary
protected Map<Object, Object> createMap(Class<? extends Map<K, V>> mapType, int initialCapacity)

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

Parameters
mapType:Class<? extends Map<K, V>>

a sub-interface of Map

initialCapacity:int

the initial capacity

Returns:Map<Object, Object>

the new Map 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 Map 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 Map 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