Top Description Fields Constructors Methods
org.mortbay.util

public Class LazyList

extends Object
implements Cloneable, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Cloneable
Imports
java.io.Serializable, java.lang.reflect.Array, java.util.ArrayList, .Arrays, .Collection, .Collections, .Iterator, .List, .ListIterator

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
java.util.List

Field Summary

Modifier and TypeField and Description
private static final String[]

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Object

Returns:

The lazylist created or added to.
add
(Object
The list to add to or null if none yet created.
list
,
Object
The item to add.
item
)

Add an item to a LazyList

public static Object

Returns:

The lazylist created or added to.
add
(Object
The list to add to or null if none yet created.
list
,
int
The index to add the item at.
index
,
Object
The item to add.
item
)

Add an item to a LazyList

public static Object

Returns:

The lazylist created or added to.
addArray
(Object
The list to add to or null if none yet created.
list
,
Object[]
The Collection whose contents should be added.
array
)

Add the contents of an array to a LazyList

public static Object

Returns:

The lazylist created or added to.
addCollection
(Object
The list to add to or null if none yet created.
list
,
Collection<E>
The Collection whose contents should be added.
collection
)

Add the contents of a Collection to a LazyList

public static Object[]

Returns:

new array with contents of array plus item
addToArray
(Object[]
The array to add to (or null)
array
,
Object
The item to add
item
,
Class<T>
The type of the array (in case of null array)
type
)

Add element to an array

public static List<E>

Returns:

A new modifiable list initialised with the elements from array.
array2List
(Object[]
Any array of object
array
)

public static Object
clone(Object list)

public static boolean
contains(Object list, Object item)

public static Object
ensureSize(Object list, int initialSize)

Ensure the capcity of the underlying list.

public static Object

Returns:

the item from the list.
get
(Object
A LazyList returned from LazyList.add(Object) or null
list
,
int
int index
i
)

Get item from the list

public static List<E>

Returns:

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

Get the real List from a LazyList.

public static List<E>

Returns:

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

Get the real List from a LazyList.

public static Iterator<E>
public static ListIterator<E>
public static Object
remove(Object list, Object o)

public static Object
remove(Object list, int i)

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

public static int

Returns:

the size of the list.
size
(Object
A LazyList returned from LazyList.add(Object) or null
list
)

The size of a lazy List

public static Object
toArray(Object list, Class<T> aClass)

public static String
public static String[]
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

__EMTPY_STRING_ARRAYback to summary
private static final String[] __EMTPY_STRING_ARRAY

Constructor Detail

LazyListback to summary
private LazyList()

Method Detail

addback to summary
public static Object add(Object list, Object item)

Add an item to a LazyList

Parameters
list:Object

The list to add to or null if none yet created.

item:Object

The item to add.

Returns:Object

The lazylist created or added to.

addback to summary
public static Object add(Object list, int index, Object item)

Add an item to a LazyList

Parameters
list:Object

The list to add to or null if none yet created.

index:int

The index to add the item at.

item:Object

The item to add.

Returns:Object

The lazylist created or added to.

addArrayback to summary
public static Object addArray(Object list, Object[] array)

Add the contents of an array to a LazyList

Parameters
list:Object

The list to add to or null if none yet created.

array:Object[]

The Collection whose contents should be added.

Returns:Object

The lazylist created or added to.

addCollectionback to summary
public static Object addCollection(Object list, Collection<E> collection)

Add the contents of a Collection to a LazyList

Parameters
list:Object

The list to add to or null if none yet created.

collection:Collection<E>

The Collection whose contents should be added.

Returns:Object

The lazylist created or added to.

addToArrayback to summary
public static Object[] addToArray(Object[] array, Object item, Class<T> type)

Add element to an array

Parameters
array:Object[]

The array to add to (or null)

item:Object

The item to add

type:Class<T>

The type of the array (in case of null array)

Returns:Object[]

new array with contents of array plus item

array2Listback to summary
public static List<E> array2List(Object[] array)
Parameters
array:Object[]

Any array of object

Returns:List<E>

A new modifiable list initialised with the elements from array.

cloneback to summary
public static Object clone(Object list)
containsback to summary
public static boolean contains(Object list, Object item)
ensureSizeback to summary
public static Object ensureSize(Object list, int initialSize)

Ensure the capcity of the underlying list.

getback to summary
public static Object get(Object list, int i)

Get item from the list

Parameters
list:Object

A LazyList returned from LazyList.add(Object) or null

i:int

int index

Returns:Object

the item from the list.

getListback to summary
public static List<E> getList(Object list)

Get the real List from a LazyList.

Parameters
list:Object

A LazyList returned from LazyList.add(Object)

Returns:List<E>

The List of added items, which may be an EMPTY_LIST or a SingletonList.

getListback to summary
public static List<E> getList(Object list, boolean nullForEmpty)

Get the real List from a LazyList.

Parameters
list:Object

A LazyList returned from LazyList.add(Object) or null

nullForEmpty:boolean

If true, null is returned instead of an empty list.

Returns:List<E>

The List of added items, which may be null, an EMPTY_LIST or a SingletonList.

iteratorback to summary
public static Iterator<E> iterator(Object list)
listIteratorback to summary
public static ListIterator<E> listIterator(Object list)
removeback to summary
public static Object remove(Object list, Object o)
removeback to summary
public static Object remove(Object list, int i)
removeFromArrayback to summary
public static Object[] removeFromArray(Object[] array, Object item)
sizeback to summary
public static int size(Object list)

The size of a lazy List

Parameters
list:Object

A LazyList returned from LazyList.add(Object) or null

Returns:int

the size of the list.

toArrayback to summary
public static Object toArray(Object list, Class<T> aClass)
toStringback to summary
public static String toString(Object list)
toStringArrayback to summary
public static String[] toStringArray(Object list)