Top Description Fields Constructors Methods
org.eclipse.jetty.util

public Class LazyList

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

Lazy List creation.

A List helper class that attempts to avoid unnecessary 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(Object) where possible.

LazyList works by passing an opaque representation of the list in and out of all the LazyList methods. This opaque object is either null for an empty list, an Object for a list with a single entry or an ArrayList for a list of items.

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.
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 array 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<?>
The Collection whose contents should be added.
collection
)

Add the contents of a Collection to a LazyList

public static Object
clone(Object list)

public static boolean
contains(Object list, Object item)

public static Object

Returns:

the new List with new size
ensureSize
(Object
the list to grow
list
,
int
the size to grow to
initialSize
)

Ensure the capacity of the underlying list.

public static <
the list entry type
E
>
E

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 <
the list entry type
E
>
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 <
the list entry type
E
>
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 boolean

Returns:

true if not-null and is not empty
hasEntry
(Object
a LazyList, List or Object
list
)

Simple utility method to test if List has at least 1 entry.

public static boolean

Returns:

true if null or is empty
isEmpty
(Object
a LazyList, List or Object
list
)

Simple utility method to test if List is empty

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

public static Object
remove(Object list, int i)

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

Returns:

array of the lazylist entries passed in
toArray
(Object
The list to convert
list
,
Class<?>
The class of the array, which may be a primitive type
clazz
)

Convert a lazylist to an array

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

Field Detail

__EMPTY_STRING_ARRAYback to summary
private static final String[] __EMPTY_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.

Annotations
@SuppressWarnings:unchecked
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.

Annotations
@SuppressWarnings:unchecked
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 array whose contents should be added.

Returns:Object

The lazylist created or added to.

addCollectionback to summary
public static Object addCollection(Object list, Collection<?> 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<?>

The Collection whose contents should be added.

Returns:Object

The lazylist created or added to.

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 capacity of the underlying list.

Parameters
list:Object

the list to grow

initialSize:int

the size to grow to

Returns:Object

the new List with new size

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

Get item from the list

Parameters
<E>
the list entry type
list:Object

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

i:int

int index

Returns:E

the item from the list.

Annotations
@SuppressWarnings:unchecked
getListback to summary
public static <E> List<E> getList(Object list)

Get the real List from a LazyList.

Parameters
<E>
the list entry type
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 <E> List<E> getList(Object list, boolean nullForEmpty)

Get the real List from a LazyList.

Parameters
<E>
the list entry type
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.

Annotations
@SuppressWarnings:unchecked
hasEntryback to summary
public static boolean hasEntry(Object list)

Simple utility method to test if List has at least 1 entry.

Parameters
list:Object

a LazyList, List or Object

Returns:boolean

true if not-null and is not empty

isEmptyback to summary
public static boolean isEmpty(Object list)

Simple utility method to test if List is empty

Parameters
list:Object

a LazyList, List or Object

Returns:boolean

true if null or is empty

iteratorback to summary
public static <E> Iterator<E> iterator(Object list)
Annotations
@SuppressWarnings:unchecked
listIteratorback to summary
public static <E> ListIterator<E> listIterator(Object list)
Annotations
@SuppressWarnings:unchecked
removeback to summary
public static Object remove(Object list, Object o)
removeback to summary
public static Object remove(Object list, int i)
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<?> clazz)

Convert a lazylist to an array

Parameters
list:Object

The list to convert

clazz:Class<?>

The class of the array, which may be a primitive type

Returns:Object

array of the lazylist entries passed in

toStringback to summary
public static String toString(Object list)
toStringArrayback to summary
public static String[] toStringArray(Object list)