001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-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.store;
017:
018: import java.io.IOException;
019: import java.util.NoSuchElementException;
020:
021: import org.geotools.data.FeatureReader;
022: import org.geotools.feature.Feature;
023: import org.geotools.feature.FeatureIterator;
024: import org.geotools.feature.IllegalAttributeException;
025:
026: /**
027: * An iterator wrapper for a FeatureReader - for use with
028: * an AbstractFeatureCollection.
029: * <p>
030: * There is no reason modify this class, subclasses that wish
031: * to work with a custom iterator need just that - a custom iterator.
032: * <p>
033: * @author jgarnett
034: * @since 2.1.RC0
035: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/data/store/FeatureReaderFeatureIterator.java $
036: */
037: final class FeatureReaderFeatureIterator implements FeatureIterator {
038: FeatureReader reader;
039:
040: public FeatureReaderFeatureIterator(FeatureReader reader) {
041: this .reader = reader;
042: }
043:
044: public boolean hasNext() {
045: try {
046: if (reader == null)
047: return false;
048: if (reader.hasNext()) {
049: return true;
050: } else {
051: // auto close because we don't trust
052: // client code to call closed :-)
053: close();
054: return false;
055: }
056: } catch (IOException e) {
057: close();
058: return false; // failure sounds like lack of next to me
059: }
060: }
061:
062: public Feature next() {
063: if (reader == null) {
064: throw new NoSuchElementException("Iterator has been closed");
065: }
066: try {
067: return reader.next();
068: } catch (IOException io) {
069: close();
070: NoSuchElementException problem = new NoSuchElementException(
071: "Could not obtain the next feature:" + io);
072: problem.initCause(io);
073: throw problem;
074: } catch (IllegalAttributeException create) {
075: close();
076: NoSuchElementException problem = new NoSuchElementException(
077: "Could not create the next feature:" + create);
078: problem.initCause(create);
079: throw problem;
080: }
081: }
082:
083: /** If this is a problem, a different iterator can be made based on FeatureWriter */
084: public void remove() {
085: throw new UnsupportedOperationException(
086: "Modification of contents is not supported");
087: }
088:
089: /**
090: * This method only needs package visability as only AbstractFeatureCollection
091: * is trusted enough to call it.
092: */
093: public void close() {
094: if (reader != null) {
095: try {
096: reader.close();
097: } catch (IOException e) {
098: // sorry but iterators die quitely in the night
099: }
100: reader = null;
101: }
102: }
103: };
|