View Javadoc

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