KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > util > TypedList


1 /*
2  * TypedList.java
3  */

4
5 package polyglot.util;
6
7
8 import java.util.*;
9
10 /**
11  * A TypedList is an List which will not allow members not belonging
12  * to a given type to be added to a collection. Optionally, it may
13  * also present an immutable view.
14  *
15  * If an attempt is made to change an immutable list, or if an attempt
16  * is made to insert an improperly-typed element, an
17  * UnsupportedOperationException is thrown.
18  *
19  * This class is given so that we can present a List for a given class
20  * without worrying about outsiders breaking the rep.
21  *
22  * This is a poor substitute for PolyJ.
23  **/

24 public class TypedList implements List, java.io.Serializable JavaDoc
25 {
26   static final long serialVersionUID = -1390984392613203018L;
27
28   /**
29    * Requires: <list> not null, and every element of <list> may be
30    * cast to class <c>.
31    * Creates a new TypedList, containing all the elements of <list>,
32    * which restricts all members to belong to class <c>. If <c> is
33    * null, no typing restriction is made. If <immutable> is true, no
34    * modifications are allowed.
35    **/

36   public static TypedList copy(List list, Class JavaDoc c, boolean immutable) {
37     return new TypedList(new ArrayList(list), c, immutable);
38   }
39
40   /**
41    * Creates a new TypedList, containing all the elements of <list>,
42    * which restricts all members to belong to class <c>. If <c> is
43    * null, no typing restriction is made. If <immutable> is true, no
44    * modifications are allowed.
45    *
46    * Throws an UnsupportedOperationException if any member of <list>
47    * may not be cast to class <c>.
48    **/

49   public static TypedList copyAndCheck(List list, Class JavaDoc c, boolean immutable) {
50     if (c != null)
51       check(list,c);
52     return copy(list,c,immutable);
53   }
54
55   /**
56    * Throws an UnsupportedOperationException if any member of <list>
57    * may not be cast to class <c>. Otherwise does nothing.
58    **/

59   public static void check(List list, Class JavaDoc c) {
60     for (Iterator i = list.iterator(); i.hasNext(); ) {
61       Object JavaDoc o = i.next();
62       if (o != null && !c.isAssignableFrom(o.getClass())) {
63     throw new UnsupportedOperationException JavaDoc(
64              "Tried to add a " + o.getClass().getName() +
65                  " to a list of type " + c.getName());
66       }
67     }
68   }
69
70   /**
71    * Requires: <list> not null, and every element of <list> may be
72    * cast to class <c>.
73    * Effects:
74    * Creates a new TypedList around <list> which restricts all
75    * members to belong to class <c>. If <c> is null, no typing
76    * restriction is made. If <immutable> is true, no modifications
77    * are allowed.
78    **/

79   public TypedList(List list, Class JavaDoc c, boolean immutable) {
80     this.immutable = immutable;
81     this.allowed_type = c;
82     this.backing_list = list;
83   }
84
85   /**
86    * Gets the allowed type for this list.
87    **/

88   public Class JavaDoc getAllowedType(){
89     return allowed_type;
90   }
91
92   /**
93    * Copies this list.
94    **/

95   public TypedList copy() {
96       return (TypedList) clone();
97   }
98   public Object JavaDoc clone() {
99       try {
100           TypedList l = (TypedList) super.clone();
101           l.backing_list = new ArrayList(backing_list);
102           return l;
103       }
104       catch (CloneNotSupportedException JavaDoc e) {
105           throw new InternalCompilerError("Java clone weirdness.");
106       }
107   }
108
109   public void add(int idx, Object JavaDoc o) {
110     tryIns(o);
111     backing_list.add(idx,o);
112   }
113   public boolean add(Object JavaDoc 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 JavaDoc set(int idx, Object JavaDoc 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 JavaDoc o)
147     { return backing_list.contains(o); }
148   public boolean containsAll(Collection coll)
149     { return backing_list.containsAll(coll); }
150   public boolean equals(Object JavaDoc o)
151     { return backing_list.equals(o); }
152   public Object JavaDoc get(int idx)
153     { return backing_list.get(idx); }
154   public int hashCode()
155     { return backing_list.hashCode(); }
156   public int indexOf(Object JavaDoc 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 JavaDoc o)
163     { return backing_list.lastIndexOf(o); }
164   public Object JavaDoc remove(int idx)
165     { tryMod(); return backing_list.remove(idx); }
166   public boolean remove(Object JavaDoc 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 JavaDoc[] toArray()
175     { return backing_list.toArray(); }
176   public Object JavaDoc[] toArray(Object JavaDoc[] oa)
177     { return backing_list.toArray(oa); }
178   public String JavaDoc toString()
179     { return backing_list.toString(); }
180
181   private final void tryIns(Object JavaDoc o) {
182     if (immutable)
183       throw new UnsupportedOperationException JavaDoc(
184                      "Add to an immutable TypedListIterator");
185     
186     if (allowed_type != null &&
187     !allowed_type.isAssignableFrom(o.getClass())) {
188       String JavaDoc why = "Tried to add a " + o.getClass().getName() +
189     " to a list of type " + allowed_type.getName();
190       throw new UnsupportedOperationException JavaDoc(why);
191     }
192   }
193
194   private final void tryIns(Collection coll) {
195     if (immutable)
196       throw new UnsupportedOperationException JavaDoc(
197                      "Add to an immutable TypedListIterator");
198     
199     for (Iterator it = coll.iterator(); it.hasNext(); ) {
200       Object JavaDoc o = it.next();
201       if (allowed_type != null &&
202       !allowed_type.isAssignableFrom(o.getClass())) {
203     String JavaDoc why = "Tried to add a " + o.getClass().getName() +
204       " to a list of type " + allowed_type.getName();
205     throw new UnsupportedOperationException JavaDoc(why);
206       }
207     }
208   }
209
210   private final void tryMod() {
211     if (immutable)
212       throw new UnsupportedOperationException JavaDoc(
213                      "Change to an immutable TypedListIterator");
214   }
215
216   // RI: allowed_type may be null.
217
private Class JavaDoc allowed_type;
218   private boolean immutable;
219   private List backing_list;
220 }
221
222
Popular Tags