| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| FilterIterator |
|
| 2.5;2.5 | ||||
| FilterIterator$Predicate |
|
| 2.5;2.5 |
| 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 Predicate<T> predicate; | |
| 46 | private Iterator<T> iter; | |
| 47 | private T nextObject; | |
| 48 | 175 | private boolean nextObjectSet = false; |
| 49 | ||
| 50 | 175 | public FilterIterator(Iterator<T> iter, Predicate<T> predicate) { |
| 51 | 175 | this.iter = iter; |
| 52 | 175 | this.predicate = predicate; |
| 53 | 175 | } |
| 54 | ||
| 55 | public boolean hasNext() { | |
| 56 | 340 | if (nextObjectSet) { |
| 57 | 0 | return true; |
| 58 | } else { | |
| 59 | 340 | return setNextObject(); |
| 60 | } | |
| 61 | } | |
| 62 | ||
| 63 | public T next() { | |
| 64 | 370 | if (!nextObjectSet) { |
| 65 | 70 | if (!setNextObject()) { |
| 66 | 0 | throw new NoSuchElementException(); |
| 67 | } | |
| 68 | } | |
| 69 | 370 | nextObjectSet = false; |
| 70 | 370 | 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 | 925 | while (iter.hasNext()) { |
| 79 | 885 | T object = iter.next(); |
| 80 | 885 | if (predicate.evaluate(object)) { |
| 81 | 370 | nextObject = object; |
| 82 | 370 | nextObjectSet = true; |
| 83 | 370 | return true; |
| 84 | } | |
| 85 | 515 | } |
| 86 | 40 | return false; |
| 87 | } | |
| 88 | ||
| 89 | /** Throws UnsupportedOperationException */ | |
| 90 | public void remove() { | |
| 91 | 0 | 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 | public boolean evaluate(T obj); | |
| 99 | } | |
| 100 | ||
| 101 | } |