01:/*
02: * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
03: * reserved.
04: *
05: * Redistribution and use in source and binary forms, with or without
06: * modification, are permitted provided that the following conditions
07: * are met:
08: *
09: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: *
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in
14: * the documentation and/or other materials provided with the
15: * distribution.
16: *
17: * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
18: * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
21: * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28: *
29: */
30:
31:package rcm.enum;
32:import java.util.Enumeration;
33:import java.util.NoSuchElementException;
34:import java.util.Vector;
35:/**
36: * Enumeration which can be restarted from the beginning.
37: */
38:public class MemoizedEnumeration implements RestartableEnumeration {
39: Vector v; // elements which have already been returned
40: Enumeration e1; // enumeration of v
41: Enumeration e2; // main enumeration
42:
43: public MemoizedEnumeration (Enumeration e) {
44: this .v = new Vector ();
45: this .e2 = e;
46: }
47:
48: public MemoizedEnumeration (Vector v) {
49: this .v = (Vector) v.clone ();
50: this .e1 = this .v.elements ();
51: }
52:
53: public boolean hasMoreElements () {
54: if (e1 != null) {
55: if (e1.hasMoreElements ())
56: return true;
57: else
58: e1 = null;
59: }
60:
61: if (e2 != null) {
62: if (e2.hasMoreElements ())
63: return true;
64: else
65: e2 = null;
66: }
67:
68: return false;
69: }
70:
71: public Object nextElement () {
72: if (e1 != null)
73: try {
74: return e1.nextElement ();
75: } catch (NoSuchElementException e) {
76: e1 = null;
77: }
78:
79: if (e2 != null)
80: try {
81: Object o = e2.nextElement ();
82: v.addElement (o);
83: return o;
84: } catch (NoSuchElementException e) {
85: e2 = null;
86: }
87:
88: throw new NoSuchElementException ();
89: }
90:
91: public void restart () {
92: e1 = v.elements ();
93: }
94:}
|