public class LazyList<E> extends AbstractSerializableListDecorator<E>
List to create objects in the list on demand.
When the get(int) method is called with an index greater than the size of the list, the list will automatically grow in size and return a new object from the specified factory. The gaps will be filled by null. If a get method call encounters a null, it will be replaced with a new object from the factory. Thus this list is unsuitable for storing null objects.
For instance:
Factory<Date> factory = new Factory<Date>() {
public Date create() {
return new Date();
}
}
List<Date> lazy = LazyList.decorate(new ArrayList<Date>(), factory);
Date date = lazy.get(3);
After the above code is executed,
date will contain a new
Date instance. Furthermore, that
Date instance is the fourth element in the list. The first, second, and third element are all set to
null.
This class differs from GrowthList because here growth occurs on get, where GrowthList grows on set and add. However, they could easily be used together by decorating twice.
This class is Serializable from Commons Collections 3.1.
GrowthList,
Serialized Form
| Modifier | Constructor and Description |
|---|---|
protected |
LazyList(List
Constructor that wraps (not copies).
|
| Modifier and Type | Method and Description |
|---|---|
E |
get(int index)
Decorate the get method to perform the lazy behaviour.
|
static <E> LazyList |
lazyList(List
Factory method to create a lazily instantiating list.
|
List |
subList(int fromIndex, int toIndex)
|
add, addAll, decorated, equals, hashCode, indexOf, lastIndexOf, listIterator, listIterator, remove, setadd, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, setCollection, size, toArray, toArray, toStringclone, finalize, getClass, notify, notifyAll, wait, wait, waitadd, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, replaceAll, retainAll, size, sort, spliterator, toArray, toArrayparallelStream, removeIf, streamprotected LazyList(List<E> list, Factory <? extends E> factory)
list - the list to decorate, must not be null
factory - the factory to use for creation, must not be null
NullPointerException - if list or factory is null
public static <E> LazyList<E> lazyList(List <E> list, Factory <? extends E> factory)
E - the type of the elements in the list
list - the list to decorate, must not be null
factory - the factory to use for creation, must not be null
NullPointerException - if list or factory is null
public E get(int index)
If the requested index is greater than the current size, the list will grow to the new size and a new object will be returned from the factory. Indexes in-between the old size and the requested size are left with a placeholder that is replaced with a factory object when requested.