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 or transformer. 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
| Modifier and Type | Field and Description |
|---|---|
| private final Factory | factory
The factory to use to lazily instantiate the objects |
| private static final long | serialVersionUID
Hides org. |
| private final Transformer | transformer
The transformer to use to lazily instantiate the objects |
| Access | Constructor and Description |
|---|---|
| protected | |
| protected | LazyList(final List<E>
the list to decorate, must not be null list, final Transformer<Integer, ? extends E> the transformer to use for creation, must not be null transformer)Constructor that wraps (not copies). |
| Modifier and Type | Method and Description |
|---|---|
| private E | |
| public E | Returns: the element at the given indexthe index to retrieve index)Overrides org. Implements java. |
| public static < the type of the elements in the list E> LazyList | |
| public static < the type of the elements in the list E> LazyList | Returns: a new lazy listthe list to decorate, must not be null list, final Transformer<Integer, ? extends E> the transformer to use for creation, must not be null transformer)Transformer method to create a lazily instantiating list. |
| public List | subList(final int
low endpoint (inclusive) of the subList fromIndex, final int high endpoint (exclusive) of the subList toIndex)Overrides org. Implements java. fromIndex, inclusive, and toIndex, exclusive.
|
| factory | back to summary |
|---|---|
| private final Factory<? extends E> factory The factory to use to lazily instantiate the objects | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Hides org. Serialization version | |
| transformer | back to summary |
|---|---|
| private final Transformer<Integer, ? extends E> transformer The transformer to use to lazily instantiate the objects | |
| LazyList | back to summary |
|---|---|
| protected LazyList(final List<E> list, final Factory<? extends E> factory) Constructor that wraps (not copies).
| |
| LazyList | back to summary |
|---|---|
| protected LazyList(final List<E> list, final Transformer<Integer, ? extends E> transformer) Constructor that wraps (not copies).
| |
| element | back to summary |
|---|---|
| private E element(final int index) | |
| get | back to summary |
|---|---|
| public E get(final int index) Overrides org. Implements java. 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 or transformer. Indexes in-between the old size and the requested size are left with a placeholder that is replaced with a factory or transformer object when requested.
| |
| lazyList | back to summary |
|---|---|
| public static <E> LazyList Factory method to create a lazily instantiating list.
| |
| lazyList | back to summary |
|---|---|
| public static <E> LazyList Transformer method to create a lazily instantiating list.
| |
| subList | back to summary |
|---|---|
| public List Overrides org. Implements java. Doc from java. Returns a view of the portion of this list between the specified
This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:
Similar idioms may be constructed for indexOf and
lastIndexOf, and all of the algorithms in the
Collections class can be applied to a subList.The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.) | |