org.mortbay.util
Class LazyList
java.lang.Object
|
+--java.util.AbstractCollection
|
+--java.util.AbstractList
|
+--org.mortbay.util.LazyList
- All Implemented Interfaces:
- java.util.Collection, java.util.List
- public class LazyList
- extends java.util.AbstractList
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.3 $
- Author:
- Greg Wilkins (gregw)
- See Also:
List
Method Summary |
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 |
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)
|
int |
size()
|
static int |
size(LazyList list)
The size of a lazy 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, toString |
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 |
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,
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 listitem
- The item to add.- Returns:
- The lazylist created or added to.
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 nullnullForEmpty
- 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.
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 nullint
- i index- Returns:
- the item from the list.
get
public java.lang.Object get(int i)
- Overrides:
get
in class java.util.AbstractList
size
public int size()
- Overrides:
size
in class java.util.AbstractCollection
listIterator
public java.util.ListIterator listIterator()
- Overrides:
listIterator
in class java.util.AbstractList
listIterator
public java.util.ListIterator listIterator(int i)
- Overrides:
listIterator
in class java.util.AbstractList
iterator
public java.util.Iterator iterator()
- Overrides:
iterator
in class java.util.AbstractList
Copyright © 2000 Mortbay Consulting Pty. Ltd. All Rights Reserved.