Merge branch 'bug/JAL-2791exportFilteredFeature' into merge/JAL-2791
[jalview.git] / src / jalview / datamodel / HiddenColumns.java
index 17d3fe4..2d43f02 100644 (file)
@@ -469,6 +469,10 @@ public class HiddenColumns
     }
   }
 
+  /**
+   * Answers true if obj is an instance of HiddenColumns, and holds the same
+   * array of start-end column ranges as this, else answers false
+   */
   @Override
   public boolean equals(Object obj)
   {
@@ -772,15 +776,39 @@ public class HiddenColumns
    */
   public void hideColumns(BitSet inserts)
   {
+    hideColumns(inserts, 0, inserts.length() - 1);
+  }
+
+  /**
+   * Hide columns corresponding to the marked bits, within the range
+   * [start,end]. Entries in tohide which are outside [start,end] are ignored.
+   * 
+   * @param tohide
+   *          columns mapped to bits starting from zero
+   * @param start
+   *          start of range to hide columns within
+   * @param end
+   *          end of range to hide columns within
+   */
+  private void hideColumns(BitSet tohide, int start, int end)
+  {
     try
     {
       LOCK.writeLock().lock();
-      for (int firstSet = inserts
-              .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
+      for (int firstSet = tohide
+              .nextSetBit(start), lastSet = start; firstSet >= start
+                      && lastSet <= end; firstSet = tohide
                       .nextSetBit(lastSet))
       {
-        lastSet = inserts.nextClearBit(firstSet);
-        hideColumns(firstSet, lastSet - 1);
+        lastSet = tohide.nextClearBit(firstSet);
+        if (lastSet <= end)
+        {
+          hideColumns(firstSet, lastSet - 1);
+        }
+        else if (firstSet <= end)
+        {
+          hideColumns(firstSet, end);
+        }
       }
       cursor = new HiddenColumnsCursor(hiddenColumns);
     } finally
@@ -792,6 +820,7 @@ public class HiddenColumns
   /**
    * Hide columns corresponding to the marked bits, within the range
    * [start,end]. Entries in tohide which are outside [start,end] are ignored.
+   * NB Existing entries in [start,end] are cleared.
    * 
    * @param tohide
    *          columns mapped to bits starting from zero
@@ -800,19 +829,10 @@ public class HiddenColumns
    * @param end
    *          end of range to hide columns within
    */
-  public void hideColumns(BitSet tohide, int start, int end)
+  public void clearAndHideColumns(BitSet tohide, int start, int end)
   {
     clearHiddenColumnsInRange(start, end);
-
-    // make sure only bits between start and end are set
-    if (!tohide.isEmpty())
-    {
-      tohide.clear(0, start);
-      tohide.clear(Math.min(end + 1, tohide.length() + 1),
-              tohide.length() + 1);
-    }
-
-    hideColumns(tohide);
+    hideColumns(tohide, start, end);
   }
 
   /**
@@ -992,7 +1012,7 @@ public class HiddenColumns
     try
     {
       LOCK.readLock().lock();
-      return new HiddenColsIterator(hiddenColumns);
+      return new RangeIterator(hiddenColumns);
     } finally
     {
       LOCK.readLock().unlock();
@@ -1013,7 +1033,7 @@ public class HiddenColumns
     try
     {
       LOCK.readLock().lock();
-      return new HiddenColsIterator(start, end, hiddenColumns);
+      return new RangeIterator(start, end, hiddenColumns);
     } finally
     {
       LOCK.readLock().unlock();
@@ -1029,7 +1049,7 @@ public class HiddenColumns
    * @param end
    *          position to end at (inclusive, visible column position)
    */
-  public Iterator<Integer> getBoundedStartIterator(int start, int end)
+  public Iterator<Integer> getStartRegionIterator(int start, int end)
   {
     try
     {
@@ -1046,7 +1066,7 @@ public class HiddenColumns
       HiddenCursorPosition pos = cursor
               .findRegionForColumn(absoluteStart - 1, false);
 
-      return new BoundedStartRegionIterator(pos, start, end,
+      return new StartRegionIterator(pos, start, end,
               hiddenColumns);
     } finally
     {
@@ -1068,7 +1088,8 @@ public class HiddenColumns
     try
     {
       LOCK.readLock().lock();
-      return new VisibleColsIterator(start, end, hiddenColumns);
+      return new RangeElementsIterator(
+              new VisibleContigsIterator(start, end + 1, hiddenColumns));
     } finally
     {
       LOCK.readLock().unlock();