org.mortbay.util

Class LazyList

public class LazyList extends 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: java.util.List

Method Summary
static Objectadd(Object list, Object item)
Add an item to a LazyList
static Objectadd(Object list, int index, Object item)
Add an item to a LazyList
protected Objectadd(Object list, Collection collection)
Add the contents of a Collection to a LazyList
static ObjectaddCollection(Object list, Collection collection)
Add the contents of a Collection to a LazyList
static Objectclone(Object list)
static booleancontains(Object list, Object item)
static ObjectensureSize(Object list, int initialSize)
static Objectget(Object list, int i)
Get item from the list
static ListgetList(Object list)
Get the real List from a LazyList.
static ListgetList(Object list, boolean nullForEmpty)
Get the real List from a LazyList.
static Iteratoriterator(Object list)
static ListIteratorlistIterator(Object list)
static Objectremove(Object list, Object o)
static Objectremove(Object list, int i)
static intsize(Object list)
The size of a lazy List
static StringtoString(Object list)
static String[]toStringArray(Object list)

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.

add

protected Object add(Object list, 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 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.

clone

public static Object clone(Object list)

contains

public static boolean contains(Object list, Object item)

ensureSize

public static Object ensureSize(Object list, int initialSize)

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.

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.

iterator

public static Iterator iterator(Object list)

listIterator

public static ListIterator listIterator(Object list)

remove

public static Object remove(Object list, Object o)

remove

public static Object remove(Object list, int i)

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.

toString

public static String toString(Object list)

toStringArray

public static String[] toStringArray(Object list)
Copyright © 2004 Mortbay Consulting Pty. Ltd. All Rights Reserved.