Class LazyList
- All Implemented Interfaces:
Serializable,Iterable,Collection,List
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 factory = new Factory() {
public Object create() {
return new Date();
}
}
List lazy = LazyList.decorate(new ArrayList(), factory);
Object obj = lazy.get(3);
After the above code is executed, obj 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.
- Since:
- Commons Collections 3.0
- Version:
- $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
- Author:
- Stephen Colebourne, Arron Bates, Paul Jack
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final FactoryThe factory to use to lazily instantiate the objectsFields inherited from class org.apache.commons.collections.collection.AbstractCollectionDecorator
collection -
Constructor Summary
Constructors -
Method Summary
Methods inherited from class org.apache.commons.collections.list.AbstractListDecorator
add, addAll, getList, indexOf, lastIndexOf, listIterator, listIterator, remove, setMethods inherited from class org.apache.commons.collections.collection.AbstractCollectionDecorator
add, addAll, clear, contains, containsAll, equals, getCollection, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Field Details
-
factory
The factory to use to lazily instantiate the objects
-
-
Constructor Details
-
LazyList
Constructor that wraps (not copies).- Parameters:
list- the list to decorate, must not be nullfactory- the factory to use for creation, must not be null- Throws:
IllegalArgumentException- if list or factory is null
-
-
Method Details
-
decorate
Factory method to create a lazily instantiating list.- Parameters:
list- the list to decorate, must not be nullfactory- the factory to use for creation, must not be null- Throws:
IllegalArgumentException- if list or factory is null
-
get
Decorate the get method to perform the lazy behaviour.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.
- Specified by:
getin interfaceList- Overrides:
getin classAbstractListDecorator- Parameters:
index- the index to retrieve
-
subList
- Specified by:
subListin interfaceList- Overrides:
subListin classAbstractListDecorator
-