Top Description Fields Constructors Methods
org.apache.commons.collections4.list

public Class LazyList<E>

extends AbstractSerializableListDecorator<E>
Class Inheritance
Imports
java.util.List, .Objects, org.apache.commons.collections4.Factory, .Transformer

Decorates another 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.

Since
3.0
See Also
GrowthList

Field Summary

Modifier and TypeField and Description
private final Factory<? extends E>
factory

The factory to use to lazily instantiate the objects

private static final long
private final Transformer<Integer, ? extends E>
transformer

The transformer to use to lazily instantiate the objects

Constructor Summary

AccessConstructor and Description
protected
LazyList(final List<E>
the list to decorate, must not be null
list
,
final Factory<? extends E>
the factory to use for creation, must not be null
factory
)

Constructor that wraps (not copies).

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).

Method Summary

Modifier and TypeMethod and Description
private E
element(final int index)

public E

Returns:

the element at the given index
get
(final int
the index to retrieve
index
)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.get.

Implements java.util.List.get.

Decorate the get method to perform the lazy behaviour.
public static <
the type of the elements in the list
E
>
LazyList<E>

Returns:

a new lazy list
lazyList
(final List<E>
the list to decorate, must not be null
list
,
final Factory<? extends E>
the factory to use for creation, must not be null
factory
)

Factory method to create a lazily instantiating list.

public static <
the type of the elements in the list
E
>
LazyList<E>

Returns:

a new lazy list
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
)

Transformer method to create a lazily instantiating list.

public List<E>
subList(final int
low endpoint (inclusive) of the subList
fromIndex
,
final int
high endpoint (exclusive) of the subList
toIndex
)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.subList.

Implements java.util.List.subList.

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.

Field Detail

factoryback to summary
private final Factory<? extends E> factory

The factory to use to lazily instantiate the objects

serialVersionUIDback to summary
private static final long serialVersionUID

Hides org.apache.commons.collections4.list.AbstractSerializableListDecorator.serialVersionUID.

Serialization version

transformerback to summary
private final Transformer<Integer, ? extends E> transformer

The transformer to use to lazily instantiate the objects

Constructor Detail

LazyListback to summary
protected LazyList(final List<E> list, final Factory<? extends E> factory)

Constructor that wraps (not copies).

Parameters
list:List<E>

the list to decorate, must not be null

factory:Factory<? extends E>

the factory to use for creation, must not be null

Exceptions
NullPointerException:
if list or factory is null
LazyListback to summary
protected LazyList(final List<E> list, final Transformer<Integer, ? extends E> transformer)

Constructor that wraps (not copies).

Parameters
list:List<E>

the list to decorate, must not be null

transformer:Transformer<Integer, ? extends E>

the transformer to use for creation, must not be null

Exceptions
NullPointerException:
if list or transformer is null

Method Detail

elementback to summary
private E element(final int index)
getback to summary
public E get(final int index)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.get.

Implements java.util.List.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 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.

Parameters
index:int

the index to retrieve

Returns:E

the element at the given index

Annotations
@Override
lazyListback to summary
public static <E> LazyList<E> lazyList(final List<E> list, final Factory<? extends E> factory)

Factory method to create a lazily instantiating list.

Parameters
<E>
the type of the elements in the list
list:List<E>

the list to decorate, must not be null

factory:Factory<? extends E>

the factory to use for creation, must not be null

Returns:LazyList<E>

a new lazy list

Exceptions
NullPointerException:
if list or factory is null
Since
4.0
lazyListback to summary
public static <E> LazyList<E> lazyList(final List<E> list, final Transformer<Integer, ? extends E> transformer)

Transformer method to create a lazily instantiating list.

Parameters
<E>
the type of the elements in the list
list:List<E>

the list to decorate, must not be null

transformer:Transformer<Integer, ? extends E>

the transformer to use for creation, must not be null

Returns:LazyList<E>

a new lazy list

Exceptions
NullPointerException:
if list or transformer is null
Since
4.4
subListback to summary
public List<E> subList(final int fromIndex, final int toIndex)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.subList.

Implements java.util.List.subList.

Doc from java.util.List.subList.

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.

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:

list.subList(from, to).clear();
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.)

Parameters
fromIndex:int

low endpoint (inclusive) of the subList

toIndex:int

high endpoint (exclusive) of the subList

Returns:List<E>

a view of the specified range within this list

Annotations
@Override