JAL-2759 minor refactor to hiding bitsets
[jalview.git] / src / jalview / datamodel / HiddenColumns.java
index 17d3fe4..45dcab2 100644 (file)
@@ -772,15 +772,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 +816,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 +825,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);
   }
 
   /**