Merging from -r468106:814127 of collections_jdk5_branch - namely where this code was generified; mostly in r738956.

Also see the following revisions:

    ------------------------------------------------------------------------
    r471192 | scolebourne | 2006-11-04 06:04:46 -0800 (Sat, 04 Nov 2006) | 1 line
    
    Remove getList() - use decorated()
    ------------------------------------------------------------------------


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@815065 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-09-15 05:55:41 +00:00
parent 23c5be297b
commit 2b9145c38e
1 changed files with 21 additions and 24 deletions

View File

@ -61,13 +61,13 @@ import org.apache.commons.collections.Factory;
* @author Arron Bates * @author Arron Bates
* @author Paul Jack * @author Paul Jack
*/ */
public class LazyList extends AbstractSerializableListDecorator { public class LazyList<E> extends AbstractSerializableListDecorator<E> {
/** Serialization version */ /** Serialization version */
private static final long serialVersionUID = -1708388017160694542L; private static final long serialVersionUID = -1708388017160694542L;
/** The factory to use to lazily instantiate the objects */ /** The factory to use to lazily instantiate the objects */
protected final Factory factory; protected final Factory<? extends E> factory;
/** /**
* Factory method to create a lazily instantiating list. * Factory method to create a lazily instantiating list.
@ -76,8 +76,8 @@ public class LazyList extends AbstractSerializableListDecorator {
* @param factory the factory to use for creation, must not be null * @param factory the factory to use for creation, must not be null
* @throws IllegalArgumentException if list or factory is null * @throws IllegalArgumentException if list or factory is null
*/ */
public static List decorate(List list, Factory factory) { public static <E> List<E> decorate(List<E> list, Factory<? extends E> factory) {
return new LazyList(list, factory); return new LazyList<E>(list, factory);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@ -88,7 +88,7 @@ public class LazyList extends AbstractSerializableListDecorator {
* @param factory the factory to use for creation, must not be null * @param factory the factory to use for creation, must not be null
* @throws IllegalArgumentException if list or factory is null * @throws IllegalArgumentException if list or factory is null
*/ */
protected LazyList(List list, Factory factory) { protected LazyList(List<E> list, Factory<? extends E> factory) {
super(list); super(list);
if (factory == null) { if (factory == null) {
throw new IllegalArgumentException("Factory must not be null"); throw new IllegalArgumentException("Factory must not be null");
@ -107,36 +107,33 @@ public class LazyList extends AbstractSerializableListDecorator {
* *
* @param index the index to retrieve * @param index the index to retrieve
*/ */
public Object get(int index) { public E get(int index) {
int size = getList().size(); int size = decorated().size();
if (index < size) { if (index < size) {
// within bounds, get the object // within bounds, get the object
Object object = getList().get(index); E object = decorated().get(index);
if (object == null) { if (object == null) {
// item is a place holder, create new one, set and return // item is a place holder, create new one, set and return
object = factory.create(); object = factory.create();
getList().set(index, object); decorated().set(index, object);
return object; return object;
} else { }
// good and ready to go // good and ready to go
return object; return object;
} }
} else {
// we have to grow the list // we have to grow the list
for (int i = size; i < index; i++) { for (int i = size; i < index; i++) {
getList().add(null); decorated().add(null);
} }
// create our last object, set and return // create our last object, set and return
Object object = factory.create(); E object = factory.create();
getList().add(object); decorated().add(object);
return object; return object;
} }
}
public List<E> subList(int fromIndex, int toIndex) {
public List subList(int fromIndex, int toIndex) { List<E> sub = decorated().subList(fromIndex, toIndex);
List sub = getList().subList(fromIndex, toIndex); return new LazyList<E>(sub, factory);
return new LazyList(sub, factory);
} }
} }