org.apache.commons.beanutils

Class LazyDynaBean

public class LazyDynaBean extends Object implements DynaBean, Serializable

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() and set() methods for each of these types:

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 will automatically add a property to the 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 type to the 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 accomodate the index being set. ArrayList is the default indexed property that LazyDynaBean uses but this can be easily changed by overriding the newIndexedProperty(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 in the LazyDynaBean, then it will instantiate a new List or Array as specified by the property's type in the DynaClass and automatically grow the List or Array so that it is big enough to accomodate the index being set.

DynaBean myBean = new LazyDynaBean();
MutableDynaClass myClass = (MutableDynaClass)myBean.getDynaClass();
myClass.add("myIndexedProperty", int[].class);
myBean.set("myIndexedProperty", 0, new Integer(10));
myBean.set("myIndexedProperty", 1, new Integer(20));

Setting Mapped Properties

If the property doesn't exist, the LazyDynaBean will automatically add a property with a HashMap type to the DynaClass and instantiate a new HashMap in the DynaBean when the set(name, key, value) method is called. HashMap is the default mapped property that LazyDynaBean uses but this can be easily changed by overriding the newMappedProperty(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 in the LazyDynaBean, then it will instantiate a new Map as specified by the property's type in the DynaClass.

DynaBean myBean = new LazyDynaBean();
MutableDynaClass myClass = (MutableDynaClass)myBean.getDynaClass();
myClass.add("myMappedProperty", TreeMap.class);
myBean.set("myMappedProperty", "myKey", "myValue");

Restricted DynaClass

MutableDynaClass have a facility to restrict the 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.

Author: Niall Pemberton

See Also: LazyDynaClass

Field Summary
protected static BigDecimalBigDecimal_ZERO
protected static BigIntegerBigInteger_ZERO
protected static ByteByte_ZERO
protected static CharacterCharacter_SPACE
protected MutableDynaClassdynaClass
The MutableDynaClass "base class" that this DynaBean is associated with.
protected static DoubleDouble_ZERO
protected static FloatFloat_ZERO
protected static IntegerInteger_ZERO
static Loglogger
Commons Logging
protected static LongLong_ZERO
protected static ShortShort_ZERO
protected Mapvalues
The MutableDynaClass "base class" that this DynaBean is associated with.
Constructor Summary
LazyDynaBean()
Construct a new LazyDynaBean with a LazyDynaClass instance.
LazyDynaBean(String name)
Construct a new LazyDynaBean with a LazyDynaClass instance.
LazyDynaBean(DynaClass dynaClass)
Construct a new DynaBean associated with the specified DynaClass instance - if its not a MutableDynaClass then a new LazyDynaClass is created and the properties copied.
Method Summary
booleancontains(String name, String key)
Does the specified mapped property contain a value for the specified key value?
protected ObjectcreateDynaBeanProperty(String name, Class type)
Create a new Instance of a 'Mapped' Property
protected ObjectcreateIndexedProperty(String name, Class type)
Create a new Instance of an 'Indexed' Property
protected ObjectcreateMappedProperty(String name, Class type)
Create a new Instance of a 'Mapped' Property
protected ObjectcreateNumberProperty(String name, Class type)
Create a new Instance of a 'Primitive' Property
protected ObjectcreateOtherProperty(String name, Class type)
Create a new Instance of a 'Mapped' Property
protected 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 Property
protected ObjectdefaultIndexedProperty(String name)

Creates a new ArrayList for an 'indexed' property which doesn't exist.

This method shouls be overriden if an alternative List or Array implementation is required for 'indexed' properties.

protected MapdefaultMappedProperty(String name)

Creates a new HashMap for a 'mapped' property which doesn't exist.

This method can be overriden if an alternative Map implementation is required for 'mapped' properties.

Objectget(String name)

Return the value of a simple property with the specified name.

N.B. Returns null if there is no property of the specified name.

Objectget(String name, int index)

Return the value of an indexed property with the specified name.

N.B. Returns null if there is no 'indexed' property of the specified name.

Objectget(String name, String key)

Return the value of a mapped property with the specified name.

N.B. Returns null if there is no 'mapped' property of the specified name.

DynaClassgetDynaClass()
Return the DynaClass instance that describes the set of properties available for this DynaBean.
MapgetMap()
Return the Map backing this DynaBean
protected ObjectgrowIndexedProperty(String name, Object indexedProperty, int index)
protected 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.
protected MapnewMap()

Creates a new instance of the Map.

voidremove(String name, String key)
Remove any existing value for the specified key on the specified mapped property.
voidset(String name, Object value)
Set the value of a simple property with the specified name.
voidset(String name, int index, Object value)
Set the value of an indexed property with the specified name.
voidset(String name, String key, Object value)
Set the value of a mapped property with the specified name.
intsize(String name)

Return the size of an indexed or mapped property.

Field Detail

BigDecimal_ZERO

protected static final BigDecimal BigDecimal_ZERO

BigInteger_ZERO

protected static final BigInteger BigInteger_ZERO

Byte_ZERO

protected static final Byte Byte_ZERO

Character_SPACE

protected static final Character Character_SPACE

dynaClass

protected MutableDynaClass dynaClass
The MutableDynaClass "base class" that this DynaBean is associated with.

Double_ZERO

protected static final Double Double_ZERO

Float_ZERO

protected static final Float Float_ZERO

Integer_ZERO

protected static final Integer Integer_ZERO

logger

private static Log logger
Commons Logging

Long_ZERO

protected static final Long Long_ZERO

Short_ZERO

protected static final Short Short_ZERO

values

protected Map values
The MutableDynaClass "base class" that this DynaBean is associated with.

Constructor Detail

LazyDynaBean

public LazyDynaBean()
Construct a new LazyDynaBean with a LazyDynaClass instance.

LazyDynaBean

public LazyDynaBean(String name)
Construct a new LazyDynaBean with a LazyDynaClass instance.

Parameters: name Name of this DynaBean class

LazyDynaBean

public LazyDynaBean(DynaClass dynaClass)
Construct a new DynaBean associated with the specified DynaClass instance - if its not a MutableDynaClass then a new LazyDynaClass is created and the properties copied.

Parameters: dynaClass The DynaClass we are associated with

Method Detail

contains

public boolean contains(String name, String key)
Does the specified mapped property contain a value for the specified key value?

Parameters: name Name of the property to check key Name of the key to check

Throws: IllegalArgumentException if no property name is specified

createDynaBeanProperty

protected Object createDynaBeanProperty(String name, Class type)
Create a new Instance of a 'Mapped' Property

createIndexedProperty

protected Object createIndexedProperty(String name, Class type)
Create a new Instance of an 'Indexed' Property

createMappedProperty

protected Object createMappedProperty(String name, Class type)
Create a new Instance of a 'Mapped' Property

createNumberProperty

protected Object createNumberProperty(String name, Class type)
Create a new Instance of a 'Primitive' Property

createOtherProperty

protected Object createOtherProperty(String name, Class type)
Create a new Instance of a 'Mapped' Property

createPrimitiveProperty

protected Object createPrimitiveProperty(String name, Class type)
Create a new Instance of a 'Primitive' Property

createProperty

protected Object createProperty(String name, Class type)
Create a new Instance of a Property

defaultIndexedProperty

protected Object defaultIndexedProperty(String name)

Creates a new ArrayList for an 'indexed' property which doesn't exist.

This method shouls be overriden if an alternative List or Array implementation is required for 'indexed' properties.

Parameters: name Name of the 'indexed property.

defaultMappedProperty

protected Map defaultMappedProperty(String name)

Creates a new HashMap for a 'mapped' property which doesn't exist.

This method can be overriden if an alternative Map implementation is required for 'mapped' properties.

Parameters: name Name of the 'mapped property.

get

public Object get(String name)

Return the value of a simple property with the specified name.

N.B. Returns null if there is no property of the specified name.

Parameters: name Name of the property whose value is to be retrieved.

Throws: IllegalArgumentException if no property name is specified

get

public Object get(String name, int index)

Return the value of an indexed property with the specified name.

N.B. Returns null if there is no 'indexed' property of the specified name.

Parameters: name Name of the property whose value is to be retrieved index Index of the value to be retrieved

Throws: IllegalArgumentException if the specified property exists, but is not indexed IndexOutOfBoundsException if the specified index is outside the range of the underlying property

get

public Object get(String name, String key)

Return the value of a mapped property with the specified name.

N.B. Returns null if there is no 'mapped' property of the specified name.

Parameters: name Name of the property whose value is to be retrieved key Key of the value to be retrieved

Throws: IllegalArgumentException if the specified property exists, but is not mapped

getDynaClass

public DynaClass getDynaClass()
Return the DynaClass instance that describes the set of properties available for this DynaBean.

getMap

public Map getMap()
Return the Map backing this DynaBean

growIndexedProperty

protected Object growIndexedProperty(String name, Object indexedProperty, int index)

isAssignable

protected boolean isAssignable(Class dest, Class source)
Is an object of the source class assignable to the destination class?

Parameters: dest Destination class source Source class

isDynaProperty

protected boolean isDynaProperty(String name)
Indicates if there is a property with the specified name.

newMap

protected Map newMap()

Creates a new instance of the Map.

remove

public void remove(String name, String key)
Remove any existing value for the specified key on the specified mapped property.

Parameters: name Name of the property for which a value is to be removed key Key of the value to be removed

Throws: IllegalArgumentException if there is no property of the specified name

set

public void set(String name, Object value)
Set the value of a simple property with the specified name.

Parameters: name Name of the property whose value is to be set value 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 restricted ConversionException if the specified value cannot be converted to the type required for this property NullPointerException if an attempt is made to set a primitive property to null

set

public void set(String name, int index, Object value)
Set the value of an indexed property with the specified name.

Parameters: name Name of the property whose value is to be set index Index of the property to be set value Value to which this property is to be set

Throws: ConversionException if the specified value cannot be converted to the type required for this property IllegalArgumentException if there is no property of the specified name IllegalArgumentException if the specified property exists, but is not indexed IndexOutOfBoundsException if the specified index is outside the range of the underlying property

set

public void set(String name, String key, Object value)
Set the value of a mapped property with the specified name.

Parameters: name Name of the property whose value is to be set key Key of the property to be set value Value to which this property is to be set

Throws: ConversionException if the specified value cannot be converted to the type required for this property IllegalArgumentException if there is no property of the specified name IllegalArgumentException if the specified property exists, but is not mapped

size

public int size(String name)

Return the size of an indexed or mapped property.

Parameters: name Name of the property

Throws: IllegalArgumentException if no property name is specified

Copyright (c) 2001-2004 - Apache Software Foundation