1 4 5 package polyglot.util; 6 7 import java.util.ListIterator ; 8 import java.lang.UnsupportedOperationException ; 9 10 25 public class TypedListIterator implements ListIterator { 26 33 public TypedListIterator(ListIterator iter, Class c, boolean immutable) { 34 this.immutable = immutable; 35 this.allowed_type = c; 36 this.backing_iterator = iter; 37 } 38 39 42 public Class getAllowedType(){ 43 return allowed_type; 44 } 45 46 public void add(Object o) { 47 tryIns(o); 48 backing_iterator.add(o); 49 } 50 51 public void set(Object 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 next() { return backing_iterator.next(); } 59 public int nextIndex() { return backing_iterator.nextIndex(); } 60 public Object previous() { return backing_iterator.previous(); } 61 public int previousIndex() { return backing_iterator.previousIndex(); } 62 public void remove() { 63 if (immutable) 64 throw new UnsupportedOperationException ( 65 "Remove from an immutable TypedListIterator"); 66 67 backing_iterator.remove(); 68 } 69 70 private final void tryIns(Object o) { 71 if (immutable) 72 throw new UnsupportedOperationException ( 73 "Add to an immutable TypedListIterator"); 74 75 if (allowed_type != null && 76 !allowed_type.isAssignableFrom(o.getClass())) { 77 String why = "Tried to add a " + o.getClass().getName() + 78 " to a list of type " + allowed_type.getName(); 79 throw new UnsupportedOperationException (why); 80 } 81 } 82 83 private Class allowed_type; 85 private boolean immutable; 86 private ListIterator backing_iterator; 87 } 88 89 | Popular Tags |