Class AbstractNode

  1  
  2 /*
  3  * Copyright (c) 2016 Vivid Solutions.
  4  *
  5  * All rights reserved. This program and the accompanying materials
  6  * are made available under the terms of the Eclipse Public License 2.0
  7  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
  8  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
  9  * and the Eclipse Distribution License is available at
 10  *
 11  * http://www.eclipse.org/org/documents/edl-v10.php.
 12  */
 13 package org.locationtech.jts.index.strtree;
 14  
 15 import java.io.Serializable;
 16 import java.util.ArrayList;
 17 import java.util.List;
 18  
 19 import org.locationtech.jts.util.Assert;
 20  
 21 /**
 22  * A node of an {@link AbstractSTRtree}. A node is one of:
 23  * <ul>
 24  * <li>empty
 25  * <li>an <i>interior node</i> containing child {@link AbstractNode}s
 26  * <li>a <i>leaf node</i> containing data items ({@link ItemBoundable}s). 
 27  * </ul>
 28  * A node stores the bounds of its children, and its level within the index tree.
 29  *
 30  * @version 1.7
 31  */
 32 public abstract class AbstractNode implements Boundable, Serializable {
 33   /**
 34    * 
 35    */
 36   private static final long serialVersionUID = 6493722185909573708L;
 37   
 38   private ArrayList childBoundables = new ArrayList();
 39   private Object bounds = null;
 40   private int level;
 41  
 42   /**
 43    * Default constructor required for serialization.
 44    */
 45   public AbstractNode() {
 46   }
 47  
 48   /**
 49    * Constructs an AbstractNode at the given level in the tree
 50    * @param level 0 if this node is a leaf, 1 if a parent of a leaf, and so on; the
 51    * root node will have the highest level
 52    */
 53   public AbstractNode(int level) {
 54     this.level = level;
 55   }
 56  
 57   /**
 58    * Returns either child {@link AbstractNode}s, or if this is a leaf node, real data (wrapped
 59    * in {@link ItemBoundable}s).
 60    * 
 61    * @return a list of the children
 62    */
 63   public List getChildBoundables() {
 64     return childBoundables;
 65   }
 66  
 67   /**
 68    * Returns a representation of space that encloses this Boundable,
 69    * preferably not much bigger than this Boundable's boundary yet fast to
 70    * test for intersection with the bounds of other Boundables. The class of
 71    * object returned depends on the subclass of AbstractSTRtree.
 72    *
 73    * @return an Envelope (for STRtrees), an Interval (for SIRtrees), or other
 74    *         object (for other subclasses of AbstractSTRtree)
 75    * @see AbstractSTRtree.IntersectsOp
 76    */
 77   protected abstract Object computeBounds();
 78  
 79   /**
 80    * Gets the bounds of this node
 81    * 
 82    * @return the object representing bounds in this index
 83    */
 84   public Object getBounds() {
 85     if (bounds == null) {
 86       bounds = computeBounds();
 87     }
 88     return bounds;
 89   }
 90  
 91   /**
 92    * Returns 0 if this node is a leaf, 1 if a parent of a leaf, and so on; the
 93    * root node will have the highest level
 94    * 
 95    * @return the node level
 96    */
 97   public int getLevel() {
 98     return level;
 99   }
100  
101   /**
102    * Gets the count of the {@link Boundable}s at this node.
103    * 
104    * @return the count of boundables at this node
105    */
106   public int size()
107   {
108     return childBoundables.size();
109   }
110   
111   /**
112    * Tests whether there are any {@link Boundable}s at this node.
113    * 
114    * @return true if there are boundables at this node
115    */
116   public boolean isEmpty()
117   {
118     return childBoundables.isEmpty();
119   }
120   
121   /**
122    * Adds either an AbstractNode, or if this is a leaf node, a data object
123    * (wrapped in an ItemBoundable)
124    * 
125    * @param childBoundable the child to add
126    */
127   public void addChildBoundable(Boundable childBoundable) {
128     Assert.isTrue(bounds == null);
129     childBoundables.add(childBoundable);
130   }
131 }
132