KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > util > TypedListIterator


1 /*
2  * TypedIterator.java
3  */

4
5 package polyglot.util;
6
7 import java.util.ListIterator JavaDoc;
8 import java.lang.UnsupportedOperationException JavaDoc;
9
10 /**
11  * A TypedListIterator is an ListIterator which will not allow members
12  * not belonging to a given type to be added to a collection.
13  * Optionally, it may also present an immutable view.
14  *
15  * If an attempt is made to change an immutable listiterator, or if
16  * an attempt 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 ListIterator for a
20  * given given class without worrying about outsiders breaking the
21  * rep.
22  *
23  * This is a poor substitute for PolyJ.
24  **/

25 public class TypedListIterator implements ListIterator JavaDoc {
26   /**
27    * Requires: <iter> not null
28    * Creates a new TypedIterator around <iter> which restricts all
29    * members to belong to class <c>. If <c> is null, no typing
30    * restriction is made. If <immutable> is true, no modifications
31    * are allowed.
32    **/

33   public TypedListIterator(ListIterator JavaDoc iter, Class JavaDoc c, boolean immutable) {
34     this.immutable = immutable;
35     this.allowed_type = c;
36     this.backing_iterator = iter;
37   }
38
39   /**
40    * Gets the allowed type for this ListIterator.
41    **/

42   public Class JavaDoc getAllowedType(){
43     return allowed_type;
44   }
45
46   public void add(Object JavaDoc o) {
47     tryIns(o);
48     backing_iterator.add(o);
49   }
50
51   public void set(Object JavaDoc o) {
52     tryIns(o);
53     backing_iterator.set(o);
54   }
55
56   public boolean hasNext() { return backing_iterator.hasNext(); }
57   public boolean hasPrevious() { return backing_iterator.hasPrevious(); }
58   public Object JavaDoc next() { return backing_iterator.next(); }
59   public int nextIndex() { return backing_iterator.nextIndex(); }
60   public Object JavaDoc previous() { return backing_iterator.previous(); }
61   public int previousIndex() { return backing_iterator.previousIndex(); }
62   public void remove() {
63     if (immutable)
64       throw new UnsupportedOperationException JavaDoc(
65                      "Remove from an immutable TypedListIterator");
66     
67     backing_iterator.remove();
68   }
69
70   private final void tryIns(Object JavaDoc o) {
71     if (immutable)
72       throw new UnsupportedOperationException JavaDoc(
73                      "Add to an immutable TypedListIterator");
74     
75     if (allowed_type != null &&
76     !allowed_type.isAssignableFrom(o.getClass())) {
77       String JavaDoc why = "Tried to add a " + o.getClass().getName() +
78     " to a list of type " + allowed_type.getName();
79       throw new UnsupportedOperationException JavaDoc(why);
80     }
81   }
82
83   // RI: allowed_type may be null.
84
private Class JavaDoc allowed_type;
85   private boolean immutable;
86   private ListIterator JavaDoc backing_iterator;
87 }
88
89
Popular Tags