JAL-2759 change subtractVisibleColumns and removal of reverse iterator
[jalview.git] / src / jalview / datamodel / HiddenColumns.java
index d7628b7..ea8da8d 100644 (file)
@@ -21,6 +21,7 @@
 package jalview.datamodel;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.BitSet;
 import java.util.Iterator;
 import java.util.List;
@@ -634,12 +635,7 @@ public class HiddenColumns
     try
     {
       LOCK.readLock().lock();
-      int num = 0;
-      if (hasHiddenColumns())
-      {
-        num = hiddenColumns.size();
-      }
-      return num;
+      return hiddenColumns.size();
     } finally
     {
       LOCK.readLock().unlock();
@@ -668,16 +664,17 @@ public class HiddenColumns
         return false;
       }
 
+      Iterator<int[]> it = this.iterator();
       Iterator<int[]> thatit = that.iterator();
-      for (int[] thisRange : hiddenColumns)
+      while (it.hasNext())
       {
-        int[] thatRange = thatit.next();
-        if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
+        if (!(Arrays.equals(it.next(), thatit.next())))
         {
           return false;
         }
       }
       return true;
+
     } finally
     {
       LOCK.readLock().unlock();
@@ -769,48 +766,25 @@ public class HiddenColumns
 
   /**
    * Find the visible column which is a given visible number of columns to the
-   * left of another visible column. i.e. for a startColumn x, the column which
-   * is distance 1 away will be column x-1.
+   * left (negative visibleDistance) or right (positive visibleDistance) of
+   * startColumn. If startColumn is not visible, we use the visible column at
+   * the left boundary of the hidden region containing startColumn.
    * 
    * @param visibleDistance
-   *          the number of visible columns to offset by
+   *          the number of visible columns to offset by (left offset = negative
+   *          value; right offset = positive value)
    * @param startColumn
-   *          the column to start from
-   * @return the position of the column in the visible alignment
+   *          the position of the column to start from (absolute position)
+   * @return the position of the column which is <visibleDistance> away
+   *         (absolute position)
    */
-  public int subtractVisibleColumns(int visibleDistance, int startColumn)
+  public int offsetByVisibleColumns(int visibleDistance, int startColumn)
   {
     try
     {
       LOCK.readLock().lock();
-      int distance = visibleDistance;
-
-      // in case startColumn is in a hidden region, move it to the left
-      int start = visibleToAbsoluteColumn(absoluteToVisibleColumn(startColumn));
-
-      Iterator<int[]> it = new ReverseRegionsIterator(0, start,
-              hiddenColumns);
-
-      while (it.hasNext() && (distance > 0))
-      {
-        int[] region = it.next();
-
-        if (start > region[1])
-        {
-          // subtract the gap to right of region from distance
-          if (start - region[1] <= distance)
-          {
-            distance -= start - region[1];
-            start = region[0] - 1;
-          }
-          else
-          {
-            start = start - distance;
-            distance = 0;
-          }
-        }
-      }
-      return start - distance;
+      int start = absoluteToVisibleColumn(startColumn);
+      return visibleToAbsoluteColumn(start + visibleDistance);
 
     } finally
     {
@@ -1156,9 +1130,9 @@ public class HiddenColumns
     // make sure only bits between start and end are set
     if (!tohide.isEmpty())
     {
-      tohide.clear(0, start - 1);
-      tohide.clear(Math.min(end + 1, tohide.length() - 1),
-              tohide.length() - 1);
+      tohide.clear(0, start);
+      tohide.clear(Math.min(end + 1, tohide.length() + 1),
+              tohide.length() + 1);
     }
 
     hideColumns(tohide);
@@ -1178,42 +1152,45 @@ public class HiddenColumns
     {
       LOCK.writeLock().lock();
       
-      HiddenCursorPosition pos = cursor.findRegionForColumn(start);
-      int index = pos.getRegionIndex();
-      int startindex = index; // first index in hiddenColumns to remove
-      
-      if (index != -1 && index != hiddenColumns.size())
+      if (!hiddenColumns.isEmpty())
       {
-        // regionIndex is the region which either contains start
-        // or lies to the right of start
-        int[] region = hiddenColumns.get(index);
-        if (region[0] < start && region[1] >= start)
+        HiddenCursorPosition pos = cursor.findRegionForColumn(start);
+        int index = pos.getRegionIndex();
+        int startindex = index; // first index in hiddenColumns to remove
+
+        if (index != -1 && index != hiddenColumns.size())
         {
-          // region contains start, truncate so that it ends just before start
-          region[1] = start - 1;
-          startindex++;
+          // regionIndex is the region which either contains start
+          // or lies to the right of start
+          int[] region = hiddenColumns.get(index);
+          if (region[0] < start && region[1] >= start)
+          {
+            // region contains start, truncate so that it ends just before start
+            region[1] = start - 1;
+            startindex++;
+          }
         }
-      }
-      
-      pos = cursor.findRegionForColumn(end);
-      index = pos.getRegionIndex();
-      int endindex = index - 1; // last index in hiddenColumns to remove
 
-      if (index != -1 && index != hiddenColumns.size())
-      {
-        // regionIndex is the region which either contains end
-        // or lies to the right of end
-        int[] region = hiddenColumns.get(index);
-        if (region[0] <= end && region[1] > end)
+        pos = cursor.findRegionForColumn(end);
+        index = pos.getRegionIndex();
+        int endindex = index - 1; // last index in hiddenColumns to remove
+
+        if (index != -1 && index != hiddenColumns.size())
         {
-          // region contains end, truncate so that it starts just after end
-          region[0] = end + 1;
+          // regionIndex is the region which either contains end
+          // or lies to the right of end
+          int[] region = hiddenColumns.get(index);
+          if (region[0] <= end && region[1] > end)
+          {
+            // region contains end, truncate so that it starts just after end
+            region[0] = end + 1;
+          }
         }
+
+        hiddenColumns.subList(startindex, endindex + 1).clear();
+        cursor.resetCursor(hiddenColumns);
+        numColumns = 0;
       }
-      
-      hiddenColumns.subList(startindex, endindex + 1).clear();
-      cursor.resetCursor(hiddenColumns);
-      numColumns = 0;
     } finally
     {
       LOCK.writeLock().unlock();