1 /**
2 * This class is based on the Iterators.FilterIterator class from
3 * araSpect (araspect.sourceforge.net). The original copyright follows ...
4 *
5 * =================================================================
6 * Copyright (c) 2001,2002 aragost ag, Z�rich, Switzerland.
7 * All rights reserved.
8 *
9 * This software is provided 'as-is', without any express or implied
10 * warranty. In no event will the authors be held liable for any
11 * damages arising from the use of this software.
12 *
13 * Permission is granted to anyone to use this software for any
14 * purpose, including commercial applications, and to alter it and
15 * redistribute it freely, subject to the following restrictions:
16 *
17 * 1. The origin of this software must not be misrepresented; you
18 * must not claim that you wrote the original software. If you
19 * use this software in a product, an acknowledgment in the
20 * product documentation would be appreciated but is not required.
21 *
22 * 2. Altered source versions must be plainly marked as such, and
23 * must not be misrepresented as being the original software.
24 *
25 * 3. This notice may not be removed or altered from any source
26 * distribution.
27 *
28 * ==================================================================
29 *
30 * Changes (c) 2003 University Health Network include the following:
31 * - move to non-nested class
32 * - collapse inherited method remove()
33 * - accept iterator instead of object in constructor
34 * - moved to HAPI package
35 * - Predicate added as an inner class; also changed to an interface
36 *
37 * These changes are distributed under the same terms as the original (above).
38 */
39 package ca.uhn.hl7v2.util;
40
41 import java.util.*;
42
43 public class FilterIterator<T> implements Iterator<T> {
44
45 private final Predicate<T> predicate;
46 private final Iterator<T> iter;
47 private T nextObject;
48 private boolean nextObjectSet = false;
49
50 public FilterIterator(Iterator<T> iter, Predicate<T> predicate) {
51 this.iter = iter;
52 this.predicate = predicate;
53 }
54
55 public boolean hasNext() {
56 if (nextObjectSet) {
57 return true;
58 } else {
59 return setNextObject();
60 }
61 }
62
63 public T next() {
64 if (!nextObjectSet) {
65 if (!setNextObject()) {
66 throw new NoSuchElementException();
67 }
68 }
69 nextObjectSet = false;
70 return nextObject;
71 }
72
73 /**
74 * Set nextObject to the next object. If there are no more
75 * objects then return false. Otherwise, return true.
76 */
77 private boolean setNextObject() {
78 while (iter.hasNext()) {
79 T object = iter.next();
80 if (predicate.evaluate(object)) {
81 nextObject = object;
82 nextObjectSet = true;
83 return true;
84 }
85 }
86 return false;
87 }
88
89 /** Throws UnsupportedOperationException */
90 public void remove() {
91 throw new UnsupportedOperationException();
92 }
93
94 /**
95 * Interface for evaluating whether an object should be returned by the iterator
96 */
97 public interface Predicate<T> {
98 boolean evaluate(T obj);
99 }
100
101 }