KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > bytecode > VarEnumerator


1 // Copyright (c) 1997 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.bytecode;
5
6 /** Use this Enuemration class to iterate over the Variables in a Scope.
7  * Descends into child scopes.
8  * @author Per Bothner <bothner@cygnus.com>
9  */

10
11 public class VarEnumerator implements java.util.Enumeration JavaDoc
12 {
13   Scope topScope;
14   Scope currentScope;
15   Variable next;
16
17   public VarEnumerator (Scope scope)
18   {
19     topScope = scope;
20     reset ();
21   }
22
23   public final void reset ()
24   {
25     currentScope = topScope;
26     if (topScope != null)
27       {
28     next = currentScope.firstVar ();
29     if (next == null)
30       fixup ();
31       }
32   }
33
34   private void fixup ()
35   {
36     while (next == null)
37       {
38     if (currentScope.firstChild != null)
39       currentScope = currentScope.firstChild;
40     else
41       {
42         while (currentScope.nextSibling == null)
43           {
44         if (currentScope == topScope)
45           return;
46         currentScope = currentScope.parent;
47           }
48         currentScope = currentScope.nextSibling;
49       }
50     next = currentScope.firstVar ();
51       }
52   }
53
54   /** Return the next Variable in the Scope tree, or null if done. */
55   public final Variable nextVar ()
56   {
57     Variable result = next;
58     if (result != null)
59       {
60     next = result.nextVar ();
61     if (next == null)
62       fixup ();
63       }
64     return result;
65   }
66
67   public final boolean hasMoreElements ()
68   {
69     return next != null;
70   }
71
72   public Object JavaDoc nextElement ()
73   {
74     Variable result = nextVar ();
75     if (result == null)
76       throw new java.util.NoSuchElementException JavaDoc("VarEnumerator");
77     return result;
78   }
79
80
81   
82 }
83
Popular Tags