1 4 5 package polyglot.util; 6 7 8 import java.util.*; 9 10 24 public class TypedList implements List, java.io.Serializable 25 { 26 static final long serialVersionUID = -1390984392613203018L; 27 28 36 public static TypedList copy(List list, Class c, boolean immutable) { 37 return new TypedList(new ArrayList(list), c, immutable); 38 } 39 40 49 public static TypedList copyAndCheck(List list, Class c, boolean immutable) { 50 if (c != null) 51 check(list,c); 52 return copy(list,c,immutable); 53 } 54 55 59 public static void check(List list, Class c) { 60 for (Iterator i = list.iterator(); i.hasNext(); ) { 61 Object o = i.next(); 62 if (o != null && !c.isAssignableFrom(o.getClass())) { 63 throw new UnsupportedOperationException ( 64 "Tried to add a " + o.getClass().getName() + 65 " to a list of type " + c.getName()); 66 } 67 } 68 } 69 70 79 public TypedList(List list, Class c, boolean immutable) { 80 this.immutable = immutable; 81 this.allowed_type = c; 82 this.backing_list = list; 83 } 84 85 88 public Class getAllowedType(){ 89 return allowed_type; 90 } 91 92 95 public TypedList copy() { 96 return (TypedList) clone(); 97 } 98 public Object clone() { 99 try { 100 TypedList l = (TypedList) super.clone(); 101 l.backing_list = new ArrayList(backing_list); 102 return l; 103 } 104 catch (CloneNotSupportedException e) { 105 throw new InternalCompilerError("Java clone weirdness."); 106 } 107 } 108 109 public void add(int idx, Object o) { 110 tryIns(o); 111 backing_list.add(idx,o); 112 } 113 public boolean add(Object o) { 114 tryIns(o); 115 return backing_list.add(o); 116 } 117 public boolean addAll(int idx, Collection coll) { 118 tryIns(coll); 119 return backing_list.addAll(idx, coll); 120 } 121 public boolean addAll(Collection coll) { 122 tryIns(coll); 123 return backing_list.addAll(coll); 124 } 125 public ListIterator listIterator() { 126 return new TypedListIterator(backing_list.listIterator(), 127 allowed_type, 128 immutable); 129 } 130 public ListIterator listIterator(int idx) { 131 return new TypedListIterator(backing_list.listIterator(idx), 132 allowed_type, 133 immutable); 134 } 135 public Object set(int idx, Object o) { 136 tryIns(o); 137 return backing_list.set(idx, o); 138 } 139 public List subList(int from, int to) { 140 return new TypedList(backing_list.subList(from, to), 141 allowed_type, 142 immutable); 143 } 144 public void clear() 145 { tryMod(); backing_list.clear(); } 146 public boolean contains(Object o) 147 { return backing_list.contains(o); } 148 public boolean containsAll(Collection coll) 149 { return backing_list.containsAll(coll); } 150 public boolean equals(Object o) 151 { return backing_list.equals(o); } 152 public Object get(int idx) 153 { return backing_list.get(idx); } 154 public int hashCode() 155 { return backing_list.hashCode(); } 156 public int indexOf(Object o) 157 { return backing_list.indexOf(o); } 158 public boolean isEmpty() 159 { return backing_list.isEmpty(); } 160 public Iterator iterator() 161 { return listIterator(); } 162 public int lastIndexOf(Object o) 163 { return backing_list.lastIndexOf(o); } 164 public Object remove(int idx) 165 { tryMod(); return backing_list.remove(idx); } 166 public boolean remove(Object o) 167 { tryMod(); return backing_list.remove(o); } 168 public boolean removeAll(Collection coll) 169 { tryMod(); return backing_list.removeAll(coll); } 170 public boolean retainAll(Collection coll) 171 { tryMod(); return backing_list.retainAll(coll); } 172 public int size() 173 { return backing_list.size(); } 174 public Object [] toArray() 175 { return backing_list.toArray(); } 176 public Object [] toArray(Object [] oa) 177 { return backing_list.toArray(oa); } 178 public String toString() 179 { return backing_list.toString(); } 180 181 private final void tryIns(Object o) { 182 if (immutable) 183 throw new UnsupportedOperationException ( 184 "Add to an immutable TypedListIterator"); 185 186 if (allowed_type != null && 187 !allowed_type.isAssignableFrom(o.getClass())) { 188 String why = "Tried to add a " + o.getClass().getName() + 189 " to a list of type " + allowed_type.getName(); 190 throw new UnsupportedOperationException (why); 191 } 192 } 193 194 private final void tryIns(Collection coll) { 195 if (immutable) 196 throw new UnsupportedOperationException ( 197 "Add to an immutable TypedListIterator"); 198 199 for (Iterator it = coll.iterator(); it.hasNext(); ) { 200 Object o = it.next(); 201 if (allowed_type != null && 202 !allowed_type.isAssignableFrom(o.getClass())) { 203 String why = "Tried to add a " + o.getClass().getName() + 204 " to a list of type " + allowed_type.getName(); 205 throw new UnsupportedOperationException (why); 206 } 207 } 208 } 209 210 private final void tryMod() { 211 if (immutable) 212 throw new UnsupportedOperationException ( 213 "Change to an immutable TypedListIterator"); 214 } 215 216 private Class allowed_type; 218 private boolean immutable; 219 private List backing_list; 220 } 221 222 | Popular Tags |