org.mortbay.util
Class LazyList

java.lang.Object
  |
  +--java.util.AbstractCollection
        |
        +--java.util.AbstractList
              |
              +--org.mortbay.util.LazyList
All Implemented Interfaces:
java.lang.Cloneable, java.util.Collection, java.util.List, java.io.Serializable

public class LazyList
extends java.util.AbstractList
implements java.lang.Cloneable, java.io.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

   LazyList 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.

Version:
$Revision: 1.9 $
Author:
Greg Wilkins (gregw)
See Also:
List, Serialized Form

Method Summary
static LazyList add(LazyList list, java.util.Collection collection)
          Add an item to a LazyList
static LazyList add(LazyList list, int initialSize, java.lang.Object item)
          Add an item to a LazyList
static LazyList add(LazyList list, java.lang.Object item)
          Add an item to a LazyList
 java.lang.Object clone()
           
static LazyList clone(LazyList list)
           
 java.lang.Object get(int i)
           
static java.lang.Object get(LazyList list, int i)
          Get item from the list
static java.util.List getList(LazyList list)
          Get the real List from a LazyList.
static java.util.List getList(LazyList list, boolean nullForEmpty)
          Get the real List from a LazyList.
 java.util.Iterator iterator()
           
 java.util.ListIterator listIterator()
           
 java.util.ListIterator listIterator(int i)
           
static LazyList remove(LazyList list, java.lang.Object o)
           
 int size()
           
static int size(LazyList list)
          The size of a lazy List
 java.lang.String toString()
           
static java.lang.String toString(LazyList list)
           
static java.lang.String[] toStringArray(LazyList list)
           
 
Methods inherited from class java.util.AbstractList
add, add, addAll, clear, equals, hashCode, indexOf, lastIndexOf, remove, set, subList
 
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
 

Method Detail

add

public static LazyList add(LazyList list,
                           java.lang.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 LazyList add(LazyList list,
                           java.util.Collection collection)
Add an item to a LazyList

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

add

public static LazyList add(LazyList list,
                           int initialSize,
                           java.lang.Object item)
Add an item to a LazyList

Parameters:
list - The list to add to or null if none yet created.
initialSize - A size to use when creating the real list
item - The item to add.
Returns:
The lazylist created or added to.

remove

public static LazyList remove(LazyList list,
                              java.lang.Object o)

getList

public static java.util.List getList(LazyList 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 java.util.List getList(LazyList 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 java.lang.String[] toStringArray(LazyList list)

size

public static int size(LazyList 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 java.lang.Object get(LazyList list,
                                   int i)
Get item from the list

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

clone

public static LazyList clone(LazyList list)

get

public java.lang.Object get(int i)
Specified by:
get in interface java.util.List
Specified by:
get in class java.util.AbstractList

size

public int size()
Specified by:
size in interface java.util.List
Specified by:
size in class java.util.AbstractCollection

listIterator

public java.util.ListIterator listIterator()
Specified by:
listIterator in interface java.util.List
Overrides:
listIterator in class java.util.AbstractList

listIterator

public java.util.ListIterator listIterator(int i)
Specified by:
listIterator in interface java.util.List
Overrides:
listIterator in class java.util.AbstractList

iterator

public java.util.Iterator iterator()
Specified by:
iterator in interface java.util.List
Overrides:
iterator in class java.util.AbstractList

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.util.AbstractCollection

toString

public static java.lang.String toString(LazyList list)


Copyright ? 2000 Mortbay Consulting Pty. Ltd. All Rights Reserved.