1 16 17 package org.springframework.util; 18 19 import java.lang.reflect.Modifier ; 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.ListIterator ; 25 26 41 public class AutoPopulatingList implements List { 42 43 46 private final List backingList; 47 48 52 private final ElementFactory elementFactory; 53 54 55 60 public AutoPopulatingList(Class elementClass) { 61 this(new ArrayList (), elementClass); 62 } 63 64 69 public AutoPopulatingList(List backingList, Class elementClass) { 70 this(backingList, new ReflectiveElementFactory(elementClass)); 71 } 72 73 77 public AutoPopulatingList(ElementFactory elementFactory) { 78 this(new ArrayList (), elementFactory); 79 } 80 81 85 public AutoPopulatingList(List backingList, ElementFactory elementFactory) { 86 Assert.notNull(backingList, "Backing List must not be null"); 87 Assert.notNull(elementFactory, "Element factory must not be null"); 88 this.backingList = backingList; 89 this.elementFactory = elementFactory; 90 } 91 92 93 public void add(int index, Object element) { 94 this.backingList.add(index, element); 95 } 96 97 public boolean add(Object o) { 98 return this.backingList.add(o); 99 } 100 101 public boolean addAll(Collection c) { 102 return this.backingList.addAll(c); 103 } 104 105 public boolean addAll(int index, Collection c) { 106 return this.backingList.addAll(index, c); 107 } 108 109 public void clear() { 110 this.backingList.clear(); 111 } 112 113 public boolean contains(Object o) { 114 return this.backingList.contains(o); 115 } 116 117 public boolean containsAll(Collection c) { 118 return this.backingList.containsAll(c); 119 } 120 121 public boolean equals(Object o) { 122 return this.backingList.equals(o); 123 } 124 125 129 public Object get(int index) { 130 int backingListSize = this.backingList.size(); 131 132 Object element = null; 133 if (index < backingListSize) { 134 element = this.backingList.get(index); 135 if (element == null) { 136 element = this.elementFactory.createElement(index); 137 this.backingList.set(index, element); 138 } 139 } 140 else { 141 for (int x = backingListSize; x < index; x++) { 142 this.backingList.add(null); 143 } 144 element = this.elementFactory.createElement(index); 145 this.backingList.add(element); 146 } 147 return element; 148 } 149 150 public int hashCode() { 151 return this.backingList.hashCode(); 152 } 153 154 public int indexOf(Object o) { 155 return this.backingList.indexOf(o); 156 } 157 158 public boolean isEmpty() { 159 return this.backingList.isEmpty(); 160 } 161 162 public Iterator iterator() { 163 return this.backingList.iterator(); 164 } 165 166 public int lastIndexOf(Object o) { 167 return this.backingList.lastIndexOf(o); 168 } 169 170 public ListIterator listIterator() { 171 return this.backingList.listIterator(); 172 } 173 174 public ListIterator listIterator(int index) { 175 return this.backingList.listIterator(index); 176 } 177 178 public Object remove(int index) { 179 return this.backingList.remove(index); 180 } 181 182 public boolean remove(Object o) { 183 return this.backingList.remove(o); 184 } 185 186 public boolean removeAll(Collection c) { 187 return this.backingList.removeAll(c); 188 } 189 190 public boolean retainAll(Collection c) { 191 return this.backingList.retainAll(c); 192 } 193 194 public Object set(int index, Object element) { 195 return this.backingList.set(index, element); 196 } 197 198 public int size() { 199 return this.backingList.size(); 200 } 201 202 public List subList(int fromIndex, int toIndex) { 203 return this.backingList.subList(fromIndex, toIndex); 204 } 205 206 public Object [] toArray() { 207 return this.backingList.toArray(); 208 } 209 210 public Object [] toArray(Object [] a) { 211 return this.backingList.toArray(a); 212 } 213 214 215 219 public interface ElementFactory { 220 221 227 Object createElement(int index) throws ElementInstantiationException; 228 } 229 230 231 234 public static class ElementInstantiationException extends RuntimeException { 235 236 public ElementInstantiationException(String msg) { 237 super(msg); 238 } 239 } 240 241 242 247 private static class ReflectiveElementFactory implements ElementFactory { 248 249 private final Class elementClass; 250 251 public ReflectiveElementFactory(Class elementClass) { 252 Assert.notNull(elementClass, "Element clas must not be null"); 253 Assert.isTrue(!elementClass.isInterface(), "Element class must not be an interface type"); 254 Assert.isTrue(!Modifier.isAbstract(elementClass.getModifiers()), "Element class cannot be an abstract class"); 255 this.elementClass = elementClass; 256 } 257 258 public Object createElement(int index) { 259 try { 260 return this.elementClass.newInstance(); 261 } 262 catch (InstantiationException ex) { 263 throw new ElementInstantiationException("Unable to instantiate element class [" + 264 this.elementClass.getName() + "]. Root cause is " + ex); 265 } 266 catch (IllegalAccessException ex) { 267 throw new ElementInstantiationException("Cannot access element class [" + 268 this.elementClass.getName() + "]. Root cause is " + ex); 269 } 270 } 271 } 272 273 } 274 | Popular Tags |