01: // Copyright (c) 1997 Per M.A. Bothner.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.bytecode;
05:
06: /** Use this Enuemration class to iterate over the Variables in a Scope.
07: * Descends into child scopes.
08: * @author Per Bothner <bothner@cygnus.com>
09: */
10:
11: public class VarEnumerator implements java.util.Enumeration {
12: Scope topScope;
13: Scope currentScope;
14: Variable next;
15:
16: public VarEnumerator(Scope scope) {
17: topScope = scope;
18: reset();
19: }
20:
21: public final void reset() {
22: currentScope = topScope;
23: if (topScope != null) {
24: next = currentScope.firstVar();
25: if (next == null)
26: fixup();
27: }
28: }
29:
30: private void fixup() {
31: while (next == null) {
32: if (currentScope.firstChild != null)
33: currentScope = currentScope.firstChild;
34: else {
35: while (currentScope.nextSibling == null) {
36: if (currentScope == topScope)
37: return;
38: currentScope = currentScope.parent;
39: }
40: currentScope = currentScope.nextSibling;
41: }
42: next = currentScope.firstVar();
43: }
44: }
45:
46: /** Return the next Variable in the Scope tree, or null if done. */
47: public final Variable nextVar() {
48: Variable result = next;
49: if (result != null) {
50: next = result.nextVar();
51: if (next == null)
52: fixup();
53: }
54: return result;
55: }
56:
57: public final boolean hasMoreElements() {
58: return next != null;
59: }
60:
61: public Object nextElement() {
62: Variable result = nextVar();
63: if (result == null)
64: throw new java.util.NoSuchElementException("VarEnumerator");
65: return result;
66: }
67:
68: }
|