org.apache.pivot.beans
Class BeanDictionary

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

public class BeanDictionary
extends Object
implements Dictionary<String,Object>, Iterable<String>

Exposes Java bean properties of an object via the Dictionary interface. A call to Dictionary.get(Object) invokes the getter for the corresponding property, and a call to Dictionary.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.


Nested Class Summary
 
Nested classes/interfaces inherited from interface org.apache.pivot.collections.Dictionary
Dictionary.Pair<K,V>
 
Field Summary
static String FIELD_PREFIX
           
static String GET_PREFIX
           
static String IS_PREFIX
           
static String LISTENERS_SUFFIX
           
static String PROPERTY_CHANGE_SUFFIX
           
static String SET_PREFIX
           
 
Constructor Summary
BeanDictionary()
          Creates a new bean dictionary that is not associated with a bean.
BeanDictionary(Object bean)
          Creates a new bean dictionary.
BeanDictionary(Object bean, boolean ignoreReadOnlyProperties)
          Creates a new bean dictionary.
 
Method Summary
static Object coerce(String value, Class<?> type)
          Coerces a string value to a primitive 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.
 ListenerList<BeanDictionaryListener> getBeanDictionaryListeners()
           
static Field getField(Class<?> type, String fieldName)
          Returns the public, non-static field for a property.
static Method getGetterMethod(Class<?> beanClass, String key)
          Returns the getter method for a property.
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 isNotifying(String key)
          Tells whether or not the specified property fires change events.
static boolean isReadOnly(Class<?> type, 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 a setter method for the given property.
 Object remove(String key)
          Removes a key/value pair from the map.
 void setBean(Object bean)
          Sets the bean object that this dictionary will wrap.
 
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

FIELD_PREFIX

public static final String FIELD_PREFIX
See Also:
Constant Field Values

LISTENERS_SUFFIX

public static final String LISTENERS_SUFFIX
See Also:
Constant Field Values

PROPERTY_CHANGE_SUFFIX

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

BeanDictionary

public BeanDictionary()
Creates a new bean dictionary that is not associated with a bean.


BeanDictionary

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

Parameters:
bean - The bean object to wrap.

BeanDictionary

public BeanDictionary(Object bean,
                      boolean ignoreReadOnlyProperties)
Creates a new bean dictionary.

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.

setBean

public void setBean(Object bean)
Sets the bean object that this dictionary will wrap.

NOTE: failing to clear the bean of a bean dictionary may result in memory leaks, as the bean object may maintain references to the bean dictionary as long as it is set.

Parameters:
bean - The bean object, or null to clear the bean.

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 a 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>
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.

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>
Parameters:
key - The key whose mapping is to be removed.
Returns:
The value that was removed.
Throws:
UnsupportedOperationException - This method is not supported.

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.

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.

isNotifying

public boolean isNotifying(String key)
Tells whether or not the specified property fires change events.

Parameters:
key - The property name.
Returns:
true if the property fires change events; false otherwise.

getType

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

Parameters:
key - The property name.
Returns:
The type of the property.

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.

isReadOnly

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

Parameters:
type - 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.

getField

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

Parameters:
type - The bean class
fieldName - 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 Object coerce(String value,
                            Class<?> type)
Coerces a string value to a primitive type.

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

getBeanDictionaryListeners

public ListenerList<BeanDictionaryListener> getBeanDictionaryListeners()