01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2003-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: package org.geotools.data;
17:
18: import java.io.IOException;
19: import java.util.NoSuchElementException;
20:
21: import org.geotools.feature.Feature;
22: import org.geotools.feature.FeatureType;
23: import org.geotools.feature.IllegalAttributeException;
24:
25: /**
26: * Basic support for a FeatureReader that limits itself to the number of
27: * features passed in.
28: *
29: * @author Chris Holmes
30: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/data/MaxFeatureReader.java $
31: * @version $Id: MaxFeatureReader.java 20651 2006-07-21 07:51:54Z jgarnett $
32: */
33: public class MaxFeatureReader implements FeatureReader {
34: protected final FeatureReader featureReader;
35: protected final int maxFeatures;
36: protected int counter = 0;
37:
38: /**
39: * Creates a new instance of MaxFeatureReader
40: *
41: * @param featureReader FeatureReader being maxed
42: * @param maxFeatures DOCUMENT ME!
43: */
44: public MaxFeatureReader(FeatureReader featureReader, int maxFeatures) {
45: this .featureReader = featureReader;
46: this .maxFeatures = maxFeatures;
47: }
48:
49: public Feature next() throws IOException,
50: IllegalAttributeException, NoSuchElementException {
51: if (hasNext()) {
52: counter++;
53:
54: return featureReader.next();
55: } else {
56: throw new NoSuchElementException("No such Feature exists");
57: }
58: }
59:
60: public void close() throws IOException {
61: featureReader.close();
62: }
63:
64: public FeatureType getFeatureType() {
65: return featureReader.getFeatureType();
66: }
67:
68: /**
69: * <p></p>
70: *
71: * @return <code>true</code> if the featureReader has not passed the max
72: * and still has more features.
73: *
74: * @throws IOException If the reader we are filtering encounters a problem
75: */
76: public boolean hasNext() throws IOException {
77: return (featureReader.hasNext() && (counter < maxFeatures));
78: }
79: }
|