JAL-2759 numColumns reset
[jalview.git] / src / jalview / datamodel / HiddenColumns.java
index 8cb7971..9a8669f 100644 (file)
@@ -66,6 +66,9 @@ public class HiddenColumns
 {
   private static final int HASH_MULTIPLIER = 31;
 
+  private static final int NUMCOLUMNS_RESET = -1; // value of numColumns if it
+                                                  // needs to be recalculated
+
   private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock();
 
   /*
@@ -140,7 +143,7 @@ public class HiddenColumns
             numColumns += region[1] - region[0] + 1;
           }
         }
-        cursor.resetCursor(hiddenColumns);
+        cursor = new HiddenColumnsCursor(hiddenColumns);
       }
     } finally
     {
@@ -208,10 +211,11 @@ public class HiddenColumns
 
       // reset the cursor to just before our insertion point: this saves
       // a lot of reprocessing in large alignments
-      cursor.resetCursor(hiddenColumns, previndex, prevHiddenCount);
+      cursor = new HiddenColumnsCursor(hiddenColumns, previndex,
+              prevHiddenCount);
 
       // reset the number of columns so they will be recounted
-      numColumns = 0;
+      resetNumColumns();
 
     } finally
     {
@@ -301,8 +305,8 @@ public class HiddenColumns
       {
         hideColumns(r[0], r[1]);
       }
-      cursor.resetCursor(hiddenColumns);
-      numColumns = 0;
+      cursor = new HiddenColumnsCursor(hiddenColumns);
+      resetNumColumns();
     } finally
     {
       LOCK.writeLock().unlock();
@@ -326,8 +330,8 @@ public class HiddenColumns
         }
       }
       hiddenColumns.clear();
-      cursor.resetCursor(hiddenColumns);
-      numColumns = 0;
+      cursor = new HiddenColumnsCursor(hiddenColumns);
+      resetNumColumns();
 
     } finally
     {
@@ -371,8 +375,7 @@ public class HiddenColumns
 
             if (hiddenColumns.isEmpty())
             {
-              hiddenColumns.clear();
-              numColumns = 0;
+              resetNumColumns();
             }
             else
             {
@@ -438,7 +441,7 @@ public class HiddenColumns
     {
       LOCK.readLock().lock();
 
-      if (numColumns == 0)
+      if (numColumns == NUMCOLUMNS_RESET)
       {
         // numColumns is out of date, so recalculate
         int size = 0;
@@ -459,6 +462,17 @@ public class HiddenColumns
   }
 
   /**
+   * Reset numColumns so that it gets recalculated. Currently the code does not
+   * recalculate numColumns on hide/reveal as it requires a full sweep of the
+   * hidden columns collection / smarter updating. Placeholder here if later on
+   * a recalculation is added.
+   */
+  private void resetNumColumns()
+  {
+    numColumns = NUMCOLUMNS_RESET;
+  }
+
+  /**
    * Get the number of distinct hidden regions
    * 
    * @return number of regions
@@ -782,8 +796,8 @@ public class HiddenColumns
         lastSet = inserts.nextClearBit(firstSet);
         hideColumns(firstSet, lastSet - 1);
       }
-      cursor.resetCursor(hiddenColumns);
-      numColumns = 0;
+      cursor = new HiddenColumnsCursor(hiddenColumns);
+      resetNumColumns();
     } finally
     {
       LOCK.writeLock().unlock();
@@ -866,8 +880,8 @@ public class HiddenColumns
         }
 
         hiddenColumns.subList(startindex, endindex + 1).clear();
-        cursor.resetCursor(hiddenColumns);
-        numColumns = 0;
+        cursor = new HiddenColumnsCursor(hiddenColumns);
+        resetNumColumns();
       }
     } finally
     {