JAL-2069 spike updated with latest (FeatureTypeSettings)
[jalview.git] / src / jalview / datamodel / features / NCNode.java
index 0755614..b991750 100644 (file)
@@ -1,5 +1,27 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.datamodel.features;
 
+import jalview.datamodel.ContiguousI;
+
 import java.util.ArrayList;
 import java.util.List;
 
@@ -9,7 +31,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)
@@ -40,7 +62,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);
   }
@@ -49,7 +71,7 @@ class NCNode<V extends ContiguousI>
   {
     region = entry;
     subregions = newNCList;
-    size = 1 + newNCList.getSize();
+    size = 1 + newNCList.size();
   }
 
   /**
@@ -69,12 +91,14 @@ class NCNode<V extends ContiguousI>
     }
   }
 
-  int getStart()
+  @Override
+  public int getBegin()
   {
     return region.getBegin();
   }
 
-  int getEnd()
+  @Override
+  public int getEnd()
   {
     return region.getEnd();
   }
@@ -148,6 +172,7 @@ class NCNode<V extends ContiguousI>
     {
       subregions.add(entry);
     }
+    size++;
   }
 
   /**
@@ -158,11 +183,18 @@ 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());
   }
 
   /**
@@ -218,4 +250,26 @@ class NCNode<V extends ContiguousI>
   {
     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;
+  }
 }