Class BeanMap<T>
- Type Parameters:
T
- Specifies the type of object that this map encapsulates.
- Direct Known Subclasses:
DelegateBeanMap
Description
A wrapper that wraps Java bean instances inside of aMap
interface that allows properties on the wrapped
object can be accessed using the get()
and put()
methods.
Use the BeanContext
class to create instances of this class.
Bean property order
The order of the properties returned by thekeySet()
and entrySet()
methods are as follows:
-
If
@Bean
annotation is specified on class, then the order is the same as the list of properties in the annotation. -
If
@Bean
annotation is not specified on the class, then the order is the same as that returned by theBeanInfo
class (i.e. ordered by definition in the class).
POJO swaps
IfObjectSwaps
are defined on the class types of the properties of this bean or the bean properties
themselves, the get(Object)
and put(String, Object)
methods will automatically transform the
property value to and from the serialized form.
See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,
V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Field Summary
Modifier and TypeFieldDescriptionTemporary holding cache for bean properties of array types when the add() method is being used.protected T
The wrapped object.The BeanMeta associated with the class of the object.Temporary holding cache for beans with read-only properties. -
Constructor Summary
ModifierConstructorDescriptionprotected
BeanMap
(BeanSession session, T bean, BeanMeta<T> meta) Instance of this class are instantiated through the BeanContext class. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Add a value to a collection or array property.boolean
containsKey
(Object property) entrySet()
Returns all the properties associated with the bean.forEachProperty
(Predicate<BeanPropertyMeta> filter, Consumer<BeanPropertyMeta> action) Performs an action on each property in this bean map.forEachValue
(Predicate<Object> valueFilter, BeanPropertyConsumer action) Invokes all the getters on this bean and consumes the results.Gets a property on the bean.<T2> T2
Same asget(Object)
but casts the value to the specific type.getBean()
Returns the wrapped bean object.getBean
(boolean create) Returns the wrapped bean object.final BeanSession
Returns the bean session that created this bean map.Returns theClassMeta
of the wrapped bean.getMeta()
Returns the metadata associated with this bean map.protected Collection<BeanPropertyMeta>
Returns a simple collection of properties for this bean map.getProperties
(String... fields) Extracts the specified field values from this bean and returns it as a simple Map.getProperty
(String propertyName) Returns the specified property on this bean map.getPropertyMeta
(String propertyName) Returns the metadata on the specified property.Same asget(Object)
except bypasses the POJO filter associated with the bean property or bean filter associated with the bean class.keySet()
Returns the names of all properties associated with the bean.load
(Reader r, ReaderParser p) Convenience method for setting multiple property values by passing in a reader.Convenience method for setting multiple property values by passing in JSON text.Convenience method for loading this map with the contents of the specified map.static <T> BeanMap<T>
of
(T bean) Convenience method for wrapping a bean inside aBeanMap
.Sets a property on the bean.Given a string containing variables of the form"{property}" , replaces those variables with property values in this bean.Methods inherited from class java.util.AbstractMap
clear, clone, containsValue, equals, hashCode, isEmpty, putAll, remove, size, toString, values
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
bean
The wrapped object. -
propertyCache
Temporary holding cache for beans with read-only properties. Normally null. -
arrayPropertyCache
Temporary holding cache for bean properties of array types when the add() method is being used. -
meta
The BeanMeta associated with the class of the object.
-
-
Constructor Details
-
BeanMap
Instance of this class are instantiated through the BeanContext class.- Parameters:
session
- The bean session object that created this bean map.bean
- The bean to wrap inside this map.meta
- The metadata associated with the bean class.
-
-
Method Details
-
of
Convenience method for wrapping a bean inside aBeanMap
.- Type Parameters:
T
- The bean type.- Parameters:
bean
- The bean being wrapped.- Returns:
- A new
BeanMap
instance wrapping the bean.
-
getMeta
Returns the metadata associated with this bean map.- Returns:
- The metadata associated with this bean map.
-
getBeanSession
Returns the bean session that created this bean map.- Returns:
- The bean session that created this bean map.
-
getBean
Returns the wrapped bean object.Triggers bean creation if bean has read-only properties set through a constructor defined by the
@Beanc
annotation.- Returns:
- The inner bean object.
-
getBean
Returns the wrapped bean object.If
create isfalse , then this method may returnnull if the bean has read-only properties set through a constructor defined by the@Beanc
annotation.This method does NOT always return the bean in it's final state. Array properties temporary stored as ArrayLists are not finalized until the
getBean()
method is called.- Parameters:
create
- If bean hasn't been instantiated yet, then instantiate it.- Returns:
- The inner bean object.
-
put
Sets a property on the bean.If there is a
ObjectSwap
associated with this bean property or bean property type class, then you must pass in a transformed value. For example, if the bean property type class is aDate
and the bean property has theTemporalDateSwap.IsoInstant
swap associated with it through the@Swap(value)
annotation, the value being passed in must be a String containing an ISO8601 date-time string value.Example:
// Construct a bean with a 'birthDate' Date field Personperson =new Person();// Create a bean context and add the ISO8601 date-time swap BeanContextbeanContext = BeanContext.create ().swaps(DateSwap.ISO8601DT.class ).build();// Wrap our bean in a bean map BeanMap<Person>beanMap =beanContext .toBeanMap(person );// Set the field beanMap .put("birthDate" ,"'1901-03-03T04:05:06-5000'" );- Specified by:
put
in interfaceMap<String,
Object> - Overrides:
put
in classAbstractMap<String,
Object> - Parameters:
property
- The name of the property to set.value
- The value to set the property to.- Returns:
- If the bean context setting
beanMapPutReturnsOldValue
istrue , then the old value of the property is returned. Otherwise, this method always returnsnull . - Throws:
RuntimeException
- if any of the following occur.- BeanMapEntry does not exist on the underlying object.
- Security settings prevent access to the underlying object setter method.
- An exception occurred inside the setter method.
-
containsKey
- Specified by:
containsKey
in interfaceMap<String,
Object> - Overrides:
containsKey
in classAbstractMap<String,
Object>
-
add
Add a value to a collection or array property.As a general rule, adding to arrays is not recommended since the array must be recreate each time this method is called.
- Parameters:
property
- Property name or child-element name (if@Xml(childName)
is specified).value
- The value to add to the collection or array.
-
get
Gets a property on the bean.If there is a
ObjectSwap
associated with this bean property or bean property type class, then this method will return the transformed value. For example, if the bean property type class is aDate
and the bean property has theTemporalDateSwap.IsoInstant
swap associated with it through the@Swap(value)
annotation, this method will return a String containing an ISO8601 date-time string value.Example:
// Construct a bean with a 'birthDate' Date field Personperson =new Person();person .setBirthDate(new Date(1, 2, 3, 4, 5, 6));// Create a bean context and add the ISO8601 date-time swap BeanContextbeanContext = BeanContext.create ().swaps(DateSwap.ISO8601DT.class ).build();// Wrap our bean in a bean map BeanMap<Person>beanMap =beanContext .toBeanMap(person );// Get the field as a string (i.e. "'1901-03-03T04:05:06-5000'") StringbirthDate =beanMap .get("birthDate" );- Specified by:
get
in interfaceMap<String,
Object> - Overrides:
get
in classAbstractMap<String,
Object> - Parameters:
property
- The name of the property to get.- Returns:
- The property value.
- Throws:
RuntimeException
- if any of the following occur.- BeanMapEntry does not exist on the underlying object.
- Security settings prevent access to the underlying object getter method.
- An exception occurred inside the getter method.
-
get
Same asget(Object)
but casts the value to the specific type.- Type Parameters:
T2
- The type to cast to.- Parameters:
property
- The name of the property to get.c
- The type to cast to.- Returns:
- The property value.
- Throws:
RuntimeException
- if any of the following occur.- BeanMapEntry does not exist on the underlying object.
- Security settings prevent access to the underlying object getter method.
- An exception occurred inside the getter method.
ClassCastException
- if property is not the specified type.
-
getRaw
Same asget(Object)
except bypasses the POJO filter associated with the bean property or bean filter associated with the bean class.- Parameters:
property
- The name of the property to get.- Returns:
- The raw property value.
-
load
Convenience method for setting multiple property values by passing in JSON text.Example:
beanMap .load("{name:'John Smith',age:21}" )- Parameters:
input
- The text that will get parsed into a map and then added to this map.- Returns:
- This object.
- Throws:
ParseException
- Malformed input encountered.
-
load
Convenience method for setting multiple property values by passing in a reader.- Parameters:
r
- The text that will get parsed into a map and then added to this map.p
- The parser to use to parse the text.- Returns:
- This object.
- Throws:
ParseException
- Malformed input encountered.IOException
- Thrown byReader .
-
load
Convenience method for loading this map with the contents of the specified map.Identical to
AbstractMap.putAll(Map)
except as a fluent-style method.- Parameters:
entries
- The map containing the entries to add to this map.- Returns:
- This object.
-
keySet
Returns the names of all properties associated with the bean.The returned set is unmodifiable.
-
getProperty
Returns the specified property on this bean map.Allows you to get and set an individual property on a bean without having a handle to the bean itself by using the
BeanMapEntry.getValue()
andBeanMapEntry.setValue(Object)
methods.This method can also be used to get metadata on a property by calling the
BeanMapEntry.getMeta()
method.- Parameters:
propertyName
- The name of the property to look up.- Returns:
- The bean property, or null if the bean has no such property.
-
getPropertyMeta
Returns the metadata on the specified property.- Parameters:
propertyName
- The name of the bean property.- Returns:
- Metadata on the specified property, or
null if that property does not exist.
-
getClassMeta
Returns theClassMeta
of the wrapped bean.- Specified by:
getClassMeta
in interfaceDelegate<T>
- Returns:
- The class type of the wrapped bean.
-
getProperties
Extracts the specified field values from this bean and returns it as a simple Map.- Parameters:
fields
- The fields to extract.- Returns:
- A new map with fields as key-value pairs.
Note that modifying the values in this map will also modify the underlying bean.
-
forEachValue
Invokes all the getters on this bean and consumes the results.- Parameters:
valueFilter
- Filter to apply to value before applying action.action
- The action to perform.- Returns:
- The list of all bean property values.
-
resolveVars
Given a string containing variables of the form"{property}" , replaces those variables with property values in this bean.- Parameters:
s
- The string containing variables.- Returns:
- A new string with variables replaced, or the same string if no variables were found.
-
getProperties
Returns a simple collection of properties for this bean map.- Returns:
- A simple collection of properties for this bean map.
-
forEachProperty
public BeanMap<T> forEachProperty(Predicate<BeanPropertyMeta> filter, Consumer<BeanPropertyMeta> action) Performs an action on each property in this bean map.- Parameters:
filter
- The filter to apply to properties.action
- The action.- Returns:
- This object.
-
entrySet
Returns all the properties associated with the bean.
-