1 16 package org.apache.commons.collections.list; 17 18 import java.util.List ; 19 20 import org.apache.commons.collections.Factory; 21 22 58 public class LazyList extends AbstractSerializableListDecorator { 59 60 61 private static final long serialVersionUID = -1708388017160694542L; 62 63 64 protected final Factory factory; 65 66 73 public static List decorate(List list, Factory factory) { 74 return new LazyList(list, factory); 75 } 76 77 85 protected LazyList(List list, Factory factory) { 86 super(list); 87 if (factory == null) { 88 throw new IllegalArgumentException ("Factory must not be null"); 89 } 90 this.factory = factory; 91 } 92 93 104 public Object get(int index) { 105 int size = getList().size(); 106 if (index < size) { 107 Object object = getList().get(index); 109 if (object == null) { 110 object = factory.create(); 112 getList().set(index, object); 113 return object; 114 } else { 115 return object; 117 } 118 } else { 119 for (int i = size; i < index; i++) { 121 getList().add(null); 122 } 123 Object object = factory.create(); 125 getList().add(object); 126 return object; 127 } 128 } 129 130 131 public List subList(int fromIndex, int toIndex) { 132 List sub = getList().subList(fromIndex, toIndex); 133 return new LazyList(sub, factory); 134 } 135 136 } 137 | Popular Tags |