Class LazyDynaBean
- java.lang.Object
-
- org.apache.commons.beanutils.LazyDynaBean
-
- All Implemented Interfaces:
java.io.Serializable,DynaBean
- Direct Known Subclasses:
LazyDynaMap
public class LazyDynaBean extends java.lang.Object implements DynaBean, java.io.Serializable
DynaBean which automatically adds properties to the
DynaClassand provides Lazy List and Lazy Map features.DynaBeans deal with three types of properties - simple, indexed and mapped and have the following
get()andset()methods for each of these types:- Simple property methods -
get(name)andset(name, value) - Indexed property methods -
get(name, index)andset(name, index, value) - Mapped property methods -
get(name, key)andset(name, key, value)
Getting Property Values
Calling any of the
get()methods, for a property which doesn't exist, returnsnullin this implementation.Setting Simple Properties
The
LazyDynaBeanwill automatically add a property to theDynaClassif it doesn't exist when theset(name, value)method is called.DynaBean myBean = new LazyDynaBean(); myBean.set("myProperty", "myValue");Setting Indexed Properties
If the property doesn't exist, the
LazyDynaBeanwill automatically add a property with anArrayListtype to theDynaClasswhen theset(name, index, value)method is called. It will also instantiate a newArrayListand automatically grow theListso that it is big enough to accommodate the index being set.ArrayListis the default indexed property that LazyDynaBean uses but this can be easily changed by overriding thedefaultIndexedProperty(name)method.DynaBean myBean = new LazyDynaBean() myBean.set("myIndexedProperty", 0, "myValue1"); myBean.set("myIndexedProperty", 1, "myValue2");If the indexed property does exist in the
DynaClassbut is set tonullin theLazyDynaBean, then it will instantiate a newListorArrayas specified by the property's type in theDynaClassand automatically grow theListorArrayso that it is big enough to accommodate the index being set.DynaBean myBean = new LazyDynaBean(); MutableDynaClass myClass = (MutableDynaClass)myBean.getDynaClass(); myClass.add("myIndexedProperty", int[].class); myBean.set("myIndexedProperty", 0, Integer.valueOf(10)); myBean.set("myIndexedProperty", 1, Integer.valueOf(20));Setting Mapped Properties
If the property doesn't exist, the
LazyDynaBeanwill automatically add a property with aHashMaptype to theDynaClassand instantiate a newHashMapin the DynaBean when theset(name, key, value)method is called.HashMapis the default mapped property that LazyDynaBean uses but this can be easily changed by overriding thedefaultMappedProperty(name)method.DynaBean myBean = new LazyDynaBean(); myBean.set("myMappedProperty", "myKey", "myValue");If the mapped property does exist in the
DynaClassbut is set tonullin theLazyDynaBean, then it will instantiate a newMapas specified by the property's type in theDynaClass.DynaBean myBean = new LazyDynaBean(); MutableDynaClass myClass = (MutableDynaClass)myBean.getDynaClass(); myClass.add("myMappedProperty", TreeMap.class); myBean.set("myMappedProperty", "myKey", "myValue");Restricted DynaClass
MutableDynaClasshave a facility to restrict theDynaClassso that its properties cannot be modified. If theMutableDynaClassis restricted then calling any of theset()methods for a property which doesn't exist will result in aIllegalArgumentExceptionbeing thrown.- See Also:
LazyDynaClass, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected static java.math.BigDecimalBigDecimal_ZEROBigDecimal Zeroprotected static java.math.BigIntegerBigInteger_ZEROBigInteger Zeroprotected static java.lang.ByteByte_ZEROByte Zeroprotected static java.lang.CharacterCharacter_SPACECharacter Spaceprotected static java.lang.DoubleDouble_ZERODouble Zeroprotected MutableDynaClassdynaClassTheMutableDynaClass"base class" that this DynaBean is associated with.protected static java.lang.FloatFloat_ZEROFloat Zeroprotected static java.lang.IntegerInteger_ZEROInteger Zeroprivate org.apache.commons.logging.LogloggerCommons Loggingprotected static java.lang.LongLong_ZEROLong Zeroprivate java.util.Map<java.lang.String,java.lang.Object>mapDecoratorMap decorator for this DynaBeanprivate static longserialVersionUIDprotected static java.lang.ShortShort_ZEROShort Zeroprotected java.util.Map<java.lang.String,java.lang.Object>valuesTheMutableDynaClass"base class" that this DynaBean is associated with.
-
Constructor Summary
Constructors Constructor Description LazyDynaBean()Construct a newLazyDynaBeanwith aLazyDynaClassinstance.LazyDynaBean(java.lang.String name)Construct a newLazyDynaBeanwith aLazyDynaClassinstance.LazyDynaBean(DynaClass dynaClass)Construct a newDynaBeanassociated with the specifiedDynaClassinstance - if its not aMutableDynaClassthen a newLazyDynaClassis created and the properties copied.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancontains(java.lang.String name, java.lang.String key)Does the specified mapped property contain a value for the specified key value?protected java.lang.ObjectcreateDynaBeanProperty(java.lang.String name, java.lang.Class<?> type)Create a new Instance of a 'DynaBean' Property.protected java.lang.ObjectcreateIndexedProperty(java.lang.String name, java.lang.Class<?> type)Create a new Instance of an 'Indexed' Propertyprotected java.lang.ObjectcreateMappedProperty(java.lang.String name, java.lang.Class<?> type)Create a new Instance of a 'Mapped' Propertyprotected java.lang.ObjectcreateNumberProperty(java.lang.String name, java.lang.Class<?> type)Create a new Instance of ajava.lang.NumberProperty.protected java.lang.ObjectcreateOtherProperty(java.lang.String name, java.lang.Class<?> type)Create a new Instance of other Property typesprotected java.lang.ObjectcreatePrimitiveProperty(java.lang.String name, java.lang.Class<?> type)Create a new Instance of a 'Primitive' Property.protected java.lang.ObjectcreateProperty(java.lang.String name, java.lang.Class<?> type)Create a new Instance of a Propertyprotected java.lang.ObjectdefaultIndexedProperty(java.lang.String name)Creates a newArrayListfor an 'indexed' property which doesn't exist.protected java.util.Map<java.lang.String,java.lang.Object>defaultMappedProperty(java.lang.String name)Creates a newHashMapfor a 'mapped' property which doesn't exist.java.lang.Objectget(java.lang.String name)Return the value of a simple property with the specified name.java.lang.Objectget(java.lang.String name, int index)Return the value of an indexed property with the specified name.java.lang.Objectget(java.lang.String name, java.lang.String key)Return the value of a mapped property with the specified name.DynaClassgetDynaClass()Return theDynaClassinstance that describes the set of properties available for this DynaBean.java.util.Map<java.lang.String,java.lang.Object>getMap()Return a Map representation of this DynaBean.protected java.lang.ObjectgrowIndexedProperty(java.lang.String name, java.lang.Object indexedProperty, int index)Grow the size of an indexed property.protected booleanisAssignable(java.lang.Class<?> dest, java.lang.Class<?> source)Is an object of the source class assignable to the destination class?protected booleanisDynaProperty(java.lang.String name)Indicates if there is a property with the specified name.private org.apache.commons.logging.Loglogger()Returns theLog.protected java.util.Map<java.lang.String,java.lang.Object>newMap()Creates a new instance of theMap.voidremove(java.lang.String name, java.lang.String key)Remove any existing value for the specified key on the specified mapped property.voidset(java.lang.String name, int index, java.lang.Object value)Set the value of an indexed property with the specified name.voidset(java.lang.String name, java.lang.Object value)Set the value of a simple property with the specified name.voidset(java.lang.String name, java.lang.String key, java.lang.Object value)Set the value of a mapped property with the specified name.intsize(java.lang.String name)Return the size of an indexed or mapped property.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
BigInteger_ZERO
protected static final java.math.BigInteger BigInteger_ZERO
BigInteger Zero
-
BigDecimal_ZERO
protected static final java.math.BigDecimal BigDecimal_ZERO
BigDecimal Zero
-
Character_SPACE
protected static final java.lang.Character Character_SPACE
Character Space
-
Byte_ZERO
protected static final java.lang.Byte Byte_ZERO
Byte Zero
-
Short_ZERO
protected static final java.lang.Short Short_ZERO
Short Zero
-
Integer_ZERO
protected static final java.lang.Integer Integer_ZERO
Integer Zero
-
Long_ZERO
protected static final java.lang.Long Long_ZERO
Long Zero
-
Float_ZERO
protected static final java.lang.Float Float_ZERO
Float Zero
-
Double_ZERO
protected static final java.lang.Double Double_ZERO
Double Zero
-
logger
private transient org.apache.commons.logging.Log logger
Commons Logging
-
values
protected java.util.Map<java.lang.String,java.lang.Object> values
TheMutableDynaClass"base class" that this DynaBean is associated with.
-
mapDecorator
private transient java.util.Map<java.lang.String,java.lang.Object> mapDecorator
Map decorator for this DynaBean
-
dynaClass
protected MutableDynaClass dynaClass
TheMutableDynaClass"base class" that this DynaBean is associated with.
-
-
Constructor Detail
-
LazyDynaBean
public LazyDynaBean()
Construct a newLazyDynaBeanwith aLazyDynaClassinstance.
-
LazyDynaBean
public LazyDynaBean(DynaClass dynaClass)
Construct a newDynaBeanassociated with the specifiedDynaClassinstance - if its not aMutableDynaClassthen a newLazyDynaClassis created and the properties copied.- Parameters:
dynaClass- The DynaClass we are associated with
-
LazyDynaBean
public LazyDynaBean(java.lang.String name)
Construct a newLazyDynaBeanwith aLazyDynaClassinstance.- Parameters:
name- Name of this DynaBean class
-
-
Method Detail
-
contains
public boolean contains(java.lang.String name, java.lang.String key)Does the specified mapped property contain a value for the specified key value?
-
createDynaBeanProperty
protected java.lang.Object createDynaBeanProperty(java.lang.String name, java.lang.Class<?> type)Create a new Instance of a 'DynaBean' Property.- Parameters:
name- The name of the propertytype- The class of the property- Returns:
- The new value
-
createIndexedProperty
protected java.lang.Object createIndexedProperty(java.lang.String name, java.lang.Class<?> type)Create a new Instance of an 'Indexed' Property- Parameters:
name- The name of the propertytype- The class of the property- Returns:
- The new value
-
createMappedProperty
protected java.lang.Object createMappedProperty(java.lang.String name, java.lang.Class<?> type)Create a new Instance of a 'Mapped' Property- Parameters:
name- The name of the propertytype- The class of the property- Returns:
- The new value
-
createNumberProperty
protected java.lang.Object createNumberProperty(java.lang.String name, java.lang.Class<?> type)Create a new Instance of ajava.lang.NumberProperty.- Parameters:
name- The name of the propertytype- The class of the property- Returns:
- The new value
-
createOtherProperty
protected java.lang.Object createOtherProperty(java.lang.String name, java.lang.Class<?> type)Create a new Instance of other Property types- Parameters:
name- The name of the propertytype- The class of the property- Returns:
- The new value
-
createPrimitiveProperty
protected java.lang.Object createPrimitiveProperty(java.lang.String name, java.lang.Class<?> type)Create a new Instance of a 'Primitive' Property.- Parameters:
name- The name of the propertytype- The class of the property- Returns:
- The new value
-
createProperty
protected java.lang.Object createProperty(java.lang.String name, java.lang.Class<?> type)Create a new Instance of a Property- Parameters:
name- The name of the propertytype- The class of the property- Returns:
- The new value
-
defaultIndexedProperty
protected java.lang.Object defaultIndexedProperty(java.lang.String name)
Creates a new
ArrayListfor an 'indexed' property which doesn't exist.This method should be overridden if an alternative
ListorArrayimplementation is required for 'indexed' properties.- Parameters:
name- Name of the 'indexed property.- Returns:
- The default value for an indexed property (java.util.ArrayList)
-
defaultMappedProperty
protected java.util.Map<java.lang.String,java.lang.Object> defaultMappedProperty(java.lang.String name)
Creates a new
HashMapfor a 'mapped' property which doesn't exist.This method can be overridden if an alternative
Mapimplementation is required for 'mapped' properties.- Parameters:
name- Name of the 'mapped property.- Returns:
- The default value for a mapped property (java.util.HashMap)
-
get
public java.lang.Object get(java.lang.String name)
Return the value of a simple property with the specified name.
N.B. Returns
nullif there is no property of the specified name.
-
get
public java.lang.Object get(java.lang.String name, int index)Return the value of an indexed property with the specified name.
N.B. Returns
nullif there is no 'indexed' property of the specified name.- Specified by:
getin interfaceDynaBean- Parameters:
name- Name of the property whose value is to be retrievedindex- Index of the value to be retrieved- Returns:
- The indexed property's value
- Throws:
java.lang.IllegalArgumentException- if the specified property exists, but is not indexedjava.lang.IndexOutOfBoundsException- if the specified index is outside the range of the underlying property
-
get
public java.lang.Object get(java.lang.String name, java.lang.String key)Return the value of a mapped property with the specified name.
N.B. Returns
nullif there is no 'mapped' property of the specified name.
-
getDynaClass
public DynaClass getDynaClass()
Return theDynaClassinstance that describes the set of properties available for this DynaBean.- Specified by:
getDynaClassin interfaceDynaBean- Returns:
- The associated DynaClass
-
getMap
public java.util.Map<java.lang.String,java.lang.Object> getMap()
Return a Map representation of this DynaBean.This, for example, could be used in JSTL in the following way to access a DynaBean's
fooProperty:${myDynaBean.map.fooProperty}
- Returns:
- a Map representation of this DynaBean
-
growIndexedProperty
protected java.lang.Object growIndexedProperty(java.lang.String name, java.lang.Object indexedProperty, int index)Grow the size of an indexed property.- Parameters:
name- The name of the propertyindexedProperty- The current property valueindex- The indexed value to grow the property to (i.e. one less than the required size)- Returns:
- The new property value (grown to the appropriate size)
-
isAssignable
protected boolean isAssignable(java.lang.Class<?> dest, java.lang.Class<?> source)Is an object of the source class assignable to the destination class?- Parameters:
dest- Destination classsource- Source class- Returns:
trueif the source class is assignable to the destination class, otherwisefalse
-
isDynaProperty
protected boolean isDynaProperty(java.lang.String name)
Indicates if there is a property with the specified name.- Parameters:
name- The name of the property to check- Returns:
trueif there is a property of the specified name, otherwisefalse
-
logger
private org.apache.commons.logging.Log logger()
Returns the
Log.
-
newMap
protected java.util.Map<java.lang.String,java.lang.Object> newMap()
Creates a new instance of the
Map.- Returns:
- a new Map instance
-
remove
public void remove(java.lang.String name, java.lang.String key)Remove any existing value for the specified key on the specified mapped property.
-
set
public void set(java.lang.String name, int index, java.lang.Object value)Set the value of an indexed property with the specified name.- Specified by:
setin interfaceDynaBean- Parameters:
name- Name of the property whose value is to be setindex- Index of the property to be setvalue- Value to which this property is to be set- Throws:
ConversionException- if the specified value cannot be converted to the type required for this propertyjava.lang.IllegalArgumentException- if there is no property of the specified namejava.lang.IllegalArgumentException- if the specified property exists, but is not indexedjava.lang.IndexOutOfBoundsException- if the specified index is outside the range of the underlying property
-
set
public void set(java.lang.String name, java.lang.Object value)Set the value of a simple property with the specified name.- Specified by:
setin interfaceDynaBean- Parameters:
name- Name of the property whose value is to be setvalue- Value to which this property is to be set- Throws:
java.lang.IllegalArgumentException- if this is not an existing property name for our DynaClass and the MutableDynaClass is restrictedConversionException- if the specified value cannot be converted to the type required for this propertyjava.lang.NullPointerException- if an attempt is made to set a primitive property to null
-
set
public void set(java.lang.String name, java.lang.String key, java.lang.Object value)Set the value of a mapped property with the specified name.- Specified by:
setin interfaceDynaBean- Parameters:
name- Name of the property whose value is to be setkey- Key of the property to be setvalue- Value to which this property is to be set- Throws:
ConversionException- if the specified value cannot be converted to the type required for this propertyjava.lang.IllegalArgumentException- if there is no property of the specified namejava.lang.IllegalArgumentException- if the specified property exists, but is not mapped
-
size
public int size(java.lang.String name)
Return the size of an indexed or mapped property.
- Parameters:
name- Name of the property- Returns:
- The indexed or mapped property size
- Throws:
java.lang.IllegalArgumentException- if no property name is specified
-
-