01: package org.geotools.feature.iso.collection;
02:
03: import java.util.Iterator;
04: import java.util.NoSuchElementException;
05:
06: import org.opengis.feature.Feature;
07: import org.opengis.feature.FeatureCollection;
08: import org.opengis.filter.Filter;
09:
10: /**
11: * Provides an implementation of Iterator that will filter
12: * contents using the provided filter.
13: * <p>
14: * This is a *Generic* iterator not limited to Feature, this
15: * will become more interesting as Filter is able to evaulate
16: * itself with more things then just Features.
17: * </p>
18: * <p>
19: * This also explains the use of Collection (where you may
20: * have expected a FeatureCollection). However
21: * <code>FeatureCollectoin.close( iterator )</code> will be
22: * called on the internal delgate.
23: * </p>
24: *
25: * @author Jody Garnett, Refractions Research, Inc.
26: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/community-schemas/fm/src/main/java/org/geotools/feature/iso/collection/FilteredIterator.java $
27: */
28: public class FilteredIterator implements Iterator {
29: /** Used to close the delgate, or null */
30: FeatureCollection collection;
31: Iterator delegate;
32: Filter filter;
33:
34: private Object next;
35:
36: public FilteredIterator(Iterator iterator, Filter filter) {
37: this .collection = null;
38: this .delegate = iterator;
39: this .filter = filter;
40: }
41:
42: public FilteredIterator(FeatureCollection collection, Filter filter) {
43: this .collection = collection;
44: this .delegate = collection.iterator();
45: this .filter = filter;
46: next = getNext();
47: }
48:
49: /** Package protected, please use SubFeatureCollection.close( iterator ) */
50: void close() {
51: if (collection != null) {
52: collection.close(delegate);
53: }
54: collection = null;
55: delegate = null;
56: filter = null;
57: next = null;
58: }
59:
60: private Object getNext() {
61: Object item = null;
62: while (delegate.hasNext()) {
63: item = (Feature) delegate.next();
64: if (filter.evaluate((Feature) item)) {
65: return item;
66: }
67: }
68: return null;
69: }
70:
71: public boolean hasNext() {
72: return next != null;
73: }
74:
75: public Object next() {
76: if (next == null) {
77: throw new NoSuchElementException();
78: }
79: Object current = next;
80: next = getNext();
81: return current;
82: }
83:
84: public void remove() {
85: if (delegate == null)
86: throw new IllegalStateException();
87:
88: delegate.remove();
89: }
90: }
|