1 22 package org.jboss.util.collection; 23 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.ListIterator ; 30 31 37 public class LazyList implements List 38 { 39 40 private List delegate = Collections.EMPTY_LIST; 41 42 public void add(int index, Object element) 43 { 44 if (delegate instanceof ArrayList == false) 45 delegate = new ArrayList (delegate); 46 delegate.add(index, element); 47 } 48 49 public boolean add(Object o) 50 { 51 if (delegate == Collections.EMPTY_LIST) 52 { 53 delegate = Collections.singletonList(o); 54 return true; 55 } 56 else 57 { 58 if (delegate instanceof ArrayList == false) 59 delegate = new ArrayList (delegate); 60 return delegate.add(o); 61 } 62 } 63 64 public boolean addAll(Collection c) 65 { 66 if (delegate instanceof ArrayList == false) 67 delegate = new ArrayList (delegate); 68 return delegate.addAll(c); 69 } 70 71 public boolean addAll(int index, Collection c) 72 { 73 if (delegate instanceof ArrayList == false) 74 delegate = new ArrayList (delegate); 75 return delegate.addAll(index, c); 76 } 77 78 public void clear() 79 { 80 delegate = Collections.EMPTY_LIST; 81 } 82 83 public boolean contains(Object o) 84 { 85 return delegate.contains(o); 86 } 87 88 public boolean containsAll(Collection c) 89 { 90 return delegate.containsAll(c); 91 } 92 93 public Object get(int index) 94 { 95 return delegate.get(index); 96 } 97 98 public int indexOf(Object o) 99 { 100 return delegate.indexOf(o); 101 } 102 103 public boolean isEmpty() 104 { 105 return delegate.isEmpty(); 106 } 107 108 public Iterator iterator() 109 { 110 return delegate.iterator(); 111 } 112 113 public int lastIndexOf(Object o) 114 { 115 return delegate.lastIndexOf(o); 116 } 117 118 public ListIterator listIterator() 119 { 120 return delegate.listIterator(); 121 } 122 123 public ListIterator listIterator(int index) 124 { 125 return delegate.listIterator(index); 126 } 127 128 public Object remove(int index) 129 { 130 if (delegate instanceof ArrayList == false) 131 delegate = new ArrayList (delegate); 132 return delegate.remove(index); 133 } 134 135 public boolean remove(Object o) 136 { 137 if (delegate instanceof ArrayList == false) 138 delegate = new ArrayList (delegate); 139 return delegate.remove(o); 140 } 141 142 public boolean removeAll(Collection c) 143 { 144 if (delegate instanceof ArrayList == false) 145 delegate = new ArrayList (delegate); 146 return delegate.remove(c); 147 } 148 149 public boolean retainAll(Collection c) 150 { 151 if (delegate instanceof ArrayList == false) 152 delegate = new ArrayList (delegate); 153 return delegate.retainAll(c); 154 } 155 156 public Object set(int index, Object element) 157 { 158 if (delegate instanceof ArrayList == false) 159 delegate = new ArrayList (delegate); 160 return delegate.set(index, element); 161 } 162 163 public int size() 164 { 165 return delegate.size(); 166 } 167 168 public List subList(int fromIndex, int toIndex) 169 { 170 return delegate.subList(fromIndex, toIndex); 171 } 172 173 public Object [] toArray() 174 { 175 return delegate.toArray(); 176 } 177 178 public Object [] toArray(Object [] a) 179 { 180 return delegate.toArray(a); 181 } 182 } 183 | Popular Tags |