org.apache.pivot.beans
Class BeanAdapter

java.lang.Object
  extended by org.apache.pivot.beans.BeanAdapter
All Implemented Interfaces:
Iterable<String>, Collection<String>, Dictionary<String,Object>, Map<String,Object>

public class BeanAdapter
extends Object
implements Map<String,Object>

Exposes Java bean properties of an object via the Map interface. A call to Dictionary.get(Object) invokes the getter for the corresponding property, and a call to Map.put(Object, Object) invokes the property's setter.

Properties may provide multiple setters; the appropriate setter to invoke is determined by the type of the value being set. If the value is null, the return type of the getter method is used.

Getter methods must be named "getProperty" where "property" is the property name. If there is no "get" method, then an "isProperty" method can also be used. Setter methods (if present) must be named "setProperty".

Getter and setter methods are checked before straight fields named "property" in order to support proper data encapsulation. And only public and non-static methods and fields can be accessed.


Nested Class Summary
 
Nested classes/interfaces inherited from interface org.apache.pivot.collections.Map
Map.MapListenerList<K,V>
 
Nested classes/interfaces inherited from interface org.apache.pivot.collections.Dictionary
Dictionary.Pair<K,V>
 
Field Summary
static String GET_PREFIX
           
static String IS_PREFIX
           
static String SET_PREFIX
           
 
Constructor Summary
BeanAdapter(Object bean)
          Creates a new bean dictionary.
BeanAdapter(Object bean, boolean ignoreReadOnlyProperties)
          Creates a new bean dictionary which can ignore readonly fields (that is, straight fields marked as final or bean properties where there is a "get" method but no corresponding "set" method).
 
Method Summary
 void clear()
          Removes all entries in the map.
static
<T> T
coerce(Object value, Class<? extends T> type)
          Coerces a value to a given type.
 boolean containsKey(String key)
          Verifies the existence of a property.
 Object get(String key)
          Invokes the getter method for the given property.
 Object getBean()
          Returns the bean object this dictionary wraps.
 Comparator<String> getComparator()
          Returns the collection's sort order.
 int getCount()
          Returns the number of entries in the map.
static Field getField(Class<?> beanClass, String key)
          Returns the public, non-static fields for a property.
static Type getGenericType(Class<?> beanClass, String key)
          Returns the generic type of a property.
 Type getGenericType(String key)
          Returns the generic type of a property.
static Method getGetterMethod(Class<?> beanClass, String key)
          Returns the getter method for a property.
 ListenerList<MapListener<String,Object>> getMapListeners()
          Returns the map listener collection.
static Method getSetterMethod(Class<?> beanClass, String key, Class<?> valueType)
          Returns the setter method for a property.
static Class<?> getType(Class<?> beanClass, String key)
          Returns the type of a property.
 Class<?> getType(String key)
          Returns the type of a property.
 boolean isEmpty()
          Tests the emptiness of the collection.
static boolean isReadOnly(Class<?> beanClass, String key)
          Tests the read-only state of a property.
 boolean isReadOnly(String key)
          Tests the read-only state of a property.
 Iterator<String> iterator()
          Returns an iterator over the bean's properties.
 Object put(String key, Object value)
          Invokes the setter method for the given property.
 void putAll(Map<String,?> valueMap)
          Invokes the setter methods for all the given properties that are present in the map.
 boolean putAll(Map<String,?> valueMap, boolean ignoreErrors)
          Invokes the setter methods for all the given properties that are present in the map.
 Object remove(String key)
          Removes a key/value pair from the map.
 void setComparator(Comparator<String> comparator)
          Sets the collection's sort order, re-ordering the collection's contents and ensuring that new entries preserve the sort order.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GET_PREFIX

public static final String GET_PREFIX
See Also:
Constant Field Values

IS_PREFIX

public static final String IS_PREFIX
See Also:
Constant Field Values

SET_PREFIX

public static final String SET_PREFIX
See Also:
Constant Field Values
Constructor Detail

BeanAdapter

public BeanAdapter(Object bean)
Creates a new bean dictionary.

Parameters:
bean - The bean object to wrap.

BeanAdapter

public BeanAdapter(Object bean,
                   boolean ignoreReadOnlyProperties)
Creates a new bean dictionary which can ignore readonly fields (that is, straight fields marked as final or bean properties where there is a "get" method but no corresponding "set" method).

Parameters:
bean - The bean object to wrap.
Method Detail

getBean

public Object getBean()
Returns the bean object this dictionary wraps.

Returns:
The bean object, or null if no bean has been set.

get

public Object get(String key)
Invokes the getter method for the given property.

Specified by:
get in interface Dictionary<String,Object>
Parameters:
key - The property name.
Returns:
The value returned by the method, or null if no such method exists.

put

public Object put(String key,
                  Object value)
Invokes the setter method for the given property. The method signature is determined by the type of the value. If the value is null, the return type of the getter method is used.

Specified by:
put in interface Dictionary<String,Object>
Specified by:
put in interface Map<String,Object>
Parameters:
key - The property name.
value - The new property value.
Returns:
Returns null, since returning the previous value would require a call to the getter method, which may not be an efficient operation.
Throws:
PropertyNotFoundException - If the given property does not exist or is read-only.
See Also:
MapListener.valueAdded(Map, Object), MapListener.valueUpdated(Map, Object, Object)

putAll

public void putAll(Map<String,?> valueMap)
Invokes the setter methods for all the given properties that are present in the map. The method signatures are determined by the type of the values. If any value is null, the return type of the getter method is used.

Parameters:
valueMap - The map of keys and values to be set.
Throws:
PropertyNotFoundException - If any of the given properties do not exist or are read-only.

