JAL-2674 HiddenColumns interface tidies
[jalview.git] / src / jalview / datamodel / features / NCNode.java
index ab10f67..007f3b1 100644 (file)
@@ -1,5 +1,7 @@
 package jalview.datamodel.features;
 
+import jalview.datamodel.ContiguousI;
+
 import java.util.ArrayList;
 import java.util.List;
 
@@ -9,7 +11,7 @@ import java.util.List;
  *
  * @param <V>
  */
-class NCNode<V extends ContiguousI>
+class NCNode<V extends ContiguousI> implements ContiguousI
 {
   /*
    * deep size (number of ranges included)
@@ -18,6 +20,9 @@ class NCNode<V extends ContiguousI>
 
   private V region;
 
+  /*
+   * null, or an object holding contained subregions of this nodes region
+   */
   private NCList<V> subregions;
 
   /**
@@ -37,7 +42,7 @@ class NCNode<V extends ContiguousI>
    */
   NCNode(V range)
   {
-    List<V> ranges = new ArrayList<V>();
+    List<V> ranges = new ArrayList<>();
     ranges.add(range);
     build(ranges);
   }
@@ -46,7 +51,7 @@ class NCNode<V extends ContiguousI>
   {
     region = entry;
     subregions = newNCList;
-    // size = 1 + newNCList.size();
+    size = 1 + newNCList.size();
   }
 
   /**
@@ -66,12 +71,14 @@ class NCNode<V extends ContiguousI>
     }
   }
 
-  int getStart()
+  @Override
+  public int getBegin()
   {
     return region.getBegin();
   }
 
-  int getEnd()
+  @Override
+  public int getEnd()
   {
     return region.getEnd();
   }
@@ -112,7 +119,7 @@ class NCNode<V extends ContiguousI>
    * @param to
    * @param result
    */
-  void addOverlaps(long from, long to, List<V> result)
+  void findOverlaps(long from, long to, List<V> result)
   {
     if (region.getBegin() <= to && region.getEnd() >= from)
     {
@@ -145,6 +152,7 @@ class NCNode<V extends ContiguousI>
     {
       subregions.add(entry);
     }
+    size++;
   }
 
   /**
@@ -155,10 +163,93 @@ class NCNode<V extends ContiguousI>
    */
   boolean isValid()
   {
+    /*
+     * we don't handle reverse ranges
+     */
+    if (region != null && region.getBegin() > region.getEnd())
+    {
+      return false;
+    }
     if (subregions == null)
     {
       return true;
     }
-    return subregions.isValid(getStart(), getEnd());
+    return subregions.isValid(getBegin(), getEnd());
+  }
+
+  /**
+   * Adds all contained entries to the given list
+   * 
+   * @param entries
+   */
+  void getEntries(List<V> entries)
+  {
+    entries.add(region);
+    if (subregions != null)
+    {
+      subregions.getEntries(entries);
+    }
+  }
+
+  /**
+   * Answers true if this object contains the given entry (by object equals
+   * test), else false
+   * 
+   * @param entry
+   * @return
+   */
+  boolean contains(V entry)
+  {
+    if (entry == null)
+    {
+      return false;
+    }
+    if (entry.equals(region))
+    {
+      return true;
+    }
+    return subregions == null ? false : subregions.contains(entry);
+  }
+
+  /**
+   * Answers the 'root' region modelled by this object
+   * 
+   * @return
+   */
+  V getRegion()
+  {
+    return region;
+  }
+
+  /**
+   * Answers the (possibly null) contained regions within this object
+   * 
+   * @return
+   */
+  NCList<V> getSubRegions()
+  {
+    return subregions;
+  }
+
+  /**
+   * Nulls the subregion reference if it is empty (after a delete entry
+   * operation)
+   */
+  void deleteSubRegionsIfEmpty()
+  {
+    if (subregions != null && subregions.size() == 0)
+    {
+      subregions = null;
+    }
+  }
+
+  /**
+   * Answers the (deep) size of this node i.e. the number of ranges it models
+   * 
+   * @return
+   */
+  int size()
+  {
+    return size;
   }
 }