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