Class LazyDynaBean
- All Implemented Interfaces:
DynaBean
- Direct Known Subclasses:
LazyDynaMap
DynaBean which automatically adds properties to the DynaClass and provides Lazy List and Lazy Map features.
DynaBeans deal with three types of properties - simple, indexed and mapped and have the following
get()</code> and <code>set() 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, returns null in this implementation.
Setting Simple Properties
The LazyDynaBean</code> will automatically add a property to the <code>DynaClass
if it doesn't exist when the set(name, value) method is called.
DynaBean myBean = new LazyDynaBean();
myBean.set("myProperty", "myValue");
Setting Indexed Properties
If the property doesn't exist, the LazyDynaBean will automatically add
a property with an ArrayList</code> type to the <code>DynaClass when
the set(name, index, value) method is called.
It will also instantiate a new ArrayList and automatically grow
the List so that it is big enough to accommodate the index being set.
ArrayList is the default indexed property that LazyDynaBean uses but
this can be easily changed by overriding the defaultIndexedProperty(name)
method.
DynaBean myBean = new LazyDynaBean();
myBean.set("myIndexedProperty", 0, "myValue1");
myBean.set("myIndexedProperty", 1, "myValue2");
If the indexed property does exist in the DynaClass but is set to
null</code> in the <code>LazyDynaBean, then it will instantiate a
new List</code> or <code>Array as specified by the property's type
in the DynaClass</code> and automatically <em>grow</em> the <code>List
or Array so 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 LazyDynaBean will automatically add
a property with a HashMap</code> type to the <code>DynaClass and
instantiate a new HashMap in the DynaBean when the
set(name, key, value)</code> method is called. <code>HashMap is the default
mapped property that LazyDynaBean uses but this can be easily changed by overriding
the defaultMappedProperty(name) method.
DynaBean myBean = new LazyDynaBean();
myBean.set("myMappedProperty", "myKey", "myValue");
If the mapped property does exist in the DynaClass but is set to
null</code> in the <code>LazyDynaBean, then it will instantiate a
new Map</code> as specified by the property's type in the <code>DynaClass.
DynaBean myBean = new LazyDynaBean();
MutableDynaClass myClass = (MutableDynaClass)myBean.getDynaClass();
myClass.add("myMappedProperty", TreeMap.class);
myBean.set("myMappedProperty", "myKey", "myValue");
Restricted DynaClass
MutableDynaClass</code> have a facility to <em>restrict</em> the <code>DynaClass so that its properties cannot be modified. If the
MutableDynaClass is restricted then calling any of the set() methods for a property which doesn't exist will result in a
IllegalArgumentException being thrown.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final BigDecimalBigDecimal Zeroprotected static final BigIntegerBigInteger Zeroprotected static final ByteByte Zeroprotected static final CharacterCharacter Spaceprotected static final DoubleDouble Zeroprotected MutableDynaClassTheMutableDynaClass"base class" that this DynaBean is associated with.protected static final FloatFloat Zeroprotected static final IntegerInteger Zeroprotected static final LongLong Zeroprotected static final ShortShort ZeroTheMutableDynaClass"base class" that this DynaBean is associated with. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newLazyDynaBean</code> with a <code>LazyDynaClassinstance.LazyDynaBean(String name) Constructs a newLazyDynaBean</code> with a <code>LazyDynaClassinstance.LazyDynaBean(DynaClass dynaClass) Constructs a newDynaBeanassociated with the specifiedDynaClass</code> instance - if its not a <code>MutableDynaClassthen a newLazyDynaClassis created and the properties copied. -
Method Summary
Modifier and TypeMethodDescriptionbooleanDoes the specified mapped property contain a value for the specified key value?protected ObjectcreateDynaBeanProperty(String name, Class<?> type) Create a new Instance of a 'DynaBean' Property.protected ObjectcreateIndexedProperty(String name, Class<?> type) Create a new Instance of an 'Indexed' Propertyprotected ObjectcreateMappedProperty(String name, Class<?> type) Create a new Instance of a 'Mapped' Propertyprotected ObjectcreateNumberProperty(String name, Class<?> type) Create a new Instance of aNumberProperty.protected ObjectcreateOtherProperty(String name, Class<?> type) Create a new Instance of other Property typesprotected ObjectcreatePrimitiveProperty(String name, Class<?> type) Create a new Instance of a 'Primitive' Property.protected ObjectcreateProperty(String name, Class<?> type) Create a new Instance of a Propertyprotected ObjectdefaultIndexedProperty(String name) Creates a newArrayListfor an 'indexed' property which doesn't exist.defaultMappedProperty(String name) Creates a newHashMapfor a 'mapped' property which doesn't exist.Return the value of a simple property with the specified name.Return the value of an indexed property with the specified name.Return the value of a mapped property with the specified name.Gets theDynaClassinstance that describes the set of properties available for this DynaBean.getMap()Gets a Map representation of this DynaBean.protected ObjectgrowIndexedProperty(String name, Object indexedProperty, int index) Grow the size of an indexed propertyprotected booleanisAssignable(Class<?> dest, Class<?> source) Is an object of the source class assignable to the destination class?protected booleanisDynaProperty(String name) Indicates if there is a property with the specified name.newMap()Creates a new instance of theMap.voidRemove any existing value for the specified key on the specified mapped property.voidSets the value of an indexed property with the specified name.voidSets the value of a simple property with the specified name.voidSets the value of a mapped property with the specified name.intReturn the size of an indexed or mapped property.
-
Field Details
-
BigInteger_ZERO
BigInteger Zero -
BigDecimal_ZERO
BigDecimal Zero -
Character_SPACE
Character Space -
Byte_ZERO
Byte Zero -
Short_ZERO
Short Zero -
Integer_ZERO
Integer Zero -
Long_ZERO
Long Zero -
Float_ZERO
Float Zero -
Double_ZERO
Double Zero -
values
TheMutableDynaClass"base class" that this DynaBean is associated with. -
dynaClass
TheMutableDynaClass"base class" that this DynaBean is associated with.
-
-
Constructor Details
-
LazyDynaBean
public LazyDynaBean()Constructs a newLazyDynaBean</code> with a <code>LazyDynaClassinstance. -
LazyDynaBean
Constructs a newDynaBeanassociated with the specifiedDynaClass</code> instance - if its not a <code>MutableDynaClassthen a newLazyDynaClassis created and the properties copied.- Parameters:
dynaClass- The DynaClass we are associated with
-
LazyDynaBean
Constructs a newLazyDynaBean</code> with a <code>LazyDynaClassinstance.- Parameters:
name- Name of this DynaBean class
-
-
Method Details
-
contains
Does the specified mapped property contain a value for the specified key value?- Specified by:
containsin interfaceDynaBean- Parameters:
name- Name of the property to checkkey- Name of the key to check- Returns:
trueif the mapped property contains a value for the specified key, otherwisefalse- Throws:
IllegalArgumentException- if no property name is specified
-
createDynaBeanProperty
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
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
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
Create a new Instance of aNumberProperty.- Parameters:
name- The name of the propertytype- The class of the property- Returns:
- The new value
-
createOtherProperty
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
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
Create a new Instance of a Property- Parameters:
name- The name of the propertytype- The class of the property- Returns:
- The new value
-
defaultIndexedProperty
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
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
Return the value of a simple property with the specified name.
N.B. Returns
nullif there is no property of the specified name.- Specified by:
getin interfaceDynaBean- Parameters:
name- Name of the property whose value is to be retrieved.- Returns:
- The property's value
- Throws:
IllegalArgumentException- if no property name is specified
-
get
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:
IllegalArgumentException- if the specified property exists, but is not indexedIndexOutOfBoundsException- if the specified index is outside the range of the underlying property
-
get
Return the value of a mapped property with the specified name.
N.B. Returns
nullif there is no 'mapped' property of the specified name.- Specified by:
getin interfaceDynaBean- Parameters:
name- Name of the property whose value is to be retrievedkey- Key of the value to be retrieved- Returns:
- The mapped property's value
- Throws:
IllegalArgumentException- if the specified property exists, but is not mapped
-
getDynaClass
Gets theDynaClassinstance that describes the set of properties available for this DynaBean.- Specified by:
getDynaClassin interfaceDynaBean- Returns:
- The associated DynaClass
-
getMap
Gets a Map representation of this DynaBean.
This, for example, could be used in JSTL in the following way to access a DynaBean'sfooProperty:${myDynaBean.<strong>map</strong>.fooProperty}
- Returns:
- a Map representation of this DynaBean
-
growIndexedProperty
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
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
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
-
newMap
Creates a new instance of the
Map.- Returns:
- a new Map instance
-
remove
Remove any existing value for the specified key on the specified mapped property.- Specified by:
removein interfaceDynaBean- Parameters:
name- Name of the property for which a value is to be removedkey- Key of the value to be removed- Throws:
IllegalArgumentException- if there is no property of the specified name
-
set
Sets 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 propertyIllegalArgumentException- if there is no property of the specified nameIllegalArgumentException- if the specified property exists, but is not indexedIndexOutOfBoundsException- if the specified index is outside the range of the underlying property
-
set
Sets 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:
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 propertyNullPointerException- if an attempt is made to set a primitive property to null
-
set
Sets 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 propertyIllegalArgumentException- if there is no property of the specified nameIllegalArgumentException- if the specified property exists, but is not mapped
-
size
Return the size of an indexed or mapped property.
- Parameters:
name- Name of the property- Returns:
- The indexed or mapped property size
- Throws:
IllegalArgumentException- if no property name is specified
-