java.lang.Object | ||||
↳ | java.util.AbstractCollection | |||
↳ | java.util.AbstractList | |||
↳ | java.util.ArrayList | |||
↳ | org.apache.commons.beanutils.LazyDynaList |
There are two main purposes for this class:
List
with either DynaBean
, java.util.Map
or POJO Beans.All elements added to the List are stored as DynaBean
's:
java.util.Map
elements are "wrapped" in a LazyDynaMap
.
WrapDynaBean.
DynaBean
's are stored un-changed.
toArray()
The toArray()
method returns an array of the
elements of the appropriate type. If the LazyDynaList
is populated with java.util.Map
objects a
Map[]
array is returned.
If the list is populated with POJO Beans an appropriate
array of the POJO Beans is returned. Otherwise a DynaBean[]
array is returned.
toDynaBeanArray()
The toDynaBeanArray()
method returns a
DynaBean[]
array of the elements in the List.
N.B.All the elements in the List must be the
same type. If the DynaClass
or Class
of the LazyDynaList
's elements is
not specified, then it will be automatically set to the type
of the first element populated.
If you have an array of java.util.Map[]
- you can put that into
a LazyDynaList
.
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());
Alternatively you can create an empty List and specify the Class for List's elements. The LazyDynaList uses the Class to automatically populate elements:
// e.g. For Maps
List lazyList = new LazyDynaList(TreeMap.class);
// e.g. For POJO Beans
List lazyList = new LazyDynaList(MyPojo.class);
// e.g. For DynaBeans
List lazyList = new LazyDynaList(MyDynaBean.class);
Alternatively you can create an empty List and specify the DynaClass for List's elements. The LazyDynaList uses the DynaClass to automatically populate elements:
// e.g. For Maps
DynaClass dynaClass = new LazyDynaMap(new HashMap());
List lazyList = new LazyDynaList(dynaClass);
// e.g. For POJO Beans
DynaClass dynaClass = (new WrapDynaBean(myPojo)).getDynaClass();
List lazyList = new LazyDynaList(dynaClass);
// e.g. 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 the Class
as in the previous example - the reason is that some DynaBean
implementations don't have a default empty constructor and
therefore need to be instantiated using the DynaClass.newInstance()
method.
A slight variation - set the element type using either
the setElementType(Class)
method or the
setElementDynaClass(DynaClass)
method - then populate
with the normal java.util.List
methods(i.e.
add()
, addAll()
or set()
).
// 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);
[Expand]
Inherited Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.util.AbstractList
|
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Default Constructor.
| |||||||||||
Construct a LazyDynaList with the
specified capacity.
| |||||||||||
Construct a LazyDynaList with a
specified DynaClass for its elements.
| |||||||||||
Construct a LazyDynaList with a
specified type for its elements.
| |||||||||||
Construct a LazyDynaList populated with the
elements of a Collection.
| |||||||||||
Construct a LazyDynaList populated with the
elements of an Array.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Insert an element at the specified index position. | |||||||||||
Add an element to the List. | |||||||||||
Add all the elements from a Collection to the list. | |||||||||||
Insert all the elements from a Collection into the list at a specified position. | |||||||||||
Return the element at the specified position. | |||||||||||
Set the element at the specified position. | |||||||||||
Set the element Type and DynaClass. | |||||||||||
Set the element Type and DynaClass. | |||||||||||
Converts the List to an Array. | |||||||||||
Converts the List to an Array of the specified type. | |||||||||||
Converts the List to an DynaBean Array. |
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.util.ArrayList
| |||||||||||
From class
java.util.AbstractList
| |||||||||||
From class
java.util.AbstractCollection
| |||||||||||
From class
java.lang.Object
| |||||||||||
From interface
java.lang.Iterable
| |||||||||||
From interface
java.util.Collection
| |||||||||||
From interface
java.util.List
|
Default Constructor.
Construct a LazyDynaList with the specified capacity.
capacity | The initial capacity of the list. |
---|
Construct a LazyDynaList with a specified DynaClass for its elements.
elementDynaClass | The DynaClass of the List's elements. |
---|
Construct a LazyDynaList with a specified type for its elements.
elementType | The Type of the List's elements. |
---|
Construct a LazyDynaList populated with the elements of a Collection.
collection | The Collection to poulate the List from. |
---|
Construct a LazyDynaList populated with the elements of an Array.
array | The Array to poulate the List from. |
---|
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.
index | The index position to insert the new element. |
---|---|
element | The new element to add. |
Add an element to the List.
element | The new element to add. |
---|
Add all the elements from a Collection to the list.
collection | The Collection of new elements. |
---|
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.
index | The index position to insert the new elements at. |
---|---|
collection | The Collection of new elements. |
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.
index | The index position to insert the new elements at. |
---|
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.
index | The index position to insert the new element at. |
---|---|
element | The new element. |
Set the element Type and DynaClass.
elementDynaClass | The DynaClass of the elements. |
---|
IllegalArgumentException | if the List already contains elements or the DynaClass is null. |
---|
Set the element Type and DynaClass.
elementType | The type of the elements. |
---|
IllegalArgumentException | if the List already contains elements or the DynaClass is null. |
---|
Converts the List to an Array.
The type of Array created depends on the contents of the List:
Converts the List to an Array of the specified type.
model | The model for the type of array to return |
---|
Converts the List to an DynaBean Array.