001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.data;
017:
018: import org.geotools.feature.Feature;
019: import org.geotools.feature.FeatureCollection;
020: import org.geotools.feature.FeatureType;
021: import org.geotools.feature.IllegalAttributeException;
022: import java.io.IOException;
023: import java.util.Arrays;
024: import java.util.Collection;
025: import java.util.Iterator;
026: import java.util.NoSuchElementException;
027:
028: /**
029: * FeatureReader that reads features from a java.util.collection of features,
030: * an array of features or a FeatureCollection.
031: *
032: * @author jones
033: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/data/CollectionFeatureReader.java $
034: */
035: public class CollectionFeatureReader implements FeatureReader {
036: private FeatureCollection collection;
037: private Iterator features;
038: private FeatureType type;
039: private boolean closed = false;
040:
041: /**
042: * Create a new instance.
043: *
044: * @param featuresArg a colleciton of features. <b>All features must be of the same FeatureType</b>
045: * @param typeArg the Feature type of of the features.
046: */
047: public CollectionFeatureReader(Collection featuresArg,
048: FeatureType typeArg) {
049: assert !featuresArg.isEmpty();
050:
051: if (featuresArg instanceof FeatureCollection) {
052: collection = (FeatureCollection) featuresArg;
053: }
054:
055: this .features = featuresArg.iterator();
056: this .type = typeArg;
057: }
058:
059: /**
060: * Create a new instance.
061: *
062: * @param featuresArg a FeatureCollection. <b>All features must be of the same FeatureType</b>
063: * @param typeArg the Feature type of of the features.
064: */
065: public CollectionFeatureReader(FeatureCollection featuresArg,
066: FeatureType typeArg) {
067: assert !featuresArg.isEmpty();
068: collection = featuresArg;
069: this .features = featuresArg.iterator();
070: this .type = typeArg;
071: }
072:
073: /**
074: * Create a new instance.
075: *
076: * @param featuresArg an of features. <b>All features must be of the same FeatureType</b>
077: */
078: public CollectionFeatureReader(Feature[] featuresArg) {
079: assert featuresArg.length > 0;
080: this .features = Arrays.asList(featuresArg).iterator();
081: type = featuresArg[0].getFeatureType();
082: }
083:
084: /**
085: * @see org.geotools.data.FeatureReader#getFeatureType()
086: */
087: public FeatureType getFeatureType() {
088: return type;
089: }
090:
091: /**
092: * @see org.geotools.data.FeatureReader#next()
093: */
094: public Feature next() throws IOException,
095: IllegalAttributeException, NoSuchElementException {
096: if (closed) {
097: throw new NoSuchElementException("Reader has been closed");
098: }
099:
100: return (Feature) features.next();
101: }
102:
103: /**
104: * @see org.geotools.data.FeatureReader#hasNext()
105: */
106: public boolean hasNext() throws IOException {
107: return features.hasNext() && !closed;
108: }
109:
110: /**
111: * @see org.geotools.data.FeatureReader#close()
112: */
113: public void close() throws IOException {
114: closed = true;
115:
116: if (collection != null) {
117: collection.close(features);
118: }
119: }
120: }
|