01: /*
02: * Geotools2 - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: */
17: package org.geotools.data.feature;
18:
19: import java.util.Iterator;
20: import java.util.NoSuchElementException;
21:
22: import org.opengis.feature.Feature;
23: import org.opengis.filter.Filter;
24:
25: /**
26: * Decorates a {@link org.geotools.feature.Feature} iterator with one that
27: * filters content.
28: *
29: * <p>
30: * This class is a dumb copy of
31: * {@link org.geotools.data.store.FilteringIterator} by Justin Deoliveira, but
32: * meant to iterate over any kind of <code>Object</code> as it is possible to
33: * {@link Filter}, though the target usage is to iterate over ISO
34: * {@link Feature}s.
35: * </p>
36: *
37: * @author Gabriel Roldan, Axios Engineering
38: * @version $Id: FilteringIterator.java 25814 2007-06-12 12:03:41Z groldan $
39: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/community-schemas/community-schema-ds/src/main/java/org/geotools/data/feature/FilteringIterator.java $
40: * @since 2.4
41: */
42: public class FilteringIterator implements Iterator {
43:
44: /**
45: * Delegate iterator
46: */
47: Iterator delegate;
48:
49: /**
50: * The Filter
51: */
52: Filter filter;
53:
54: /**
55: * Next feature
56: */
57: Object next;
58:
59: public FilteringIterator(Iterator delegate, Filter filter) {
60: this .delegate = delegate;
61: this .filter = filter;
62: }
63:
64: public Iterator getDelegate() {
65: return delegate;
66: }
67:
68: public void remove() {
69: delegate.remove();
70: }
71:
72: public boolean hasNext() {
73: if (next != null) {
74: return true;
75: }
76:
77: while (delegate.hasNext()) {
78: Object peek = delegate.next();
79: if (filter.evaluate(peek)) {
80: next = peek;
81: break;
82: }
83: }
84:
85: return next != null;
86: }
87:
88: public Object next() {
89: if (!hasNext()) {
90: throw new NoSuchElementException(
91: "There are no more elements");
92: }
93: Object f = next;
94: next = null;
95: return f;
96: }
97:
98: }
|