org.apache.commons.collections.list
Class LazyList<E>

java.lang.Object
  extended by org.apache.commons.collections.collection.AbstractCollectionDecorator<E>
      extended by org.apache.commons.collections.list.AbstractListDecorator<E>
          extended by org.apache.commons.collections.list.AbstractSerializableListDecorator<E>
              extended by org.apache.commons.collections.list.LazyList<E>
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, List<E>

public class LazyList<E>
extends AbstractSerializableListDecorator<E>

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. 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 is Serializable from Commons Collections 3.1.

Since:
Commons Collections 3.0
Version:
$Revision: 1.1.1.1 $ $Date: 2005/05/23 04:35:22 $
Author:
Stephen Colebourne, Arron Bates, Paul Jack
See Also:
Serialized Form

Field Summary
protected  Factory<? extends E> factory
          The factory to use to lazily instantiate the objects
 
Fields inherited from class org.apache.commons.collections.collection.AbstractCollectionDecorator
collection
 
Constructor Summary
protected LazyList(List<E> list, Factory<? extends E> factory)
          Constructor that wraps (not copies).
 
Method Summary
static
<E> List<E>
decorate(List<E> list, Factory<? extends E> factory)
          Factory method to create a lazily instantiating list.
 E get(int index)
          Decorate the get method to perform the lazy behaviour.
 List<E> subList(int fromIndex, int toIndex)
           
 
Methods inherited from class org.apache.commons.collections.list.AbstractListDecorator
add, addAll, getList, indexOf, lastIndexOf, listIterator, listIterator, remove, set
 
Methods 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, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray
 

Field Detail

factory

protected final Factory<? extends E> factory
The factory to use to lazily instantiate the objects

Constructor Detail

LazyList

protected LazyList(List<E> list,
                   Factory<? extends E> factory)
Constructor that wraps (not copies).

Parameters:
list - the list to decorate, must not be null
factory - the factory to use for creation, must not be null
Throws:
IllegalArgumentException - if list or factory is null
Method Detail

decorate

public static <E> List<E> decorate(List<E> list,
                                   Factory<? extends E> factory)
Factory method to create a lazily instantiating list.

Parameters:
list - the list to decorate, must not be null
factory - the factory to use for creation, must not be null
Throws:
IllegalArgumentException - if list or factory is null

get

public E get(int index)
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:
get in interface List<E>
Overrides:
get in class AbstractListDecorator<E>
Parameters:
index - the index to retrieve

subList

public List<E> subList(int fromIndex,
                       int toIndex)
Specified by:
subList in interface List<E>
Overrides:
subList in class AbstractListDecorator<E>


Copyright © 2005-2005 Apache Software Foundation, Matt Hall, John Watkinson. All Rights Reserved.