public class BeanMap
extends java.util.AbstractMap<java.lang.Object,java.lang.Object>
implements java.lang.Cloneable
If an exception occurs during attempts to get or set a property then the property is considered non existent in the Map
Modifier and Type | Class and Description |
---|---|
protected static class |
BeanMap.Entry
Map entry used by
BeanMap . |
Modifier and Type | Field and Description |
---|---|
private java.lang.Object |
bean |
static java.util.HashMap |
defaultTransformers
Deprecated.
Use
getTypeTransformer(Class) method |
static java.lang.Object[] |
NULL_ARGUMENTS
An empty array.
|
private java.util.HashMap<java.lang.String,java.lang.reflect.Method> |
readMethods |
private java.util.HashMap<java.lang.String,java.lang.Class<? extends java.lang.Object>> |
types |
private static java.util.Map<java.lang.Class<? extends java.lang.Object>,org.apache.commons.collections.Transformer> |
typeTransformers
Maps primitive Class types to transformers.
|
private java.util.HashMap<java.lang.String,java.lang.reflect.Method> |
writeMethods |
Constructor and Description |
---|
BeanMap()
Constructs a new empty
BeanMap . |
BeanMap(java.lang.Object bean)
Constructs a new
BeanMap that operates on the specified bean. |
Modifier and Type | Method and Description |
---|---|
void |
clear()
This method reinitializes the bean map to have default values for the bean's properties.
|
java.lang.Object |
clone()
Clone this bean map using the following process:
If there is no underlying bean, return a cloned BeanMap without a bean.
Since there is an underlying bean, try to instantiate a new bean of the same type using Class.newInstance().
If the instantiation fails, throw a CloneNotSupportedException
Clone the bean map and set the newly instantiated bean as the underlying bean for the bean map.
Copy each property that is both readable and writable from the existing object to a cloned bean map.
If anything fails along the way, throw a CloneNotSupportedException.
|
boolean |
containsKey(java.lang.Object name)
Returns true if the bean defines a property with the given name.
|
boolean |
containsValue(java.lang.Object value)
Returns true if the bean defines a property whose current value is the given object.
|
protected java.lang.Object |
convertType(java.lang.Class<?> newType,
java.lang.Object value)
Converts the given value to the given type.
|
private static java.util.Map<java.lang.Class<? extends java.lang.Object>,org.apache.commons.collections.Transformer> |
createTypeTransformers() |
protected java.lang.Object[] |
createWriteMethodArguments(java.lang.reflect.Method method,
java.lang.Object value)
Creates an array of parameters to pass to the given mutator method.
|
java.util.Iterator<java.util.Map.Entry<java.lang.Object,java.lang.Object>> |
entryIterator()
Convenience method for getting an iterator over the entries.
|
java.util.Set<java.util.Map.Entry<java.lang.Object,java.lang.Object>> |
entrySet()
Gets a Set of MapEntry objects that are the mappings for this BeanMap.
|
protected void |
firePropertyChange(java.lang.Object key,
java.lang.Object oldValue,
java.lang.Object newValue)
Called during a successful
put(Object,Object) operation. |
java.lang.Object |
get(java.lang.Object name)
Returns the value of the bean's property with the given name.
|
java.lang.Object |
getBean()
Returns the bean currently being operated on.
|
protected java.lang.reflect.Method |
getReadMethod(java.lang.Object name)
Returns the accessor for the property with the given name.
|
java.lang.reflect.Method |
getReadMethod(java.lang.String name)
Returns the accessor for the property with the given name.
|
java.lang.Class<?> |
getType(java.lang.String name)
Returns the type of the property with the given name.
|
protected org.apache.commons.collections.Transformer |
getTypeTransformer(java.lang.Class<?> aType)
Returns a transformer for the given primitive type.
|
protected java.lang.reflect.Method |
getWriteMethod(java.lang.Object name)
Returns the mutator for the property with the given name.
|
java.lang.reflect.Method |
getWriteMethod(java.lang.String name)
Returns the mutator for the property with the given name.
|
private void |
initialise() |
java.util.Iterator<java.lang.String> |
keyIterator()
Convenience method for getting an iterator over the keys.
|
java.util.Set<java.lang.Object> |
keySet()
Get the keys for this BeanMap.
|
protected void |
logInfo(java.lang.Exception ex)
Logs the given exception to
System.out . |
protected void |
logWarn(java.lang.Exception ex)
Logs the given exception to
System.err . |
java.lang.Object |
put(java.lang.Object name,
java.lang.Object value)
Sets the bean property with the given name to the given value.
|
void |
putAllWriteable(BeanMap map)
Puts all of the writable properties from the given BeanMap into this BeanMap.
|
protected void |
reinitialise()
Reinitializes this bean.
|
void |
setBean(java.lang.Object newBean)
Sets the bean to be operated on by this map.
|
int |
size()
Returns the number of properties defined by the bean.
|
java.lang.String |
toString()
Renders a string representation of this object.
|
java.util.Iterator<java.lang.Object> |
valueIterator()
Convenience method for getting an iterator over the values.
|
java.util.Collection<java.lang.Object> |
values()
Returns the values for the BeanMap.
|
public static final java.lang.Object[] NULL_ARGUMENTS
private static final java.util.Map<java.lang.Class<? extends java.lang.Object>,org.apache.commons.collections.Transformer> typeTransformers
@Deprecated public static java.util.HashMap defaultTransformers
getTypeTransformer(Class)
methodprivate transient java.lang.Object bean
private transient java.util.HashMap<java.lang.String,java.lang.reflect.Method> readMethods
private transient java.util.HashMap<java.lang.String,java.lang.reflect.Method> writeMethods
private transient java.util.HashMap<java.lang.String,java.lang.Class<? extends java.lang.Object>> types
public BeanMap()
BeanMap
.public BeanMap(java.lang.Object bean)
BeanMap
that operates on the specified bean. If the given bean is null
, then this map will be empty.bean
- the bean for this map to operate onprivate static java.util.Map<java.lang.Class<? extends java.lang.Object>,org.apache.commons.collections.Transformer> createTypeTransformers()
public void clear()
clear()
differs from the Map contract in that the mappings are not
actually removed from the map (the mappings for a BeanMap are fixed).clear
in interface java.util.Map<java.lang.Object,java.lang.Object>
clear
in class java.util.AbstractMap<java.lang.Object,java.lang.Object>
public java.lang.Object clone() throws java.lang.CloneNotSupportedException
clone
in class java.util.AbstractMap<java.lang.Object,java.lang.Object>
java.lang.CloneNotSupportedException
- if the underlying bean cannot be clonedpublic boolean containsKey(java.lang.Object name)
The given name must be a String
; if not, this method returns false. This method will also return false if the bean does not define a
property with that name.
Write-only properties will not be matched as the test operates against property read methods.
containsKey
in interface java.util.Map<java.lang.Object,java.lang.Object>
containsKey
in class java.util.AbstractMap<java.lang.Object,java.lang.Object>
name
- the name of the property to checkString
; false if the bean does not define a property with that name; or true if the bean
does define a property with that namepublic boolean containsValue(java.lang.Object value)
containsValue
in interface java.util.Map<java.lang.Object,java.lang.Object>
containsValue
in class java.util.AbstractMap<java.lang.Object,java.lang.Object>
value
- the value to checkprotected java.lang.Object convertType(java.lang.Class<?> newType, java.lang.Object value) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetException
If no such constructor exists, and the given type is a primitive type, then the given value is converted to a string using its toString()
method, and that string is parsed into the correct primitive type using, for instance, Integer.valueOf(String)
to convert the string
into an int
.
If no special constructor exists and the given type is not a primitive type, this method returns the original value.
newType
- the type to convert the value tovalue
- the value to convertjava.lang.NumberFormatException
- if newType is a primitive type, and the string representation of the given value cannot be converted to that typejava.lang.InstantiationException
- if the constructor found with reflection raises itjava.lang.reflect.InvocationTargetException
- if the constructor found with reflection raises itjava.lang.IllegalAccessException
- neverjava.lang.IllegalArgumentException
- neverprotected java.lang.Object[] createWriteMethodArguments(java.lang.reflect.Method method, java.lang.Object value) throws java.lang.IllegalAccessException, java.lang.ClassCastException
convertType(Class,Object)
.method
- the mutator methodvalue
- the value to pass to the mutator methodjava.lang.IllegalAccessException
- if convertType(Class,Object)
raises itjava.lang.IllegalArgumentException
- if any other exception is raised by convertType(Class,Object)
java.lang.ClassCastException
- if an error occurs creating the method argspublic java.util.Iterator<java.util.Map.Entry<java.lang.Object,java.lang.Object>> entryIterator()
public java.util.Set<java.util.Map.Entry<java.lang.Object,java.lang.Object>> entrySet()
Each MapEntry can be set but not removed.
entrySet
in interface java.util.Map<java.lang.Object,java.lang.Object>
entrySet
in class java.util.AbstractMap<java.lang.Object,java.lang.Object>
protected void firePropertyChange(java.lang.Object key, java.lang.Object oldValue, java.lang.Object newValue)
put(Object,Object)
operation. Default implementation does nothing. Override to be notified of property changes in the
bean caused by this map.key
- the name of the property that changedoldValue
- the old value for that propertynewValue
- the new value for that propertypublic java.lang.Object get(java.lang.Object name)
The given name must be a String
and must not be null; otherwise, this method returns null
. If the bean defines a property with the
given name, the value of that property is returned. Otherwise, null
is returned.
Write-only properties will not be matched as the test operates against property read methods.
get
in interface java.util.Map<java.lang.Object,java.lang.Object>
get
in class java.util.AbstractMap<java.lang.Object,java.lang.Object>
name
- the name of the property whose value to returnpublic java.lang.Object getBean()
protected java.lang.reflect.Method getReadMethod(java.lang.Object name)
name
- the name of the propertyString
; null if no such property exists; or the accessor method for that propertypublic java.lang.reflect.Method getReadMethod(java.lang.String name)
name
- the name of the propertypublic java.lang.Class<?> getType(java.lang.String name)
name
- the name of the propertynull
if no such property existsprotected org.apache.commons.collections.Transformer getTypeTransformer(java.lang.Class<?> aType)
aType
- the primitive type whose transformer to returnprotected java.lang.reflect.Method getWriteMethod(java.lang.Object name)
name
- the name of theString
; null if no such property exists; null if the property is read-only; or the
mutator method for that propertypublic java.lang.reflect.Method getWriteMethod(java.lang.String name)
name
- the name of the propertyprivate void initialise()
public java.util.Iterator<java.lang.String> keyIterator()
Write-only properties will not be returned in the iterator.
public java.util.Set<java.lang.Object> keySet()
Write-only properties are not included in the returned set of property names, although it is possible to set their value and to get their type.
keySet
in interface java.util.Map<java.lang.Object,java.lang.Object>
keySet
in class java.util.AbstractMap<java.lang.Object,java.lang.Object>
protected void logInfo(java.lang.Exception ex)
System.out
. Used to display warnings while accessing/mutating the bean.ex
- the exception to logprotected void logWarn(java.lang.Exception ex)
System.err
. Used to display errors while accessing/mutating the bean.ex
- the exception to logpublic java.lang.Object put(java.lang.Object name, java.lang.Object value) throws java.lang.IllegalArgumentException, java.lang.ClassCastException
put
in interface java.util.Map<java.lang.Object,java.lang.Object>
put
in class java.util.AbstractMap<java.lang.Object,java.lang.Object>
name
- the name of the property to setvalue
- the value to set that property tojava.lang.IllegalArgumentException
- if the given name is null; if the given name is not a String
; if the bean doesn't define a property with that
name; or if the bean property with that name is read-onlyjava.lang.ClassCastException
- if an error occurs creating the method argspublic void putAllWriteable(BeanMap map)
map
- the BeanMap whose properties to putprotected void reinitialise()
setBean(Object)
. Does introspection to find properties.public void setBean(java.lang.Object newBean)
newBean
- the new bean to operate onpublic int size()
size
in interface java.util.Map<java.lang.Object,java.lang.Object>
size
in class java.util.AbstractMap<java.lang.Object,java.lang.Object>
public java.lang.String toString()
toString
in class java.util.AbstractMap<java.lang.Object,java.lang.Object>
String
representation of this objectpublic java.util.Iterator<java.lang.Object> valueIterator()
public java.util.Collection<java.lang.Object> values()
values
in interface java.util.Map<java.lang.Object,java.lang.Object>
values
in class java.util.AbstractMap<java.lang.Object,java.lang.Object>
Copyright (c) 2000-2008 - Apache Software Foundation