Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | None | 0 0
  1. package bLO3dTree;
  2.  
  3. import threeDPoint.ThreeDPoint;
  4.  
  5. public class BLO3dTreeLeafNode extends BLO3dTreeNode {
  6.     private ThreeDPoint[] points = new ThreeDPoint[4];
  7.     private String[] labels = new String[4];
  8.     private int size = 0;
  9.  
  10.     /** ** * * ** ** * ** * * ** ** * ** * ** * ** * * ** * ** * ** ** * *
  11.      * @return the label associated to aPoint (using fuzzy tests for float equality)
  12.      * or return null if aPoint not present
  13.      * @param aPoint the point whose label is wanted
  14.      */
  15.     public String labelPoint(ThreeDPoint aPoint) {
  16.         if (aPoint == null) {
  17.             return null;
  18.         }
  19.         int index = findIndex(aPoint);
  20.         if (index == -1) {
  21.             return null;
  22.         }
  23.         return labels[index];
  24.     }
  25.  
  26.     /** ** * * ** ** * ** * * ** ** * ** * ** * ** * * ** * ** * ** ** * *
  27.      * insert aPoint into the leaf with aLabel as label,
  28.      * do so provided the leaf is not full
  29.      * @param aPoint the point to insert
  30.      * @param aLabel the label to associate to the point
  31.      * @throws NullPointException when aPoint is null
  32.      * @throws NullLabelException when aLabel is null
  33.      * @throws DuplicatePointException when aPoint already occurs
  34.      * @throws LeafFullException when the leaf already has 4 points
  35.      */
  36.     public void addPoint(ThreeDPoint aPoint, String aLabel) throws NullPointException, NullLabelException, DuplicatePointException, LeafFullException {
  37.         if (aPoint == null) {
  38.             throw new NullPointException();
  39.         }
  40.         if (aLabel == null) {
  41.             throw new NullLabelException();
  42.         }
  43.         if (this.labelPoint(aPoint) != null) {
  44.             throw new DuplicatePointException("Duplicate of point"+ aPoint.toString());
  45.         }
  46.         if (size==4) {
  47.             throw new LeafFullException();
  48.         }
  49.         size++;
  50.         points[size] = aPoint;
  51.         labels[size] = aLabel;
  52.     }
  53.  
  54.     /** ** * * ** ** * ** * * ** ** * ** * ** * ** * * ** * ** * ** ** * *
  55.      * remove a point and its corresponding label
  56.      * @return true if the point was present, false otherwise
  57.      * @param aPoint the point to be removed
  58.      */
  59.     public Boolean removePoint(ThreeDPoint aPoint) {
  60.         int index = findIndex(aPoint);
  61.         if (index == -1) {
  62.             return false;
  63.         }
  64.         for (int i = index+1; i<size; i++) {
  65.             points[i-1] = points[i];
  66.             labels[i-1] = labels[i];
  67.         }
  68.         size--;
  69.         return true;
  70.     }
  71.  
  72.     /** ** * * ** ** * ** * * ** ** * ** * ** * ** * * ** * ** * ** ** * *
  73.      * @return the index where aPoint occurs, or -1 otherwise
  74.      * (using fuzzy tests for float equality)
  75.      * @param aPoint the point whose index is wanted
  76.      * @require aPoint not null
  77.      */
  78.     private int findIndex(ThreeDPoint aPoint) {
  79.         for (int i = 0; i < size; i++) {
  80.             ThreeDPoint point = points[i];
  81.             if (point.fuzzyEquals(aPoint)) {
  82.                 return i;
  83.             }
  84.         }
  85.         return -1;
  86.     }
  87.  
  88.     /**
  89.      * @return an array with points
  90.      */
  91.     public ThreeDPoint[] getPoints() {
  92.         ThreeDPoint[] result = new ThreeDPoint[size];
  93.         for (int i = 0; i < size; i++) {
  94.             result[i] = points[i];
  95.         }
  96.         return result;
  97.     }
  98.  
  99.     /**
  100.      * @return an array with labels
  101.      */
  102.     public String[] getLabels() {
  103.         String[] result = new String[size];
  104.         for (int i = 0; i < size; i++) {
  105.             result[i] = labels[i];
  106.         }
  107.         return result;
  108.     }
  109.  
  110.     /**
  111.      * @return the size
  112.      */
  113.     public int getSize() {
  114.         return size;
  115.     }
  116.  
  117.     /**
  118.      * check if the leaf already contains 4 points
  119.      * @return true when the leaf is already full
  120.      */
  121.     public Boolean isFull() {
  122.         return (size == 4);
  123.     }
  124.  
  125.     public BLO3dTreeLeafNode(int aLevel) {
  126.         super(aLevel);
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement