This project has retired. For details please refer to its
Attic page.
FilterIterator xref
1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing,13 * software distributed under the License is distributed on an14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15 * KIND, either express or implied. See the License for the16 * specific language governing permissions and limitations17 * under the License.18 */1920package org.apache.chemistry.opencmis.jcr.util;
2122import java.util.Iterator;
23import java.util.NoSuchElementException;
2425/**26 * Iterator filtering out items which do not match a given predicate.27 * @param <T>28 */29publicclass FilterIterator<T> implements Iterator<T> {
30privatefinal Iterator<T> iterator;
31privatefinal Predicate<T> predicate;
3233private T next;
3435/**36 * Create a new filtered iterator based on the given <code>iterator</code>.37 *38 * @param iterator iterator to filter39 * @param predicate only item matching this predicate are included40 */41publicFilterIterator(Iterator<T> iterator, Predicate<T> predicate) {
42this.iterator = iterator;
43this.predicate = predicate;
44 }
4546publicboolean hasNext() {
47while (next == null && iterator.hasNext()) {
48 T e = iterator.next();
49if (predicate.evaluate(e)) {
50 next = e;
51 }
52 }
5354return next != null;
55 }
5657public T next() {
58if (hasNext()) {
59 T e = next;
60 next = null;
61return e;
62 }
63else {
64thrownew NoSuchElementException();
65 }
66 }
6768publicvoid remove() {
69thrownew UnsupportedOperationException();
70 }
7172 }