Package org.apache.pivot.beans
Class BeanAdapter
- java.lang.Object
-
- org.apache.pivot.beans.BeanAdapter
-
- All Implemented Interfaces:
java.lang.Iterable<java.lang.String>,Collection<java.lang.String>,Dictionary<java.lang.String,java.lang.Object>,Map<java.lang.String,java.lang.Object>
public class BeanAdapter extends java.lang.Object implements Map<java.lang.String,java.lang.Object>
Exposes Java bean properties of an object via theMapinterface. A call toDictionary.get(Object)invokes the getter for the corresponding property, and a call toMap.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
publicand non-staticmethods and fields can be accessed.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.pivot.collections.Dictionary
Dictionary.Pair<K,V>
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringGET_PREFIXPrefix for "getProperty" method names.static java.lang.StringIS_PREFIXPrefix for "isProperty" method names.static java.lang.StringSET_PREFIXPrefix for "setProperty" method names.
-
Constructor Summary
Constructors Constructor Description BeanAdapter(java.lang.Object beanObject)Creates a new bean dictionary.BeanAdapter(java.lang.Object beanObject, boolean ignoreReadOnlyValue)Creates a new bean dictionary which can ignore readonly fields (that is, straight fields marked asfinalor bean properties where there is a "get" method but no corresponding "set" method).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all entries in the map.static <T> Tcoerce(java.lang.Object value, java.lang.Class<? extends T> type, java.lang.String key)Coerces a value to a given type.booleancontainsKey(java.lang.String key)Verifies the existence of a property.java.lang.Objectget(java.lang.String key)Invokes the getter method for the given property.java.lang.ObjectgetBean()Returns the bean object this dictionary wraps.java.util.Comparator<java.lang.String>getComparator()Returns the collection's sort order.intgetCount()Returns the number of entries in the map.static java.lang.reflect.FieldgetField(java.lang.Class<?> beanClass, java.lang.String key)Returns the public, non-static fields for a property.static java.lang.reflect.TypegetGenericType(java.lang.Class<?> beanClass, java.lang.String key)Returns the generic type of a property.java.lang.reflect.TypegetGenericType(java.lang.String key)Returns the generic type of a property.static java.lang.reflect.MethodgetGetterMethod(java.lang.Class<?> beanClass, java.lang.String key)Returns the getter method for a property.ListenerList<MapListener<java.lang.String,java.lang.Object>>getMapListeners()Returns the map listener collection.static java.lang.reflect.MethodgetSetterMethod(java.lang.Class<?> beanClass, java.lang.String key, java.lang.Class<?> valueType)Returns the setter method for a property.static java.lang.Class<?>getType(java.lang.Class<?> beanClass, java.lang.String key)Returns the type of a property.java.lang.Class<?>getType(java.lang.String key)Returns the type of a property.booleanisEmpty()Tests the emptiness of the collection.static booleanisReadOnly(java.lang.Class<?> beanClass, java.lang.String key)Tests the read-only state of a property.booleanisReadOnly(java.lang.String key)Tests the read-only state of a property.java.util.Iterator<java.lang.String>iterator()Returns an iterator over the bean's properties.java.lang.Objectput(java.lang.String key, java.lang.Object value)Invokes the setter method for the given property.booleanputAll(Map<java.lang.String,?> valueMap, boolean ignoreErrors)Invokes the setter methods for all the given properties that are present in the map.java.lang.Objectremove(java.lang.String key)Removes a key/value pair from the map.voidsetComparator(java.util.Comparator<java.lang.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
-
Methods inherited from interface org.apache.pivot.collections.Dictionary
containsAny, copy, getBoolean, getBoolean, getColor, getFirst, getFont, getInt, getInt, getString, getString, putAll
-
-
-
-
Field Detail
-
GET_PREFIX
public static final java.lang.String GET_PREFIX
Prefix for "getProperty" method names.- See Also:
- Constant Field Values
-
IS_PREFIX
public static final java.lang.String IS_PREFIX
Prefix for "isProperty" method names.- See Also:
- Constant Field Values
-
SET_PREFIX
public static final java.lang.String SET_PREFIX
Prefix for "setProperty" method names.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
BeanAdapter
public BeanAdapter(java.lang.Object beanObject)
Creates a new bean dictionary.- Parameters:
beanObject- The bean object to wrap.
-
BeanAdapter
public BeanAdapter(java.lang.Object beanObject, boolean ignoreReadOnlyValue)Creates a new bean dictionary which can ignore readonly fields (that is, straight fields marked asfinalor bean properties where there is a "get" method but no corresponding "set" method).- Parameters:
beanObject- The bean object to wrap.ignoreReadOnlyValue-trueiffinalor non-settable fields should be excluded from the dictionary,falseto include all fields.
-
-
Method Detail
-
getBean
public java.lang.Object getBean()
Returns the bean object this dictionary wraps.- Returns:
- The bean object, or
nullif no bean has been set.
-
get
public java.lang.Object get(java.lang.String key)
Invokes the getter method for the given property.- Specified by:
getin interfaceDictionary<java.lang.String,java.lang.Object>- Parameters:
key- The property name.- Returns:
- The value returned by the method, or
nullif no such method exists.
-
put
public java.lang.Object put(java.lang.String key, java.lang.Object value)Invokes the setter method for the given property. The method signature is determined by the type of the value. If the value isnull, the return type of the getter method is used.- Specified by:
putin interfaceDictionary<java.lang.String,java.lang.Object>- Specified by:
putin interfaceMap<java.lang.String,java.lang.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 boolean putAll(Map<java.lang.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 isnull, 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- Iftruethen anyPropertyNotFoundExceptionthrown by theput()method will be caught and ignored.- Returns:
trueif any exceptions were caught,falseif not.
-
remove
@UnsupportedOperation public java.lang.Object remove(java.lang.String key)
Description copied from interface:DictionaryRemoves a key/value pair from the map.- Specified by:
removein interfaceDictionary<java.lang.String,java.lang.Object>- Specified by:
removein interfaceMap<java.lang.String,java.lang.Object>- Parameters:
key- The key whose mapping is to be removed.- Returns:
- The value that was removed.
- Throws:
java.lang.UnsupportedOperationException- This operation is not supported.- See Also:
MapListener.valueRemoved(Map, Object, Object)
-
clear
@UnsupportedOperation public void clear()
Description copied from interface:MapRemoves all entries in the map.- Specified by:
clearin interfaceCollection<java.lang.String>- Specified by:
clearin interfaceMap<java.lang.String,java.lang.Object>- Throws:
java.lang.UnsupportedOperationException- This operation is not supported.- See Also:
MapListener.mapCleared(Map)
-
containsKey
public boolean containsKey(java.lang.String key)
Verifies the existence of a property. The property must have a getter method; write-only properties are not supported.- Specified by:
containsKeyin interfaceDictionary<java.lang.String,java.lang.Object>- Parameters:
key- The property name.- Returns:
trueif the property exists;false, otherwise.
-
isEmpty
@UnsupportedOperation public boolean isEmpty()
Description copied from interface:CollectionTests the emptiness of the collection.- Specified by:
isEmptyin interfaceCollection<java.lang.String>- Returns:
trueif the collection contains no elements;false, otherwise.- Throws:
java.lang.UnsupportedOperationException- This operation is not supported.
-
getCount
@UnsupportedOperation public int getCount()
Description copied from interface:MapReturns the number of entries in the map.
-
getComparator
public final java.util.Comparator<java.lang.String> getComparator()
Description copied from interface:CollectionReturns the collection's sort order.- Specified by:
getComparatorin interfaceCollection<java.lang.String>- Returns:
- The comparator used to order elements in the collection, or
nullif the sort order is undefined. - See Also:
Collection.setComparator(Comparator)
-
setComparator
@UnsupportedOperation public void setComparator(java.util.Comparator<java.lang.String> comparator)
Description copied from interface:CollectionSets 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:
setComparatorin interfaceCollection<java.lang.String>- Specified by:
setComparatorin interfaceMap<java.lang.String,java.lang.Object>- Parameters:
comparator- The comparator used to order elements in the collection, or null if the collection is unsorted.- Throws:
java.lang.UnsupportedOperationException- This operation is not supported.- See Also:
MapListener.comparatorChanged(Map, Comparator)
-
isReadOnly
public boolean isReadOnly(java.lang.String key)
Tests the read-only state of a property.- Parameters:
key- The property name.- Returns:
trueif the property is read-only;false, otherwise.
-
getType
public java.lang.Class<?> getType(java.lang.String key)
Returns the type of a property.- Parameters:
key- The property name.- Returns:
- The real class type of this property.
- See Also:
getType(Class, String)
-
getGenericType
public java.lang.reflect.Type getGenericType(java.lang.String key)
Returns the generic type of a property.- Parameters:
key- The property name.- Returns:
- The generic type of this property.
- See Also:
getGenericType(Class, String)
-
iterator
public java.util.Iterator<java.lang.String> iterator()
Returns an iterator over the bean's properties.- Specified by:
iteratorin interfacejava.lang.Iterable<java.lang.String>- Returns:
- A property iterator for this bean.
-
getMapListeners
public final ListenerList<MapListener<java.lang.String,java.lang.Object>> getMapListeners()
Description copied from interface:MapReturns the map listener collection.- Specified by:
getMapListenersin interfaceMap<java.lang.String,java.lang.Object>- Returns:
- Current list of listeners on this map.
-
isReadOnly
public static boolean isReadOnly(java.lang.Class<?> beanClass, java.lang.String key)Tests the read-only state of a property. Note that if no such property exists, this method will returntrue(it will not throw an exception).- Parameters:
beanClass- The bean class.key- The property name.- Returns:
trueif the property is read-only;false, otherwise.
-
getType
public static java.lang.Class<?> getType(java.lang.Class<?> beanClass, java.lang.String key)Returns the type of a property.- Parameters:
beanClass- The bean class.key- The property name.- Returns:
- The type of the property, or
nullif no such bean property exists.
-
getGenericType
public static java.lang.reflect.Type getGenericType(java.lang.Class<?> beanClass, java.lang.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
nullif no such bean property exists. If the type is a generic, an instance ofParameterizedTypewill be returned. Otherwise, an instance ofClasswill be returned.
-
getField
public static java.lang.reflect.Field getField(java.lang.Class<?> beanClass, java.lang.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
nullif the field does not exist, or is non-public or static.
-
getGetterMethod
public static java.lang.reflect.Method getGetterMethod(java.lang.Class<?> beanClass, java.lang.String key)Returns the getter method for a property.- Parameters:
beanClass- The bean class.key- The property name.- Returns:
- The getter method, or
nullif the method does not exist.
-
getSetterMethod
public static java.lang.reflect.Method getSetterMethod(java.lang.Class<?> beanClass, java.lang.String key, java.lang.Class<?> valueType)Returns the setter method for a property.- Parameters:
beanClass- The bean class.key- The property name.valueType- The type of the property.- Returns:
- The setter method, or
nullif the method does not exist.
-
coerce
public static <T> T coerce(java.lang.Object value, java.lang.Class<? extends T> type, java.lang.String key)Coerces a value to a given type.- Type Parameters:
T- The parametric type to coerce to.- Parameters:
value- The object to be coerced.type- The type to coerce it to.key- The property name in question.- Returns:
- The coerced value.
- Throws:
java.lang.IllegalArgumentException- for all the possible other exceptions.
-
-