org.mortbay.util
Class LazyList

java.lang.Object
  extended by org.mortbay.util.LazyList

public class LazyList
extends java.lang.Object

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.

Version:
$Revision: 1.18 $
Author:
Greg Wilkins (gregw)
See Also:
List

Method Summary
protected  java.lang.Object add(java.lang.Object list, java.util.Collection collection)
          Deprecated. Use addCollection
static java.lang.Object add(java.lang.Object list, int index, java.lang.Object item)
          Add an item to a LazyList
static java.lang.Object add(java.lang.Object list, java.lang.Object item)
          Add an item to a LazyList
static java.lang.Object addCollection(java.lang.Object list, java.util.Collection collection)
          Add the contents of a Collection to a LazyList
static java.lang.Object clone(java.lang.Object list)
           
static boolean contains(java.lang.Object list, java.lang.Object item)
           
static java.lang.Object ensureSize(java.lang.Object list, int initialSize)
           
static java.lang.Object get(java.lang.Object list, int i)
          Get item from the list
static java.util.List getList(java.lang.Object list)
          Get the real List from a LazyList.
static java.util.List getList(java.lang.Object list, boolean nullForEmpty)
          Get the real List from a LazyList.
static java.util.Iterator iterator(java.lang.Object list)
           
static java.util.ListIterator listIterator(java.lang.Object list)
           
static java.lang.Object remove(java.lang.Object list, int i)
           
static java.lang.Object remove(java.lang.Object list, java.lang.Object o)
           
static int size(java.lang.Object list)
          The size of a lazy List
static java.lang.String toString(java.lang.Object list)
           
static java.lang.String[] toStringArray(java.lang.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 java.lang.Object add(java.lang.Object 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 java.lang.Object add(java.lang.Object list,
                                   int index,
                                   java.lang.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.

add

protected java.lang.Object add(java.lang.Object list,
                               java.util.Collection collection)
Deprecated. Use addCollection

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.

addCollection

public static java.lang.Object addCollection(java.lang.Object list,
                                             java.util.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.

ensureSize

public static java.lang.Object ensureSize(java.lang.Object list,
                                          int initialSize)

remove

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

remove

public static java.lang.Object remove(java.lang.Object list,
                                      int i)

getList

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

size

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

clone

public static java.lang.Object clone(java.lang.Object list)

toString

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

iterator

public static java.util.Iterator iterator(java.lang.Object list)

listIterator

public static java.util.ListIterator listIterator(java.lang.Object list)


Copyright © 2004 Mortbay Consulting Pty. Ltd. All Rights Reserved.