01: /*
02: * @(#)FilterIterator.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.lib;
10:
11: import java.util.*;
12:
13: class FilterIterator implements Iterator {
14: private Iterator it;
15: private Object next;
16: private boolean end;
17: private boolean needToFindNext = true;
18:
19: public FilterIterator() {
20: }
21:
22: public FilterIterator(Iterator it) {
23: this .it = it;
24: }
25:
26: protected boolean shouldInclude(Object obj) {
27: return true;
28: }
29:
30: protected boolean findNext() {
31: while (it.hasNext()) {
32: Object n = it.next();
33: if (shouldInclude(n)) {
34: next = n;
35: return true;
36: }
37: }
38: this .end = true;
39: return false;
40: }
41:
42: public boolean hasNext() {
43: if (needToFindNext) {
44: findNext();
45: this .needToFindNext = false;
46: }
47: return !end;
48: }
49:
50: public Object next() {
51: if (!hasNext()) {
52: throw new NoSuchElementException();
53: }
54: this .needToFindNext = true;
55: return next;
56: }
57:
58: public void remove() {
59: throw new UnsupportedOperationException();
60: }
61: }
|