001: package org.geotools.data.store;
002:
003: import java.io.IOException;
004: import java.util.ArrayList;
005: import java.util.Collection;
006: import java.util.Iterator;
007: import java.util.List;
008:
009: import org.geotools.data.DataUtilities;
010: import org.geotools.data.FeatureReader;
011: import org.geotools.data.collection.DelegateFeatureReader;
012: import org.geotools.feature.CollectionListener;
013: import org.geotools.feature.Feature;
014: import org.geotools.feature.FeatureCollection;
015: import org.geotools.feature.FeatureCollections;
016: import org.geotools.feature.FeatureIterator;
017: import org.geotools.feature.FeatureList;
018: import org.geotools.feature.FeatureType;
019: import org.geotools.feature.IllegalAttributeException;
020: import org.geotools.feature.collection.DelegateFeatureIterator;
021: import org.geotools.feature.visitor.FeatureVisitor;
022: import org.geotools.geometry.jts.ReferencedEnvelope;
023: import org.geotools.util.ProgressListener;
024: import org.opengis.filter.Filter;
025: import org.opengis.filter.sort.SortBy;
026:
027: import com.vividsolutions.jts.geom.Envelope;
028: import com.vividsolutions.jts.geom.Geometry;
029:
030: /**
031: * Decorates a feature collection with one that filters content.
032: *
033: * @author Justin Deoliveira, The Open Planning Project
034: *
035: */
036: public class FilteringFeatureCollection implements FeatureCollection {
037:
038: /**
039: * The original feature collection.
040: */
041: FeatureCollection delegate;
042: /**
043: * the filter
044: */
045: Filter filter;
046:
047: public FilteringFeatureCollection(FeatureCollection delegate,
048: Filter filter) {
049: this .delegate = delegate;
050: this .filter = filter;
051: }
052:
053: public FeatureIterator features() {
054: return new DelegateFeatureIterator(this , iterator());
055: }
056:
057: public void close(FeatureIterator close) {
058: close.close();
059: }
060:
061: public Iterator iterator() {
062: return new FilteringIterator(delegate.iterator(), filter);
063: }
064:
065: public void close(Iterator close) {
066: FilteringIterator filtering = (FilteringIterator) close;
067: delegate.close(filtering.getDelegate());
068: }
069:
070: public void addListener(CollectionListener listener)
071: throws NullPointerException {
072: delegate.addListener(listener);
073: }
074:
075: public void removeListener(CollectionListener listener)
076: throws NullPointerException {
077: delegate.removeListener(listener);
078: }
079:
080: public FeatureType getFeatureType() {
081: return delegate.getFeatureType();
082: }
083:
084: public FeatureType getSchema() {
085: return delegate.getSchema();
086: }
087:
088: public void accepts(FeatureVisitor visitor,
089: ProgressListener progress) throws IOException {
090: delegate.accepts(visitor, progress);
091: }
092:
093: public FeatureCollection subCollection(Filter filter) {
094: throw new UnsupportedOperationException();
095: }
096:
097: public FeatureList sort(SortBy order) {
098: throw new UnsupportedOperationException();
099: }
100:
101: public void purge() {
102: delegate.purge();
103: }
104:
105: public int size() {
106: int count = 0;
107: Iterator i = iterator();
108: try {
109: while (i.hasNext()) {
110: count++;
111: i.next();
112: }
113:
114: return count;
115: } finally {
116: close(i);
117: }
118: }
119:
120: public void clear() {
121: delegate.clear();
122: }
123:
124: public boolean isEmpty() {
125: return size() == 0;
126: }
127:
128: public Object[] toArray() {
129: return toArray(new Object[size()]);
130: }
131:
132: public Object[] toArray(Object[] a) {
133: List list = new ArrayList();
134: Iterator i = iterator();
135: try {
136: while (i.hasNext()) {
137: list.add(i.next());
138: }
139:
140: return list.toArray(a);
141: } finally {
142: close(i);
143: }
144: }
145:
146: public boolean add(Object o) {
147: if (!filter.evaluate(o)) {
148: return false;
149: }
150:
151: return delegate.add(o);
152: }
153:
154: public boolean contains(Object o) {
155: return delegate.contains(o) && filter.evaluate(o);
156: }
157:
158: public boolean remove(Object o) {
159: return delegate.remove(o);
160: }
161:
162: public boolean addAll(Collection c) {
163: boolean changed = false;
164:
165: for (Iterator i = c.iterator(); i.hasNext();) {
166: changed = changed | add(i.next());
167: }
168:
169: return changed;
170: }
171:
172: public boolean containsAll(Collection c) {
173: for (Iterator i = c.iterator(); i.hasNext();) {
174: if (!contains(i.next())) {
175: return false;
176: }
177: }
178:
179: return true;
180: }
181:
182: public boolean removeAll(Collection c) {
183: return delegate.removeAll(c);
184: }
185:
186: public boolean retainAll(Collection c) {
187: return delegate.retainAll(c);
188: }
189:
190: public FeatureReader reader() throws IOException {
191: return new DelegateFeatureReader(getSchema(), features());
192: }
193:
194: public String getID() {
195: return delegate.getID();
196: }
197:
198: public Object[] getAttributes(Object[] attributes) {
199: return delegate.getAttributes(attributes);
200: }
201:
202: public Object getAttribute(String xPath) {
203: return delegate.getAttribute(xPath);
204: }
205:
206: public Object getAttribute(int index) {
207: return delegate.getAttribute(index);
208: }
209:
210: public void setAttribute(int position, Object val)
211: throws IllegalAttributeException,
212: ArrayIndexOutOfBoundsException {
213: delegate.setAttribute(position, val);
214: }
215:
216: public int getNumberOfAttributes() {
217: return delegate.getNumberOfAttributes();
218: }
219:
220: public void setAttribute(String xPath, Object attribute)
221: throws IllegalAttributeException {
222: delegate.setAttribute(xPath, attribute);
223: }
224:
225: public Geometry getDefaultGeometry() {
226: return delegate.getDefaultGeometry();
227: }
228:
229: public void setDefaultGeometry(Geometry geometry)
230: throws IllegalAttributeException {
231: delegate.setDefaultGeometry(geometry);
232: }
233:
234: public ReferencedEnvelope getBounds() {
235: //calculate manually
236: return ReferencedEnvelope.reference(DataUtilities.bounds(this));
237: }
238:
239: }
|