4
* The JTS Topology Suite is a collection of Java classes that
5
* implement the fundamental operations required to validate a given
6
* geo-spatial data set to a known topological specification.
8
* Copyright (C) 2001 Vivid Solutions
10
* This library is free software; you can redistribute it and/or
11
* modify it under the terms of the GNU Lesser General Public
12
* License as published by the Free Software Foundation; either
13
* version 2.1 of the License, or (at your option) any later version.
15
* This library is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
* Lesser General Public License for more details.
20
* You should have received a copy of the GNU Lesser General Public
21
* License along with this library; if not, write to the Free Software
22
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
* For more information, contact:
28
* 2328 Government Street
33
* www.vividsolutions.com
35
package com.vividsolutions.jts.geom;
37
import java.util.Iterator;
38
import java.util.NoSuchElementException;
41
* Iterates over all {@link Geometry}s in a {@link GeometryCollection}.
42
* . Implements a pre-order depth-first traversal of the <code>GeometryCollection</code>
43
* (which may be nested). The original <code>GeometryCollection</code> is
44
* returned as well (as the first object), as are all sub-collections. It is
45
* simple to ignore the <code>GeometryCollection</code> objects if they are not
50
public class GeometryCollectionIterator implements Iterator {
53
* The <code>GeometryCollection</code> being iterated over.
55
private Geometry parent;
57
* Indicates whether or not the first element (the <code>GeometryCollection</code>
58
* ) has been returned.
60
private boolean atStart;
62
* The number of <code>Geometry</code>s in the the <code>GeometryCollection</code>
67
* The index of the <code>Geometry</code> that will be returned when <code>next</code>
72
* The iterator over a nested <code>GeometryCollection</code>, or <code>null</code>
73
* if this <code>GeometryCollectionIterator</code> is not currently iterating
74
* over a nested <code>GeometryCollection</code>.
76
private GeometryCollectionIterator subcollectionIterator;
79
* Constructs an iterator over the given <code>GeometryCollection</code>.
81
*@param parent the collection over which to iterate; also, the first
82
* element returned by the iterator.
84
public GeometryCollectionIterator(Geometry parent) {
88
max = parent.getNumGeometries();
91
public boolean hasNext() {
95
if (subcollectionIterator != null) {
96
if (subcollectionIterator.hasNext()) {
99
subcollectionIterator = null;
107
public Object next() {
108
// the parent GeometryCollection is the first object returned
113
if (subcollectionIterator != null) {
114
if (subcollectionIterator.hasNext()) {
115
return subcollectionIterator.next();
118
subcollectionIterator = null;
122
throw new NoSuchElementException();
124
Geometry obj = parent.getGeometryN(index++);
125
if (obj instanceof GeometryCollection) {
126
subcollectionIterator = new GeometryCollectionIterator((GeometryCollection) obj);
127
// there will always be at least one element in the sub-collection
128
return subcollectionIterator.next();
136
*@throws UnsupportedOperationException This method is not implemented.
138
public void remove() {
139
throw new UnsupportedOperationException(getClass().getName());