Source code: Compil3r/Quad/ExceptionHandlerIterator.java
1 // ExceptionHandlerIterator.java, created Fri Jan 11 16:42:38 2002 by joewhaley
2 // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3 // Licensed under the terms of the GNU LGPL; see COPYING for details.
4 package Compil3r.Quad;
5
6 import java.util.NoSuchElementException;
7
8 import Util.Templates.ListIterator;
9
10 /**
11 * Iterator for iterating through exception handlers. Compatible with ListIterator.
12 * @see Util.Templates.ListIterator
13 * @see ExceptionHandler
14 * @author John Whaley <jwhaley@alum.mit.edu>
15 * @version $Id: ExceptionHandlerIterator.java,v 1.8 2003/05/12 10:05:16 joewhaley Exp $
16 */
17 public class ExceptionHandlerIterator implements ListIterator.ExceptionHandler {
18
19 private final ExceptionHandlerList root;
20 private ExceptionHandlerList current;
21
22 /** Creates new ExceptionHandlerIterator.
23 * @param exs list of exception handlers to iterate through. */
24 public ExceptionHandlerIterator(ExceptionHandlerList ehs) {
25 root = current = ehs;
26 }
27
28 /** Returns true if this iterator has a next element.
29 * @return true if this iterator has a next element. */
30 public boolean hasNext() { return current != null; }
31 /** Returns the next element of this iterator. Use nextExceptionHandler to avoid the cast.
32 * @see #nextExceptionHandler
33 * @return the next element of this iterator. */
34 public Object next() { return nextExceptionHandler(); }
35 /** Returns the next element of this iterator, avoiding the cast.
36 * @see #next
37 * @return the next element of this iterator. */
38 public ExceptionHandler nextExceptionHandler() {
39 if (current == null) throw new NoSuchElementException();
40 ExceptionHandler x = current.getHandler();
41 current = current.getParent();
42 return x;
43 }
44 /** Returns the index of the next element of this iterator.
45 * @return the index of the next element of this iterator. */
46 public int nextIndex() {
47 int i=0; ExceptionHandlerList p = root;
48 while (p != current) {
49 ++i; p = p.getParent();
50 }
51 return i;
52 }
53
54 /** Returns true if this iterator has a previous element.
55 * @return true if this iterator has a previous element. */
56 public boolean hasPrevious() { return root != current; }
57 /** Returns the previous element of this iterator. Use previousExceptionHandler to avoid the cast.
58 * @see #previousExceptionHandler
59 * @return the previous element of this iterator. */
60 public Object previous() { return previousExceptionHandler(); }
61 /** Returns the previous element of this iterator, avoiding the cast.
62 * @see #previous
63 * @return the previous element of this iterator. */
64 public ExceptionHandler previousExceptionHandler() {
65 if (root == current) throw new NoSuchElementException();
66 ExceptionHandlerList p = root;
67 ExceptionHandlerList q = p.getParent();
68 while (q != current) {
69 p = q;
70 q = q.getParent();
71 }
72 return p.getHandler();
73 }
74 /** Returns the index of the previous element of this iterator.
75 * @return the index of the previous element of this iterator. */
76 public int previousIndex() { return nextIndex()-1; }
77 /** Throws UnsupportedOperationException. (Removing is not supported.)
78 * @throws UnsupportedOperationException always
79 */
80
81 public void remove() { throw new UnsupportedOperationException(); }
82 /** Throws UnsupportedOperationException. (Setting is not supported.)
83 * @throws UnsupportedOperationException always
84 */
85 public void set(Object o) { throw new UnsupportedOperationException(); }
86 /** Throws UnsupportedOperationException. (Adding is not supported.)
87 * @throws UnsupportedOperationException always
88 */
89 public void add(Object o) { throw new UnsupportedOperationException(); }
90
91 /** Return an empty, unmodifiable iterator.
92 * @return an empty, unmodifiable iterator */
93 public static ExceptionHandlerIterator getEmptyIterator() { return EMPTY; }
94
95 /** The empty basic block iterator. Immutable. */
96 public static final ExceptionHandlerIterator EMPTY = new ExceptionHandlerIterator(null);
97 }