001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/io/rtree/Node.java $
002 // ----------------------------------------
003 //RTree implementation.
004 //Copyright (C) 2002-2004 Wolfgang Baer - WBaer@gmx.de
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; either
009 //version 2.1 of the License, or (at your option) any later version.
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 //You should have received a copy of the GNU Lesser General Public
017 //License along with this library; if not, write to the Free Software
018 //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019 //----------------------------------------
020
021 package org.deegree.io.rtree;
022
023 import java.io.Serializable;
024
025 /**
026 * <p>
027 * Abstract class for common implementation and definition of abstract methods for both concrete
028 * classes LeafNode and NoneLeafNode.
029 * </p>
030 *
031 * @author Wolfgang Baer - WBaer@gmx.de
032 * @author last edited by: $Author: aschmitz $
033 *
034 * @version $Revision: 12519 $, $Date: 2008-06-25 11:37:30 +0200 (Mi, 25 Jun 2008) $
035 */
036 abstract class Node implements Serializable {
037
038 protected transient PageFile file;
039
040 protected int parentNode;
041
042 protected int pageNumber;
043
044 protected int counter;
045
046 protected HyperBoundingBox unionMinBB;
047
048 protected HyperBoundingBox[] hyperBBs;
049
050 protected int place;
051
052 /**
053 * Constructor.
054 *
055 * @param pageNumber -
056 * number of this node in page file
057 * @param pageFile -
058 * the PageFile of this node
059 */
060 protected Node( int pageNumber, PageFile pageFile ) {
061 this.file = pageFile;
062 this.pageNumber = pageNumber;
063 parentNode = 0;
064 hyperBBs = new HyperBoundingBox[file.getCapacity()];
065
066 for ( int i = 0; i < file.getCapacity(); i++ )
067 hyperBBs[i] = HyperBoundingBox.getNullHyperBoundingBox( file.getDimension() );
068
069 unionMinBB = HyperBoundingBox.getNullHyperBoundingBox( file.getDimension() );
070 counter = 0;
071 }
072
073 /**
074 * Inserts the given data into the node
075 *
076 * @param obj -
077 * object to insert (Typ Integer oder AbstractNode)
078 * @param box -
079 * the associated HyperBoundingBox
080 */
081 protected abstract void insertData( Object obj, HyperBoundingBox box );
082
083 /**
084 * Deletes a the entry with given index from node
085 *
086 * @param index -
087 * index of entry
088 */
089 protected abstract void deleteData( int index );
090
091 /**
092 * Fetches the data for given index from node
093 *
094 * @param index -
095 * index of data
096 */
097 protected abstract Object getData( int index );
098
099 /**
100 * Returns the parent node of this.
101 *
102 * @return Node
103 */
104 protected Node getParent() {
105
106 Node node = null;
107 try {
108 node = file.readNode( parentNode );
109 } catch ( PageFileException e ) {
110 // PageFileException: AbstractNode.getParent() - readNode
111 e.printStackTrace();
112 }
113
114 return node;
115 }
116
117 /**
118 * Returns the page number of this.
119 *
120 * @return int
121 */
122 protected int getPageNumber() {
123 return pageNumber;
124 }
125
126 /**
127 * Sets the page number of this to given number
128 *
129 * @param number -
130 * int
131 */
132 protected void setPageNumber( int number ) {
133 this.pageNumber = number;
134 }
135
136 /**
137 * Currently used space in the node
138 *
139 * @return int
140 */
141 protected int getUsedSpace() {
142 return counter;
143 }
144
145 /**
146 * Returns the HyperBoundingBox over all Entries currently in the node
147 *
148 * @return HyperBoundingBox
149 */
150 protected HyperBoundingBox getUnionMinBB() {
151 return unionMinBB;
152 }
153
154 /**
155 * Updates the HyperBoundingBox over all Entries currently in the node
156 */
157 protected void updateNodeBoundingBox() {
158 this.unionMinBB = HyperBoundingBox.getNullHyperBoundingBox( file.getDimension() );
159 for ( int i = 0; i < this.getUsedSpace(); i++ )
160 this.unionMinBB = this.unionMinBB.unionBoundingBox( this.hyperBBs[i] );
161 }
162
163 /**
164 * Returns an array of HyperBoundingBox objects of the entries of the node. The array may be
165 * empty - for used place in the node see getUsedSpace.
166 *
167 * @return HyperBoundingBox[] - boxes of the entries
168 * @see #getUsedSpace()
169 */
170 protected HyperBoundingBox[] getHyperBoundingBoxes() {
171 return hyperBBs;
172 }
173
174 /**
175 * Returns the HyperBoundingBox for entrie with given index.
176 *
177 * @param index -
178 * index of entry
179 * @return HyperBoundingBox
180 */
181 protected HyperBoundingBox getHyperBoundingBox( int index ) {
182 return hyperBBs[index];
183 }
184
185 /**
186 * Tests if this is the root node.
187 *
188 * @return boolean
189 */
190 protected boolean isRoot() {
191 return pageNumber == 0;
192 }
193
194 /**
195 * Deep copy without data entries (only HyperBoundingBox objects)
196 *
197 * @see java.lang.Object#clone()
198 */
199 @Override
200 protected abstract Object clone();
201
202 /**
203 * String-Representation of Node
204 *
205 */
206 @Override
207 public String toString() {
208 String str = "";
209
210 if ( this instanceof LeafNode ) {
211 str = "LeafNode: " + unionMinBB.toString();
212 } else {
213 str = "NoneLeafNode: " + unionMinBB.toString();
214 }
215
216 return str;
217 }
218 }