Class MapAccessor<K,V>
java.lang.Object
org.springframework.binding.collection.MapAccessor<K,V>
- All Implemented Interfaces:
MapAdaptable<K,
V>
A simple, generic decorator for getting attributes out of a map. May be instantiated directly or used as a base class
as a convenience.
- Author:
- Keith Donald
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionasMap()
Returns this object's contents as aMap
.void
assertContainsKey
(Object key) Asserts that the attribute is present in the attribute map.<T> T
assertKeyValueInstanceOf
(Object key, Object value, Class<T> requiredType) Assert that the key value, if non null, is an instance of the required type.<T> T
assertKeyValueOfType
(Object key, Class<T> requiredType) Assert that value of the map key, if non-null, is of the required type.boolean
containsKey
(Object key, Class<?> requiredType) Indicates if the attribute is present in the attribute map and of the required type.Returns a value in the map, returning null if the attribute is not present.<T extends V>
TReturns a value in the map, asserting it is of the required type if present and returningnull
if not found.<T extends V>
TReturns a value in the map of the specified type, returning the defaultValue if no value is found.Returns a value in the map, returning the defaultValue if no value was found.<T extends V>
T[]Returns a array value in the map, asserting it is of the required type if present and returningnull
if not found.getBoolean
(Object key) Returns a boolean value in the map, returningnull
if no value was found.getBoolean
(Object key, Boolean defaultValue) Returns a boolean value in the map, returning the defaultValue if no value was found.getCollection
(Object key) Returns a collection value in the map, returningnull
if no value was found.<T extends Collection<V>>
TgetCollection
(Object key, Class<T> requiredType) Returns a collection value in the map, asserting it is of the required type if present and returningnull
if not found.getInteger
(Object key) Returns an integer value in the map, returningnull
if no value was found.getInteger
(Object key, Integer defaultValue) Returns an integer value in the map, returning the defaultValue if no value was found.Returns a long value in the map, returningnull
if no value was found.Returns a long value in the map, returning the defaultValue if no value was found.<T extends Number>
TReturns a number value in the map that is of the specified type, returningnull
if no value was found.<T extends Number>
TReturns a number attribute value in the map of the specified type, returning the defaultValue if no value was found.getRequired
(Object key) Returns a value in the map, throwing an exception if the attribute is not present and of the correct type.<T extends V>
TgetRequired
(Object key, Class<T> requiredType) Returns an value in the map, asserting it is present and of the required type.<T extends V>
T[]getRequiredArray
(Object key, Class<? extends T[]> requiredType) Returns an array value in the map, asserting it is of the required type if present and throwing an exception if not found.getRequiredBoolean
(Object key) Returns a boolean value in the map, throwing an exception if the value is not present and of the correct type.Returns a collection value in the map, throwing an exception if not found.<T extends Collection<V>>
TgetRequiredCollection
(Object key, Class<T> requiredType) Returns a collection value in the map, asserting it is of the required type if present and throwing an exception if not found.getRequiredInteger
(Object key) Returns an integer value in the map, throwing an exception if the value is not present and of the correct type.getRequiredLong
(Object key) Returns a long value in the map, throwing an exception if the value is not present and of the correct type.<T extends Number>
TgetRequiredNumber
(Object key, Class<T> requiredType) Returns a number value in the map, throwing an exception if the attribute is not present and of the correct type.getRequiredString
(Object key) Returns a string value in the map, throwing an exception if the attribute is not present and of the correct type.Returns a string value in the map, returningnull
if no value was found.Returns a string value in the map, returning the defaultValue if no value was found.
-
Constructor Details
-
MapAccessor
Creates a new attribute map accessor.- Parameters:
map
- the map
-
-
Method Details
-
asMap
Description copied from interface:MapAdaptable
Returns this object's contents as aMap
. The returned map may or may not be modifiable depending on this implementation.Warning: this operation may be called frequently; if so care should be taken so that the map contents (if calculated) be cached as appropriate.
- Specified by:
asMap
in interfaceMapAdaptable<K,
V> - Returns:
- the object's contents as a map
-
get
Returns a value in the map, returning null if the attribute is not present.- Parameters:
key
- the key- Returns:
- the value
-
get
Returns a value in the map, returning the defaultValue if no value was found.- Parameters:
key
- the keydefaultValue
- the default- Returns:
- the attribute value
-
get
Returns a value in the map, asserting it is of the required type if present and returningnull
if not found.- Parameters:
key
- the keyrequiredType
- the required type- Returns:
- the value
- Throws:
IllegalArgumentException
- if the key is present but the value is not of the required type
-
get
Returns a value in the map of the specified type, returning the defaultValue if no value is found.- Parameters:
key
- the keyrequiredType
- the required typedefaultValue
- the default- Returns:
- the attribute value
- Throws:
IllegalArgumentException
- if the key is present but the value is not of the required type
-
getRequired
Returns a value in the map, throwing an exception if the attribute is not present and of the correct type.- Parameters:
key
- the key- Returns:
- the value
- Throws:
IllegalArgumentException
-
getRequired
public <T extends V> T getRequired(Object key, Class<T> requiredType) throws IllegalArgumentException Returns an value in the map, asserting it is present and of the required type.- Parameters:
key
- the keyrequiredType
- the required type- Returns:
- the value
- Throws:
IllegalArgumentException
-
getString
Returns a string value in the map, returningnull
if no value was found.- Parameters:
key
- the key- Returns:
- the string value
- Throws:
IllegalArgumentException
- if the key is present but the value is not a string
-
getString
Returns a string value in the map, returning the defaultValue if no value was found.- Parameters:
key
- the keydefaultValue
- the default- Returns:
- the string value
- Throws:
IllegalArgumentException
- if the key is present but the value is not a string
-
getRequiredString
Returns a string value in the map, throwing an exception if the attribute is not present and of the correct type.- Parameters:
key
- the key- Returns:
- the string value
- Throws:
IllegalArgumentException
- if the key is not present or present but the value is not a string
-
getCollection
Returns a collection value in the map, returningnull
if no value was found.- Parameters:
key
- the key- Returns:
- the collection value
- Throws:
IllegalArgumentException
- if the key is present but the value is not a collection
-
getCollection
public <T extends Collection<V>> T getCollection(Object key, Class<T> requiredType) throws IllegalArgumentException Returns a collection value in the map, asserting it is of the required type if present and returningnull
if not found.- Parameters:
key
- the key- Returns:
- the collection value
- Throws:
IllegalArgumentException
- if the key is present but the value is not a collection
-
getRequiredCollection
Returns a collection value in the map, throwing an exception if not found.- Parameters:
key
- the key- Returns:
- the collection value
- Throws:
IllegalArgumentException
- if the key is not present or present but the value is not a collection
-
getRequiredCollection
public <T extends Collection<V>> T getRequiredCollection(Object key, Class<T> requiredType) throws IllegalArgumentException Returns a collection value in the map, asserting it is of the required type if present and throwing an exception if not found.- Parameters:
key
- the key- Returns:
- the collection value
- Throws:
IllegalArgumentException
- if the key is not present or present but the value is not a collection of the required type
-
getArray
public <T extends V> T[] getArray(Object key, Class<? extends T[]> requiredType) throws IllegalArgumentException Returns a array value in the map, asserting it is of the required type if present and returningnull
if not found.- Parameters:
key
- the key- Returns:
- the array value
- Throws:
IllegalArgumentException
- if the key is present but the value is not an array of the required type
-
getRequiredArray
public <T extends V> T[] getRequiredArray(Object key, Class<? extends T[]> requiredType) throws IllegalArgumentException Returns an array value in the map, asserting it is of the required type if present and throwing an exception if not found.- Parameters:
key
- the key- Returns:
- the array value
- Throws:
IllegalArgumentException
- if the key is not present or present but the value is not a array of the required type
-
getNumber
public <T extends Number> T getNumber(Object key, Class<T> requiredType) throws IllegalArgumentException Returns a number value in the map that is of the specified type, returningnull
if no value was found.- Parameters:
key
- the keyrequiredType
- the required number type- Returns:
- the number value
- Throws:
IllegalArgumentException
- if the key is present but the value is not a number of the required type
-
getNumber
public <T extends Number> T getNumber(Object key, Class<T> requiredType, T defaultValue) throws IllegalArgumentException Returns a number attribute value in the map of the specified type, returning the defaultValue if no value was found.- Parameters:
key
- the attribute namedefaultValue
- the default- Returns:
- the number value
- Throws:
IllegalArgumentException
- if the key is present but the value is not a number of the required type
-
getRequiredNumber
public <T extends Number> T getRequiredNumber(Object key, Class<T> requiredType) throws IllegalArgumentException Returns a number value in the map, throwing an exception if the attribute is not present and of the correct type.- Parameters:
key
- the key- Returns:
- the number value
- Throws:
IllegalArgumentException
- if the key is not present or present but the value is not a number of the required type
-
getInteger
Returns an integer value in the map, returningnull
if no value was found.- Parameters:
key
- the key- Returns:
- the integer value
- Throws:
IllegalArgumentException
- if the key is present but the value is not an integer
-
getInteger
Returns an integer value in the map, returning the defaultValue if no value was found.- Parameters:
key
- the keydefaultValue
- the default- Returns:
- the integer value
- Throws:
IllegalArgumentException
- if the key is present but the value is not an integer
-
getRequiredInteger
Returns an integer value in the map, throwing an exception if the value is not present and of the correct type.- Parameters:
key
- the attribute name- Returns:
- the integer attribute value
- Throws:
IllegalArgumentException
- if the key is not present or present but the value is not an integer
-
getLong
Returns a long value in the map, returningnull
if no value was found.- Parameters:
key
- the key- Returns:
- the long value
- Throws:
IllegalArgumentException
- if the key is present but not a long
-
getLong
Returns a long value in the map, returning the defaultValue if no value was found.- Parameters:
key
- the keydefaultValue
- the default- Returns:
- the long attribute value
- Throws:
IllegalArgumentException
- if the key is present but the value is not a long
-
getRequiredLong
Returns a long value in the map, throwing an exception if the value is not present and of the correct type.- Parameters:
key
- the key- Returns:
- the long attribute value
- Throws:
IllegalArgumentException
- if the key is not present or present but the value is not a long
-
getBoolean
Returns a boolean value in the map, returningnull
if no value was found.- Parameters:
key
- the key- Returns:
- the boolean value
- Throws:
IllegalArgumentException
- if the key is present but the value is not a boolean
-
getBoolean
Returns a boolean value in the map, returning the defaultValue if no value was found.- Parameters:
key
- the keydefaultValue
- the default- Returns:
- the boolean value
- Throws:
IllegalArgumentException
- if the key is present but the value is not a boolean
-
getRequiredBoolean
Returns a boolean value in the map, throwing an exception if the value is not present and of the correct type.- Parameters:
key
- the attribute- Returns:
- the boolean value
- Throws:
IllegalArgumentException
- if the key is not present or present but the value is not a boolean
-
assertContainsKey
Asserts that the attribute is present in the attribute map.- Parameters:
key
- the key- Throws:
IllegalArgumentException
- if the key is not present
-
containsKey
Indicates if the attribute is present in the attribute map and of the required type.- Parameters:
key
- the attribute name- Returns:
- true if present and of the required type, false if not present.
- Throws:
IllegalArgumentException
-
assertKeyValueOfType
Assert that value of the map key, if non-null, is of the required type.- Parameters:
key
- the attribute namerequiredType
- the required attribute value type- Returns:
- the attribute value
-
assertKeyValueInstanceOf
Assert that the key value, if non null, is an instance of the required type.- Parameters:
key
- the keyvalue
- the valuerequiredType
- the required type- Returns:
- the value
-