001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-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.index.quadtree;
017:
018: import java.io.IOException;
019: import java.util.ArrayList;
020: import java.util.Arrays;
021: import java.util.List;
022:
023: import com.vividsolutions.jts.geom.Envelope;
024:
025: /**
026: * DOCUMENT ME!
027: *
028: * @author Tommaso Nolli
029: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/main/java/org/geotools/index/quadtree/Node.java $
030: */
031: public class Node {
032: private Envelope bounds;
033: protected int numShapesId;
034: protected int[] shapesId;
035: protected List subNodes;
036: protected Node parent;
037: private boolean visited = false;
038: private boolean childrenVisited = false;
039: protected int id;
040:
041: public Node(Envelope bounds, int id, Node parent) {
042: this .parent = parent;
043: this .id = id;
044: this .bounds = new Envelope(bounds);
045: this .subNodes = new ArrayList(4);
046: this .shapesId = new int[4];
047: Arrays.fill(this .shapesId, -1);
048: }
049:
050: /**
051: * DOCUMENT ME!
052: *
053: * @return Returns the bounds.
054: */
055: public Envelope getBounds() {
056: return this .bounds;
057: }
058:
059: /**
060: * DOCUMENT ME!
061: *
062: * @param bounds The bounds to set.
063: */
064: public void setBounds(Envelope bounds) {
065: this .bounds = bounds;
066: }
067:
068: /**
069: * DOCUMENT ME!
070: *
071: * @return Returns the numSubNodes.
072: */
073: public int getNumSubNodes() {
074: return this .subNodes.size();
075: }
076:
077: /**
078: * DOCUMENT ME!
079: *
080: * @return Returns the number of records stored.
081: */
082: public int getNumShapeIds() {
083: return this .numShapesId;
084: }
085:
086: /**
087: * DOCUMENT ME!
088: *
089: * @param node
090: *
091: * @throws NullPointerException DOCUMENT ME!
092: */
093: public void addSubNode(Node node) {
094: if (node == null) {
095: throw new NullPointerException(
096: "Cannot add null to subnodes");
097: }
098:
099: this .subNodes.add(node);
100: }
101:
102: /**
103: * Removes a subnode
104: *
105: * @param node The subnode to remove
106: *
107: * @return true if the subnode has been removed
108: */
109: public boolean removeSubNode(Node node) {
110: return this .subNodes.remove(node);
111: }
112:
113: /**
114: *
115: *
116: */
117: public void clearSubNodes() {
118: this .subNodes.clear();
119: }
120:
121: /**
122: * Gets the Node at the requested position
123: *
124: * @param pos The position
125: *
126: * @return A Node
127: *
128: * @throws StoreException DOCUMENT ME!
129: */
130: public Node getSubNode(int pos) throws StoreException {
131: return (Node) this .subNodes.get(pos);
132: }
133:
134: /**
135: * Add a shape id
136: *
137: * @param id
138: */
139: public void addShapeId(int id) {
140: if (this .shapesId.length == this .numShapesId) {
141: // Increase the array
142: int[] newIds = new int[this .numShapesId * 2];
143: Arrays.fill(newIds, -1);
144: System.arraycopy(this .shapesId, 0, newIds, 0,
145: this .numShapesId);
146: this .shapesId = newIds;
147: }
148:
149: this .shapesId[this .numShapesId] = id;
150: this .numShapesId++;
151: }
152:
153: /**
154: * Gets a shape id
155: *
156: * @param pos The position
157: *
158: * @return The shape id (or recno) at the requested position
159: *
160: * @throws ArrayIndexOutOfBoundsException DOCUMENT ME!
161: */
162: public int getShapeId(int pos) {
163: if (pos >= this .numShapesId) {
164: throw new ArrayIndexOutOfBoundsException("Requsted " + pos
165: + " but size = " + this .numShapesId);
166: }
167:
168: return this .shapesId[pos];
169: }
170:
171: /**
172: * Sets the shape ids
173: *
174: * @param ids
175: */
176: public void setShapesId(int[] ids) {
177: if (ids == null) {
178: this .numShapesId = 0;
179: } else {
180: this .shapesId = ids;
181: this .numShapesId = 0;
182:
183: for (int i = 0; i < ids.length; i++) {
184: if (ids[i] == -1) {
185: break;
186: }
187:
188: this .numShapesId++;
189: }
190: }
191: }
192:
193: /**
194: * DOCUMENT ME!
195: *
196: * @return Returns the shapesId.
197: */
198: public int[] getShapesId() {
199: return this .shapesId;
200: }
201:
202: public Node getParent() {
203: return parent;
204: }
205:
206: public void setParent(Node parent) {
207: this .parent = parent;
208: }
209:
210: public boolean isVisited() {
211: return visited;
212: }
213:
214: public void setVisited(boolean visited) {
215: this .visited = visited;
216: }
217:
218: public Node getSibling() throws StoreException {
219: if (parent == null || id == parent.getNumSubNodes() - 1)
220: return null;
221: return parent.getSubNode(id + 1);
222: }
223:
224: public boolean isChildrenVisited() {
225: return childrenVisited;
226: }
227:
228: public void setChildrenVisited(boolean childrenVisited) {
229: this .childrenVisited = childrenVisited;
230: }
231:
232: public Node copy() throws IOException {
233: return new Node(bounds, id, parent);
234: }
235: }
|