Class BeanMap<T>

java.lang.Object
java.util.AbstractMap<String,Object>
org.apache.juneau.BeanMap<T>
Type Parameters:
T - Specifies the type of object that this map encapsulates.
All Implemented Interfaces:
Map<String,Object>, Delegate<T>
Direct Known Subclasses:
DelegateBeanMap

public class BeanMap<T> extends AbstractMap<String,Object> implements Delegate<T>
Java bean wrapper class.
Description
A wrapper that wraps Java bean instances inside of a Map 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 the keySet() 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 the BeanInfo class (i.e. ordered by definition in the class).
POJO swaps
If ObjectSwaps 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:
  • Field Details

    • bean

      protected T 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

      protected BeanMeta<T> meta
      The BeanMeta associated with the class of the object.
  • Constructor Details

    • BeanMap

      protected BeanMap(BeanSession session, T bean, BeanMeta<T> meta)
      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

      public static <T> BeanMap<T> of(T bean)
      Convenience method for wrapping a bean inside a BeanMap.
      Type Parameters:
      T - The bean type.
      Parameters:
      bean - The bean being wrapped.
      Returns:
      A new BeanMap instance wrapping the bean.
    • getMeta

      public BeanMeta<T> getMeta()
      Returns the metadata associated with this bean map.
      Returns:
      The metadata associated with this bean map.
    • getBeanSession

      public final BeanSession getBeanSession()
      Returns the bean session that created this bean map.
      Returns:
      The bean session that created this bean map.
    • getBean

      public T 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

      public T getBean(boolean create)
      Returns the wrapped bean object.

      If create is false, then this method may return null 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

      public Object put(String property, Object value)
      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 a Date and the bean property has the TemporalDateSwap.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 Person person = new Person(); // Create a bean context and add the ISO8601 date-time swap BeanContext beanContext = 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 interface Map<String,Object>
      Overrides:
      put in class AbstractMap<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 is true, then the old value of the property is returned. Otherwise, this method always returns null.
      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

      public boolean containsKey(Object property)
      Specified by:
      containsKey in interface Map<String,Object>
      Overrides:
      containsKey in class AbstractMap<String,Object>
    • add

      public void add(String property, Object value)
      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

      public Object get(Object property)
      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 a Date and the bean property has the TemporalDateSwap.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 Person person = new Person(); person.setBirthDate(new Date(1, 2, 3, 4, 5, 6)); // Create a bean context and add the ISO8601 date-time swap BeanContext beanContext = 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'") String birthDate = beanMap.get("birthDate");

      Specified by:
      get in interface Map<String,Object>
      Overrides:
      get in class AbstractMap<String,Object>
      Parameters:
      property - The name of the property to get.
      Returns:
      The property value.
      Throws:
      RuntimeException - if any of the following occur.
      1. BeanMapEntry does not exist on the underlying object.
      2. Security settings prevent access to the underlying object getter method.
      3. An exception occurred inside the getter method.
    • get

      public <T2> T2 get(String property, Class<T2> c)
      Same as get(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.
      1. BeanMapEntry does not exist on the underlying object.
      2. Security settings prevent access to the underlying object getter method.
      3. An exception occurred inside the getter method.
      ClassCastException - if property is not the specified type.
    • getRaw

      public Object getRaw(Object property)
      Same as get(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

      public BeanMap<T> load(String input) throws ParseException
      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 by Reader.
    • load

      public BeanMap<T> load(Map entries)
      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

      public Set<String> keySet()
      Returns the names of all properties associated with the bean.

      The returned set is unmodifiable.

      Specified by:
      keySet in interface Map<String,Object>
      Overrides:
      keySet in class AbstractMap<String,Object>
    • getProperty

      public BeanMapEntry getProperty(String propertyName)
      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() and BeanMapEntry.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

      public BeanPropertyMeta getPropertyMeta(String propertyName)
      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 the ClassMeta of the wrapped bean.
      Specified by:
      getClassMeta in interface Delegate<T>
      Returns:
      The class type of the wrapped bean.
    • getProperties

      public Map<String,Object> getProperties(String... fields)
      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

      public BeanMap<T> forEachValue(Predicate<Object> valueFilter, BeanPropertyConsumer action)
      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

      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.
      Specified by:
      entrySet in interface Map<String,Object>
      Specified by:
      entrySet in class AbstractMap<String,Object>
      Returns:
      A new set.