~ubuntu-branches/ubuntu/natty/jts/natty

« back to all changes in this revision

Viewing changes to src/com/vividsolutions/jts/geom/GeometryCollectionIterator.java

  • Committer: Bazaar Package Importer
  • Author(s): Wolfgang Baer
  • Date: 2005-08-07 14:12:35 UTC
  • Revision ID: james.westby@ubuntu.com-20050807141235-7hy3ll3xpq79djcb
Tags: upstream-1.6
ImportĀ upstreamĀ versionĀ 1.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
/*
 
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.
 
7
 *
 
8
 * Copyright (C) 2001 Vivid Solutions
 
9
 *
 
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.
 
14
 *
 
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.
 
19
 *
 
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
 
23
 *
 
24
 * For more information, contact:
 
25
 *
 
26
 *     Vivid Solutions
 
27
 *     Suite #1A
 
28
 *     2328 Government Street
 
29
 *     Victoria BC  V8T 5G5
 
30
 *     Canada
 
31
 *
 
32
 *     (250)385-6040
 
33
 *     www.vividsolutions.com
 
34
 */
 
35
package com.vividsolutions.jts.geom;
 
36
 
 
37
import java.util.Iterator;
 
38
import java.util.NoSuchElementException;
 
39
 
 
40
/**
 
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
 
46
 *  needed.
 
47
 *
 
48
 *@version 1.6
 
49
 */
 
50
public class GeometryCollectionIterator implements Iterator {
 
51
 
 
52
  /**
 
53
   *  The <code>GeometryCollection</code> being iterated over.
 
54
   */
 
55
  private Geometry parent;
 
56
  /**
 
57
   *  Indicates whether or not the first element (the <code>GeometryCollection</code>
 
58
   *  ) has been returned.
 
59
   */
 
60
  private boolean atStart;
 
61
  /**
 
62
   *  The number of <code>Geometry</code>s in the the <code>GeometryCollection</code>
 
63
   *  .
 
64
   */
 
65
  private int max;
 
66
  /**
 
67
   *  The index of the <code>Geometry</code> that will be returned when <code>next</code>
 
68
   *  is called.
 
69
   */
 
70
  private int index;
 
71
  /**
 
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>.
 
75
   */
 
76
  private GeometryCollectionIterator subcollectionIterator;
 
77
 
 
78
  /**
 
79
   *  Constructs an iterator over the given <code>GeometryCollection</code>.
 
80
   *
 
81
   *@param  parent  the collection over which to iterate; also, the first
 
82
   *      element returned by the iterator.
 
83
   */
 
84
  public GeometryCollectionIterator(Geometry parent) {
 
85
    this.parent = parent;
 
86
    atStart = true;
 
87
    index = 0;
 
88
    max = parent.getNumGeometries();
 
89
  }
 
90
 
 
91
  public boolean hasNext() {
 
92
    if (atStart) {
 
93
      return true;
 
94
    }
 
95
    if (subcollectionIterator != null) {
 
96
      if (subcollectionIterator.hasNext()) {
 
97
        return true;
 
98
      }
 
99
      subcollectionIterator = null;
 
100
    }
 
101
    if (index >= max) {
 
102
      return false;
 
103
    }
 
104
    return true;
 
105
  }
 
106
 
 
107
  public Object next() {
 
108
    // the parent GeometryCollection is the first object returned
 
109
    if (atStart) {
 
110
      atStart = false;
 
111
      return parent;
 
112
    }
 
113
    if (subcollectionIterator != null) {
 
114
      if (subcollectionIterator.hasNext()) {
 
115
        return subcollectionIterator.next();
 
116
      }
 
117
      else {
 
118
        subcollectionIterator = null;
 
119
      }
 
120
    }
 
121
    if (index >= max) {
 
122
      throw new NoSuchElementException();
 
123
    }
 
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();
 
129
    }
 
130
    return obj;
 
131
  }
 
132
 
 
133
  /**
 
134
   *  Not implemented.
 
135
   *
 
136
   *@throws  UnsupportedOperationException  This method is not implemented.
 
137
   */
 
138
  public void remove() {
 
139
    throw new UnsupportedOperationException(getClass().getName());
 
140
  }
 
141
}
 
142