Class LazyDynaList

All Implemented Interfaces:
Serializable, Cloneable, Iterable<Object>, Collection<Object>, List<Object>, RandomAccess

public class LazyDynaList extends ArrayList<Object>

Lazy DynaBean List. Link icon

There are two main purposes for this class:

  • To provide Lazy List behavior - automatically growing and populating the List with either DynaBean</code>, <code>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</code> elements are "wrapped" in a <code>LazyDynaMap.
  • POJO Bean elements are "wrapped" in a WrapDynaBean.
  • DynaBean's are stored un-changed.

toArray() Link icon

The toArray() method returns an array of the elements of the appropriate type. If the LazyDynaList is populated with 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() Link icon

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</code> or <code>Class of the LazyDynaList's elements is not specified, then it will be automatically set to the type of the first element populated.

Example 1 Link icon

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());
 

Example 2 Link icon

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 Link icon

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</code> rather than the <code>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.

Example 4 Link icon

A slight variation - set the element type using either the setElementType(Class) method or the setElementDynaClass(DynaClass) method - then populate with the normal 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);

 
Since:
1.8.0
See Also:
  • Constructor Details Link icon

    • LazyDynaList Link icon

      public LazyDynaList()
      Constructs a new instance.
    • LazyDynaList Link icon

      public LazyDynaList(Class<?> elementType)
      Constructs a LazyDynaList with a specified type for its elements.
      Parameters:
      elementType - The Type of the List's elements.
    • LazyDynaList Link icon

      public LazyDynaList(Collection<?> collection)
      Constructs a LazyDynaList populated with the elements of a Collection.
      Parameters:
      collection - The Collection to populate the List from.
    • LazyDynaList Link icon

      public LazyDynaList(DynaClass elementDynaClass)
      Constructs a LazyDynaList with a specified DynaClass for its elements.
      Parameters:
      elementDynaClass - The DynaClass of the List's elements.
    • LazyDynaList Link icon

      public LazyDynaList(int capacity)
      Constructs a LazyDynaList with the specified capacity.
      Parameters:
      capacity - The initial capacity of the list.
    • LazyDynaList Link icon

      public LazyDynaList(Object[] array)
      Constructs a LazyDynaList populated with the elements of an Array.
      Parameters:
      array - The Array to populate the List from.
  • Method Details Link icon

    • add Link icon

      public void add(int index, 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 interface List<Object>
      Overrides:
      add in class ArrayList<Object>
      Parameters:
      index - The index position to insert the new element.
      element - The new element to add.
    • add Link icon

      public boolean add(Object element)

      Add an element to the List.

      Specified by:
      add in interface Collection<Object>
      Specified by:
      add in interface List<Object>
      Overrides:
      add in class ArrayList<Object>
      Parameters:
      element - The new element to add.
      Returns:
      true.
    • addAll Link icon

      public boolean addAll(Collection<?> collection)

      Add all the elements from a Collection to the list.

      Specified by:
      addAll in interface Collection<Object>
      Specified by:
      addAll in interface List<Object>
      Overrides:
      addAll in class ArrayList<Object>
      Parameters:
      collection - The Collection of new elements.
      Returns:
      true if elements were added.
    • addAll Link icon

      public boolean addAll(int index, 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 interface List<Object>
      Overrides:
      addAll in class ArrayList<Object>
      Parameters:
      index - The index position to insert the new elements at.
      collection - The Collection of new elements.
      Returns:
      true if elements were added.
    • get Link icon

      public 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 interface List<Object>
      Overrides:
      get in class ArrayList<Object>
      Parameters:
      index - The index position to insert the new elements at.
      Returns:
      The element at the specified position.
    • set Link icon

      public Object set(int index, 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 interface List<Object>
      Overrides:
      set in class ArrayList<Object>
      Parameters:
      index - The index position to insert the new element at.
      element - The new element.
      Returns:
      The new element.
    • setElementDynaClass Link icon

      public void setElementDynaClass(DynaClass elementDynaClass)

      Set the element Type and DynaClass.

      Parameters:
      elementDynaClass - The DynaClass of the elements.
      Throws:
      IllegalArgumentException - if the List already contains elements or the DynaClass is null.
    • setElementType Link icon

      public void setElementType(Class<?> elementType)

      Set the element Type and DynaClass.

      Parameters:
      elementType - The type of the elements.
      Throws:
      IllegalArgumentException - if the List already contains elements or the DynaClass is null.
    • toArray Link icon

      public 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 interface Collection<Object>
      Specified by:
      toArray in interface List<Object>
      Overrides:
      toArray in class ArrayList<Object>
      Returns:
      An Array of the elements in this List.
    • toArray Link icon

      public <T> T[] toArray(T[] model)

      Converts the List to an Array of the specified type.

      Specified by:
      toArray in interface Collection<Object>
      Specified by:
      toArray in interface List<Object>
      Overrides:
      toArray in class ArrayList<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 Link icon

      Converts the List to an DynaBean Array.

      Returns:
      A DynaBean[] of the elements in this List.