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.FeatureWriter;
022: import org.geotools.feature.Feature;
023: import org.geotools.feature.FeatureIterator;
024:
025: /**
026: * An iterator wrapper for a FeatureWriter - for use with
027: * an AbstractFeatureCollection.
028: * <p>
029: * There is no reason modify this class, subclasses that wish
030: * to work with a custom iterator need just that - a custom iterator.
031: * <p>
032: * <p>
033: * The use of this class against a FeatureSource not backed by
034: * a Transaction may *really* cut into performance. Consider if
035: * you will the overhead involved in writing out each feature into
036: * a temporary file (when the user may not even modify anything).
037: * </p>
038: * @author jgarnett
039: * @since 2.1.RC0
040: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/data/store/FeatureWriterFeatureIterator.java $
041: */
042: final class FeatureWriterFeatureIterator implements FeatureIterator {
043: FeatureWriter writer;
044:
045: public FeatureWriterFeatureIterator(FeatureWriter writer) {
046: this .writer = writer;
047: }
048:
049: public boolean hasNext() {
050: try {
051: if (writer == null) {
052: return false;
053: }
054: writer.write(); // write out any "changes" made
055: if (writer.hasNext()) {
056: return true;
057: } else {
058: close();
059: return false;
060: // auto close because we don't trust client
061: // code to call close
062: }
063: } catch (IOException e) {
064: close();
065: return false; // failure sounds like lack of next to me
066: }
067: }
068:
069: public Feature next() {
070: if (writer == null) {
071: throw new NoSuchElementException("Iterator has been closed");
072: }
073: try {
074: return writer.next();
075: } catch (IOException io) {
076: NoSuchElementException problem = new NoSuchElementException(
077: "Could not obtain the next feature:" + io);
078: problem.initCause(io);
079: throw problem;
080: }
081: }
082:
083: public void remove() {
084: try {
085: writer.remove();
086: } catch (IOException problem) {
087: throw (IllegalStateException) new IllegalStateException(
088: "Could not remove feature").initCause(problem);
089: }
090: }
091:
092: public void close() {
093: if (writer != null) {
094: try {
095: writer.close();
096: } catch (IOException e) {
097: // sorry but iterators die quitely in the night
098: }
099: writer = null;
100: }
101: }
102: };
|