001:
002: /*
003: * The JTS Topology Suite is a collection of Java classes that
004: * implement the fundamental operations required to validate a given
005: * geo-spatial data set to a known topological specification.
006: *
007: * Copyright (C) 2001 Vivid Solutions
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: *
023: * For more information, contact:
024: *
025: * Vivid Solutions
026: * Suite #1A
027: * 2328 Government Street
028: * Victoria BC V8T 5G5
029: * Canada
030: *
031: * (250)385-6040
032: * www.vividsolutions.com
033: */
034: package com.vividsolutions.jts.geom;
035:
036: import java.util.Iterator;
037: import java.util.NoSuchElementException;
038:
039: /**
040: * Iterates over all {@link Geometry}s in a {@link GeometryCollection}.
041: * . Implements a pre-order depth-first traversal of the <code>GeometryCollection</code>
042: * (which may be nested). The original <code>GeometryCollection</code> is
043: * returned as well (as the first object), as are all sub-collections. It is
044: * simple to ignore the <code>GeometryCollection</code> objects if they are not
045: * needed.
046: *
047: *@version 1.7
048: */
049: public class GeometryCollectionIterator implements Iterator {
050:
051: /**
052: * The <code>GeometryCollection</code> being iterated over.
053: */
054: private Geometry parent;
055: /**
056: * Indicates whether or not the first element (the <code>GeometryCollection</code>
057: * ) has been returned.
058: */
059: private boolean atStart;
060: /**
061: * The number of <code>Geometry</code>s in the the <code>GeometryCollection</code>
062: * .
063: */
064: private int max;
065: /**
066: * The index of the <code>Geometry</code> that will be returned when <code>next</code>
067: * is called.
068: */
069: private int index;
070: /**
071: * The iterator over a nested <code>GeometryCollection</code>, or <code>null</code>
072: * if this <code>GeometryCollectionIterator</code> is not currently iterating
073: * over a nested <code>GeometryCollection</code>.
074: */
075: private GeometryCollectionIterator subcollectionIterator;
076:
077: /**
078: * Constructs an iterator over the given <code>GeometryCollection</code>.
079: *
080: *@param parent the collection over which to iterate; also, the first
081: * element returned by the iterator.
082: */
083: public GeometryCollectionIterator(Geometry parent) {
084: this .parent = parent;
085: atStart = true;
086: index = 0;
087: max = parent.getNumGeometries();
088: }
089:
090: public boolean hasNext() {
091: if (atStart) {
092: return true;
093: }
094: if (subcollectionIterator != null) {
095: if (subcollectionIterator.hasNext()) {
096: return true;
097: }
098: subcollectionIterator = null;
099: }
100: if (index >= max) {
101: return false;
102: }
103: return true;
104: }
105:
106: public Object next() {
107: // the parent GeometryCollection is the first object returned
108: if (atStart) {
109: atStart = false;
110: return parent;
111: }
112: if (subcollectionIterator != null) {
113: if (subcollectionIterator.hasNext()) {
114: return subcollectionIterator.next();
115: } else {
116: subcollectionIterator = null;
117: }
118: }
119: if (index >= max) {
120: throw new NoSuchElementException();
121: }
122: Geometry obj = parent.getGeometryN(index++);
123: if (obj instanceof GeometryCollection) {
124: subcollectionIterator = new GeometryCollectionIterator(
125: (GeometryCollection) obj);
126: // there will always be at least one element in the sub-collection
127: return subcollectionIterator.next();
128: }
129: return obj;
130: }
131:
132: /**
133: * Not implemented.
134: *
135: *@throws UnsupportedOperationException This method is not implemented.
136: */
137: public void remove() {
138: throw new UnsupportedOperationException(getClass().getName());
139: }
140: }
|