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:/**
37: * Enumeration which transforms the elements of another enumeration.
38: */
39:public abstract class FilteredEnumeration implements Enumeration {
40: Enumeration e;
41: Object o; // first object waiting to be returned, or null if none
42: Vector v; // other objects yielded and waiting to be returned
43: int i; // next object to return from v
44:
45: public FilteredEnumeration (Enumeration e) {
46: this .e = e;
47: }
48:
49: public boolean hasMoreElements () {
50: next ();
51: return o != null;
52: }
53:
54: public Object nextElement () {
55: next ();
56: if (o == null)
57: throw new NoSuchElementException ();
58: Object result = o;
59: o = null;
60: return result;
61: }
62:
63: void next () {
64: // check if yielded element is waiting to be returned
65: if (o != null)
66: return;
67:
68: // check in v for other yielded elements
69: if (v != null) {
70: if (i < v.size ()) {
71: o = v.elementAt (i);
72: v.setElementAt (null, i);
73: ++i;
74: } else {
75: v.setSize (0);
76: i = 0;
77: }
78: }
79:
80: // transform elements until an element is yielded
81: while (o == null && e.hasMoreElements ())
82: transform (e.nextElement ());
83: }
84:
85: public void yield (Object obj) {
86: if (o == null)
87: o = obj;
88: else {
89: if (v == null)
90: v = new Vector ();
91: v.addElement (obj);
92: }
93: }
94:
95: public abstract void transform (Object o);
96:}
|