org.mortbay.util
Class LazyList

java.lang.Object
  extended byorg.mortbay.util.LazyList
All Implemented Interfaces:
Cloneable, Serializable

public class LazyList
extends Object
implements Cloneable, Serializable

Lazy List creation. A List helper class that attempts to avoid unneccessary List creation. If a method needs to create a List to return, but it is expected that this will either be empty or frequently contain a single item, then using LazyList will avoid additional object creations by using Collections.EMPTY_LIST or Collections.singletonList where possible.

Usage

   Object lazylist =null;
   while(loopCondition)
   {
     Object item = getItem();
     if (item.isToBeAdded())
         lazylist = LazyList.add(lazylist,item);
   }
   return LazyList.getList(lazylist);
 
An ArrayList of default size is used as the initial LazyList.

Author:
Greg Wilkins (gregw)
See Also:
List, Serialized Form

Method Summary
static Object add(Object list, int index, Object item)
          Add an item to a LazyList
static Object add(Object list, Object item)
          Add an item to a LazyList
static Object addArray(Object list, Object[] array)
          Add the contents of an array to a LazyList
static Object addCollection(Object list, Collection collection)
          Add the contents of a Collection to a LazyList
static Object[] addToArray(Object[] array, Object item, Class type)
          Add element to an array
static List array2List(Object[] array)
           
static Object clone(Object list)
           
static boolean contains(Object list, Object item)
           
static Object ensureSize(Object list, int initialSize)
          Ensure the capcity of the underlying list.
static Object get(Object list, int i)
          Get item from the list
static List getList(Object list)
          Get the real List from a LazyList.
static List getList(Object list, boolean nullForEmpty)
          Get the real List from a LazyList.
static Iterator iterator(Object list)
           
static ListIterator listIterator(Object list)
           
static Object remove(Object list, int i)
           
static Object remove(Object list, Object o)
           
static Object[] removeFromArray(Object[] array, Object item)
           
static int size(Object list)
          The size of a lazy List
static Object toArray(Object list, Class aClass)
           
static String toString(Object list)
           
static String[] toStringArray(Object list)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

add

public static Object add(Object list,
                         Object item)
Add an item to a LazyList

Parameters:
list - The list to add to or null if none yet created.
item - The item to add.
Returns:
The lazylist created or added to.

add

public static Object add(Object list,
                         int index,
                         Object item)
Add an item to a LazyList

Parameters:
list - The list to add to or null if none yet created.
index - The index to add the item at.
item - The item to add.
Returns:
The lazylist created or added to.

addCollection

public static Object addCollection(Object list,
                                   Collection collection)
Add the contents of a Collection to a LazyList

Parameters:
list - The list to add to or null if none yet created.
collection - The Collection whose contents should be added.
Returns:
The lazylist created or added to.

addArray

public static Object addArray(Object list,
                              Object[] array)
Add the contents of an array to a LazyList

Parameters:
list - The list to add to or null if none yet created.
Returns:
The lazylist created or added to.

ensureSize

public static Object ensureSize(Object list,
                                int initialSize)
Ensure the capcity of the underlying list.


remove

public static Object remove(Object list,
                            Object o)

remove

public static Object remove(Object list,
                            int i)

getList

public static List getList(Object list)
Get the real List from a LazyList.

Parameters:
list - A LazyList returned from LazyList.add(Object)
Returns:
The List of added items, which may be an EMPTY_LIST or a SingletonList.

getList

public static List getList(Object list,
                           boolean nullForEmpty)
Get the real List from a LazyList.

Parameters:
list - A LazyList returned from LazyList.add(Object) or null
nullForEmpty - If true, null is returned instead of an empty list.
Returns:
The List of added items, which may be null, an EMPTY_LIST or a SingletonList.

toStringArray

public static String[] toStringArray(Object list)

toArray

public static Object toArray(Object list,
                             Class aClass)

size

public static int size(Object list)
The size of a lazy List

Parameters:
list - A LazyList returned from LazyList.add(Object) or null
Returns:
the size of the list.

get

public static Object get(Object list,
                         int i)
Get item from the list

Parameters:
list - A LazyList returned from LazyList.add(Object) or null
i - int index
Returns:
the item from the list.

contains

public static boolean contains(Object list,
                               Object item)

clone

public static Object clone(Object list)

toString

public static String toString(Object list)

iterator

public static Iterator iterator(Object list)

listIterator

public static ListIterator listIterator(Object list)

array2List

public static List array2List(Object[] array)
Parameters:
array - Any array of object
Returns:
A new modifiable list initialised with the elements from array.

addToArray

public static Object[] addToArray(Object[] array,
                                  Object item,
                                  Class type)
Add element to an array

Parameters:
array - The array to add to (or null)
item - The item to add
type - The type of the array (in case of null array)
Returns:
new array with contents of array plus item

removeFromArray

public static Object[] removeFromArray(Object[] array,
                                       Object item)