putAll

public boolean putAll(Map<String,?> valueMap,
                      boolean ignoreErrors)
Invokes the setter methods for all the given properties that are present in the map. The method signatures are determined by the type of the values. If any value is null, the return type of the getter method is used. There is an option to ignore (that is, not throw) exceptions during the process, but to return status if any exceptions were caught and ignored.

Parameters:
valueMap - The map of keys and values to be set.
ignoreErrors - If true then any PropertyNotFoundException thrown by the put() method will be caught and ignored.
Returns:
true if any exceptions were caught, false if not.

remove

public Object remove(String key)
Description copied from interface: Dictionary
Removes a key/value pair from the map.

Specified by:
remove in interface Dictionary<String,Object>
Specified by:
remove in interface Map<String,Object>
Parameters:
key - The key whose mapping is to be removed.
Returns:
The value that was removed.
Throws:
UnsupportedOperationException - This method is not supported.
See Also:
MapListener.valueRemoved(Map, Object, Object)

clear

public void clear()
Description copied from interface: Map
Removes all entries in the map.

Specified by:
clear in interface Collection<String>
Specified by:
clear in interface Map<String,Object>
Throws:
UnsupportedOperationException - This method is not supported.
See Also:
MapListener.mapCleared(Map)

containsKey

public boolean containsKey(String key)
Verifies the existence of a property. The property must have a getter method; write-only properties are not supported.

Specified by:
containsKey in interface Dictionary<String,Object>
Parameters:
key - The property name.
Returns:
true if the property exists; false, otherwise.

isEmpty

public boolean isEmpty()
Description copied from interface: Collection
Tests the emptiness of the collection.

Specified by:
isEmpty in interface Collection<String>
Returns:
true if the collection contains no elements; false, otherwise.
Throws:
UnsupportedOperationException - This method is not supported.

getCount

public int getCount()
Description copied from interface: Map
Returns the number of entries in the map.

Specified by:
getCount in interface Map<String,Object>
Throws:
UnsupportedOperationException - This method is not supported.

getComparator

public Comparator<String> getComparator()
Description copied from interface: Collection
Returns the collection's sort order.

Specified by:
getComparator in interface Collection<String>
Returns:
The comparator used to order elements in the collection, or null if the sort order is undefined.
See Also:
Collection.setComparator(Comparator)

setComparator

public void setComparator(Comparator<String> comparator)
Description copied from interface: Collection
Sets the collection's sort order, re-ordering the collection's contents and ensuring that new entries preserve the sort order.

Calling this method more than once with the same comparator will re-sort the collection.

Specified by:
setComparator in interface Collection<String>
Specified by:
setComparator in interface Map<String,Object>
Parameters:
comparator - The comparator used to order elements in the collection, or null if the collection is unsorted.
Throws:
UnsupportedOperationException - This method is not supported.
See Also:
MapListener.comparatorChanged(Map, Comparator)

isReadOnly

public boolean isReadOnly(String key)
Tests the read-only state of a property.

Parameters:
key - The property name.
Returns:
true if the property is read-only; false, otherwise.

getType

public Class<?> getType(String key)
Returns the type of a property.

Parameters:
key - The property name.
See Also:
getType(Class, String)

getGenericType

public Type getGenericType(String key)
Returns the generic type of a property.

Parameters:
key - The property name.
See Also:
getGenericType(Class, String)

iterator

public Iterator<String> iterator()
Returns an iterator over the bean's properties.

Specified by:
iterator in interface Iterable<String>
Returns:
A property iterator for this bean.

getMapListeners

public ListenerList<MapListener<String,Object>> getMapListeners()
Description copied from interface: Map
Returns the map listener collection.

Specified by:
getMapListeners in interface Map<String,Object>

isReadOnly

public static boolean isReadOnly(Class<?> beanClass,
                                 String key)
Tests the read-only state of a property. Note that if no such property exists, this method will return true (it will not throw an exception).

Parameters:
beanClass - The bean class.
key - The property name.
Returns:
true if the property is read-only; false, otherwise.

getType

public static Class<?> getType(Class<?> beanClass,
                               String key)
Returns the type of a property.

Parameters:
beanClass - The bean class.
key - The property name.
Returns:
The type of the property, or null if no such bean property exists.

getGenericType

public static Type getGenericType(Class<?> beanClass,
                                  String key)
Returns the generic type of a property.

Parameters:
beanClass - The bean class.
key - The property name.
Returns:
The generic type of the property, or null if no such bean property exists. If the type is a generic, an instance of ParameterizedType will be returned. Otherwise, an instance of Class will be returned.

getField

public static Field getField(Class<?> beanClass,
                             String key)
Returns the public, non-static fields for a property. Note that fields will only be consulted for bean properties after bean methods.

Parameters:
beanClass - The bean class.
key - The property name.
Returns:
The field, or null if the field does not exist, or is non-public or static.

getGetterMethod

public static Method getGetterMethod(Class<?> beanClass,
                                     String key)
Returns the getter method for a property.

Parameters:
beanClass - The bean class.
key - The property name.
Returns:
The getter method, or null if the method does not exist.

getSetterMethod

public static Method getSetterMethod(Class<?> beanClass,
                                     String key,
                                     Class<?> valueType)
Returns the setter method for a property.

Parameters:
beanClass - The bean class.
key - The property name.
Returns:
The getter method, or null if the method does not exist.

coerce

public static <T> T coerce(Object value,
                           Class<? extends T> type)
Coerces a value to a given type.

Parameters:
value -
type -
Returns:
The coerced value.