Class LazyDynaList
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<E>
-
- java.util.ArrayList<java.lang.Object>
-
- org.apache.commons.beanutils.LazyDynaList
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,java.lang.Iterable<java.lang.Object>
,java.util.Collection<java.lang.Object>
,java.util.List<java.lang.Object>
,java.util.RandomAccess
public class LazyDynaList extends java.util.ArrayList<java.lang.Object>
Lazy DynaBean List.
There are two main purposes for this class:
- To provide Lazy List behavior - automatically
growing and populating the
List
with eitherDynaBean
,java.util.Map
or POJO Beans. - To provide a straight forward way of putting a Collection or Array into the lazy list and a straight forward way to get it out again at the end.
All elements added to the List are stored as
DynaBean
's:java.util.Map
elements are "wrapped" in aLazyDynaMap
.- POJO Bean elements are "wrapped" in a
WrapDynaBean.
DynaBean
's are stored un-changed.
toArray()
The
toArray()
method returns an array of the elements of the appropriate type. If theLazyDynaList
is populated withjava.util.Map
objects aMap[]
array is returned. If the list is populated with POJO Beans an appropriate array of the POJO Beans is returned. Otherwise aDynaBean[]
array is returned.toDynaBeanArray()
The
toDynaBeanArray()
method returns aDynaBean[]
array of the elements in the List.N.B.All the elements in the List must be the same type. If the
DynaClass
orClass
of theLazyDynaList
's elements is not specified, then it will be automatically set to the type of the first element populated.Example 1
If you have an array of
java.util.Map[]
- you can put that into aLazyDynaList
.TreeMap[] myArray = .... // your Map[] List lazyList = new LazyDynaList(myArray);
New elements of the appropriate Map type are automatically populated:
// get(index) automatically grows the list DynaBean newElement = (DynaBean)lazyList.get(lazyList.size()); newElement.put("someProperty", "someValue");
Once you've finished you can get back an Array of the elements of the appropriate type:
// Retrieve the array from the list TreeMap[] myArray = (TreeMap[])lazyList.toArray());
Example 2
Alternatively you can create an empty List and specify the Class for List's elements. The LazyDynaList uses the Class to automatically populate elements:
// for example For Maps List lazyList = new LazyDynaList(TreeMap.class); // for example For POJO Beans List lazyList = new LazyDynaList(MyPojo.class); // for example For DynaBeans List lazyList = new LazyDynaList(MyDynaBean.class);
Example 3
Alternatively you can create an empty List and specify the DynaClass for List's elements. The LazyDynaList uses the DynaClass to automatically populate elements:
// for example For Maps DynaClass dynaClass = new LazyDynaMap(new HashMap()); List lazyList = new LazyDynaList(dynaClass); // for example For POJO Beans DynaClass dynaClass = (new WrapDynaBean(myPojo)).getDynaClass(); List lazyList = new LazyDynaList(dynaClass); // for example For DynaBeans DynaClass dynaClass = new BasicDynaClass(properties); List lazyList = new LazyDynaList(dynaClass);
N.B. You may wonder why control the type using a
DynaClass
rather than theClass
as in the previous example - the reason is that someDynaBean
implementations don't have a default empty constructor and therefore need to be instantiated using theDynaClass.newInstance()
method.Example 4
A slight variation - set the element type using either the
setElementType(Class)
method or thesetElementDynaClass(DynaClass)
method - then populate with the normaljava.util.List
methods(i.e.add()
,addAll()
orset()
).// Create a new LazyDynaList (100 element capacity) LazyDynaList lazyList = new LazyDynaList(100); // Either Set the element type... lazyList.setElementType(TreeMap.class); // ...or the element DynaClass... lazyList.setElementDynaClass(new MyCustomDynaClass()); // Populate from a collection lazyList.addAll(myCollection);
- Since:
- 1.8.0
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.Class<?>
elementDynaBeanType
The DynaBean type of the List's elements.private DynaClass
elementDynaClass
The DynaClass of the List's elements.private java.lang.Class<?>
elementType
The type of the List's elements.private static long
serialVersionUID
private WrapDynaClass
wrapDynaClass
The WrapDynaClass if the List's contains POJO Bean elements.
-
Constructor Summary
Constructors Constructor Description LazyDynaList()
Constructs a new instance.LazyDynaList(int capacity)
Construct a LazyDynaList with the specified capacity.LazyDynaList(java.lang.Class<?> elementType)
Construct a LazyDynaList with a specified type for its elements.LazyDynaList(java.lang.Object[] array)
Construct a LazyDynaList populated with the elements of an Array.LazyDynaList(java.util.Collection<?> collection)
Construct a LazyDynaList populated with the elements of a Collection.LazyDynaList(DynaClass elementDynaClass)
Construct a LazyDynaList with a specified DynaClass for its elements.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(int index, java.lang.Object element)
Insert an element at the specified index position.boolean
add(java.lang.Object element)
Add an element to the List.boolean
addAll(int index, java.util.Collection<?> collection)
Insert all the elements from a Collection into the list at a specified position.boolean
addAll(java.util.Collection<?> collection)
Add all the elements from a Collection to the list.private LazyDynaMap
createDynaBeanForMapProperty(java.lang.Object value)
Creates a newLazyDynaMap
object for the given property value.java.lang.Object
get(int index)
Return the element at the specified position.private DynaClass
getDynaClass()
Return the DynaClass.private void
growList(int requiredSize)
Automatically grown the List to the appropriate size, populating with DynaBeans.java.lang.Object
set(int index, java.lang.Object element)
Set the element at the specified position.void
setElementDynaClass(DynaClass elementDynaClass)
Set the element Type and DynaClass.void
setElementType(java.lang.Class<?> elementType)
Set the element Type and DynaClass.java.lang.Object[]
toArray()
Converts the List to an Array.<T> T[]
toArray(T[] model)
Converts the List to an Array of the specified type.DynaBean[]
toDynaBeanArray()
Converts the List to an DynaBean Array.private DynaBean
transform(java.lang.Object element)
Transform the element into a DynaBean:-
Methods inherited from class java.util.ArrayList
clear, clone, contains, ensureCapacity, equals, forEach, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeIf, removeRange, replaceAll, retainAll, size, sort, spliterator, subList, trimToSize
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
elementDynaClass
private DynaClass elementDynaClass
The DynaClass of the List's elements.
-
wrapDynaClass
private transient WrapDynaClass wrapDynaClass
The WrapDynaClass if the List's contains POJO Bean elements. WrapDynaClass isn't serlializable, which is why its stored separately in a transient instance variable.
-
elementType
private java.lang.Class<?> elementType
The type of the List's elements.
-
elementDynaBeanType
private java.lang.Class<?> elementDynaBeanType
The DynaBean type of the List's elements.
-
-
Constructor Detail
-
LazyDynaList
public LazyDynaList()
Constructs a new instance.
-
LazyDynaList
public LazyDynaList(java.lang.Class<?> elementType)
Construct a LazyDynaList with a specified type for its elements.- Parameters:
elementType
- The Type of the List's elements.
-
LazyDynaList
public LazyDynaList(java.util.Collection<?> collection)
Construct a LazyDynaList populated with the elements of a Collection.- Parameters:
collection
- The Collection to populate the List from.
-
LazyDynaList
public LazyDynaList(DynaClass elementDynaClass)
Construct a LazyDynaList with a specified DynaClass for its elements.- Parameters:
elementDynaClass
- The DynaClass of the List's elements.
-
LazyDynaList
public LazyDynaList(int capacity)
Construct a LazyDynaList with the specified capacity.- Parameters:
capacity
- The initial capacity of the list.
-
LazyDynaList
public LazyDynaList(java.lang.Object[] array)
Construct a LazyDynaList populated with the elements of an Array.- Parameters:
array
- The Array to populate the List from.
-
-
Method Detail
-
add
public void add(int index, java.lang.Object element)
Insert an element at the specified index position.
If the index position is greater than the current size of the List, then the List is automatically grown to the appropriate size.
- Specified by:
add
in interfacejava.util.List<java.lang.Object>
- Overrides:
add
in classjava.util.ArrayList<java.lang.Object>
- Parameters:
index
- The index position to insert the new element.element
- The new element to add.
-
add
public boolean add(java.lang.Object element)
Add an element to the List.
- Specified by:
add
in interfacejava.util.Collection<java.lang.Object>
- Specified by:
add
in interfacejava.util.List<java.lang.Object>
- Overrides:
add
in classjava.util.ArrayList<java.lang.Object>
- Parameters:
element
- The new element to add.- Returns:
- true.
-
addAll
public boolean addAll(java.util.Collection<?> collection)
Add all the elements from a Collection to the list.
- Specified by:
addAll
in interfacejava.util.Collection<java.lang.Object>
- Specified by:
addAll
in interfacejava.util.List<java.lang.Object>
- Overrides:
addAll
in classjava.util.ArrayList<java.lang.Object>
- Parameters:
collection
- The Collection of new elements.- Returns:
- true if elements were added.
-
addAll
public boolean addAll(int index, java.util.Collection<?> collection)
Insert all the elements from a Collection into the list at a specified position.
If the index position is greater than the current size of the List, then the List is automatically grown to the appropriate size.
- Specified by:
addAll
in interfacejava.util.List<java.lang.Object>
- Overrides:
addAll
in classjava.util.ArrayList<java.lang.Object>
- Parameters:
collection
- The Collection of new elements.index
- The index position to insert the new elements at.- Returns:
- true if elements were added.
-
createDynaBeanForMapProperty
private LazyDynaMap createDynaBeanForMapProperty(java.lang.Object value)
Creates a newLazyDynaMap
object for the given property value.- Parameters:
value
- the property value- Returns:
- the newly created
LazyDynaMap
-
get
public java.lang.Object get(int index)
Return the element at the specified position.
If the position requested is greater than the current size of the List, then the List is automatically grown (and populated) to the appropriate size.
- Specified by:
get
in interfacejava.util.List<java.lang.Object>
- Overrides:
get
in classjava.util.ArrayList<java.lang.Object>
- Parameters:
index
- The index position to insert the new elements at.- Returns:
- The element at the specified position.
-
getDynaClass
private DynaClass getDynaClass()
Return the DynaClass.
-
growList
private void growList(int requiredSize)
Automatically grown the List to the appropriate size, populating with DynaBeans.
- Parameters:
requiredSize
- the required size of the List.
-
set
public java.lang.Object set(int index, java.lang.Object element)
Set the element at the specified position.
If the position requested is greater than the current size of the List, then the List is automatically grown (and populated) to the appropriate size.
- Specified by:
set
in interfacejava.util.List<java.lang.Object>
- Overrides:
set
in classjava.util.ArrayList<java.lang.Object>
- Parameters:
index
- The index position to insert the new element at.element
- The new element.- Returns:
- The new element.
-
setElementDynaClass
public void setElementDynaClass(DynaClass elementDynaClass)
Set the element Type and DynaClass.
- Parameters:
elementDynaClass
- The DynaClass of the elements.- Throws:
java.lang.IllegalArgumentException
- if the List already contains elements or the DynaClass is null.
-
setElementType
public void setElementType(java.lang.Class<?> elementType)
Set the element Type and DynaClass.
- Parameters:
elementType
- The type of the elements.- Throws:
java.lang.IllegalArgumentException
- if the List already contains elements or the DynaClass is null.
-
toArray
public java.lang.Object[] toArray()
Converts the List to an Array.
The type of Array created depends on the contents of the List:
- If the List contains only LazyDynaMap type elements then a java.util.Map[] array will be created.
- If the List contains only elements which are "wrapped" DynaBeans then an Object[] of the most suitable type will be created.
- ...otherwise a DynaBean[] will be created.
- Specified by:
toArray
in interfacejava.util.Collection<java.lang.Object>
- Specified by:
toArray
in interfacejava.util.List<java.lang.Object>
- Overrides:
toArray
in classjava.util.ArrayList<java.lang.Object>
- Returns:
- An Array of the elements in this List.
-
toArray
public <T> T[] toArray(T[] model)
Converts the List to an Array of the specified type.
- Specified by:
toArray
in interfacejava.util.Collection<java.lang.Object>
- Specified by:
toArray
in interfacejava.util.List<java.lang.Object>
- Overrides:
toArray
in classjava.util.ArrayList<java.lang.Object>
- Type Parameters:
T
- The type of the array elements- Parameters:
model
- The model for the type of array to return- Returns:
- An Array of the elements in this List.
-
toDynaBeanArray
public DynaBean[] toDynaBeanArray()
Converts the List to an DynaBean Array.
- Returns:
- A DynaBean[] of the elements in this List.
-
transform
private DynaBean transform(java.lang.Object element)
Transform the element into a DynaBean:
- Map elements are turned into LazyDynaMap's.
- POJO Beans are "wrapped" in a WrapDynaBean.
- DynaBeans are unchanged.
- Parameters:
element
- The element to transformed.The
- DynaBean to store in the List.
-
-