JAL-2759 Various minor HiddenColumns updates after review
[jalview.git] / src / jalview / datamodel / HiddenColumns.java
index 6e594bd..b903826 100644 (file)
@@ -33,13 +33,13 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
  * use an iterator obtained from one of:
  * 
  * - getBoundedIterator: iterates over the hidden regions, within some bounds,
- * returning absolute positions
+ * returning *absolute* positions
  * 
  * - getBoundedStartIterator: iterates over the start positions of hidden
- * regions, within some bounds, returning visible positions
+ * regions, within some bounds, returning *visible* positions
  * 
  * - getVisContigsIterator: iterates over visible regions in a range, returning
- * absolute positions
+ * *absolute* positions
  * 
  * - getVisibleColsIterator: iterates over the visible *columns*
  * 
@@ -47,7 +47,20 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
  * numbering begins at 0 throughout this class.
  * 
  * @author kmourao
- *
+ */
+
+/* Implementation notes:
+ * 
+ * Methods which change the hiddenColumns collection should use a writeLock to
+ * prevent other threads accessing the hiddenColumns collection while changes
+ * are being made. They should also reset the hidden columns cursor, and either
+ * update the hidden columns count, or set it to 0 (so that it will later be
+ * updated when needed).
+ * 
+ * 
+ * Methods which only need read access to the hidden columns collection should
+ * use a readLock to prevent other threads changing the hidden columns
+ * collection while it is in use.
  */
 public class HiddenColumns
 {
@@ -79,14 +92,6 @@ public class HiddenColumns
   {
   }
 
-  /* 
-   * Methods which change the hiddenColumns collection. These methods should 
-   * use a writeLock to prevent other threads accessing the hiddenColumns
-   * collection while changes are being made. They should also reset the hidden
-   * columns cursor, and either update the hidden columns count, or set it to 0 
-   * (so that it will later be updated when needed).
-   */
-
   /**
    * Copy constructor
    * 
@@ -273,9 +278,9 @@ public class HiddenColumns
         }
         region[1] = Math.max(nextRegion[1], end);
 
-        // in theory this is faster than hiddenColumns.remove(i+1)
-        // benchmarking results a bit ambivalent
-        hiddenColumns.subList(i + 1, i + 2).clear();
+        // in theory hiddenColumns.subList(i + 1, i + 2).clear() is faster than
+        // hiddenColumns.remove(i+1) but benchmarking results a bit ambivalent
+        hiddenColumns.remove(i + 1);
       }
       added = true;
     }
@@ -383,15 +388,6 @@ public class HiddenColumns
     }
   }
 
-
-
-
-  /*
-   * Methods which only need read access to the hidden columns collection. 
-   * These methods should use a readLock to prevent other threads changing
-   * the hidden columns collection while it is in use.
-   */
-
   /**
    * Output regions data as a string. String is in the format:
    * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
@@ -639,7 +635,7 @@ public class HiddenColumns
    * @return the index of the next hidden column, or alPos if there is no next
    *         hidden column
    */
-  public int getHiddenBoundaryRight(int alPos)
+  public int getNextHiddenBoundary(boolean left, int alPos)
   {
     try
     {
@@ -647,7 +643,13 @@ public class HiddenColumns
       if (!hiddenColumns.isEmpty())
       {
         int index = cursor.findRegionForColumn(alPos).getRegionIndex();
-        if (index < hiddenColumns.size())
+
+        if (left && index > 0)
+        {
+          int[] region = hiddenColumns.get(index - 1);
+          return region[1];
+        }
+        else if (!left && index < hiddenColumns.size())
         {
           int[] region = hiddenColumns.get(index);
           if (alPos < region[0])
@@ -672,38 +674,6 @@ public class HiddenColumns
   }
 
   /**
-   * This method returns the leftmost limit of a region of an alignment with
-   * hidden columns. In otherwords, the previous hidden column.
-   * 
-   * @param alPos
-   *          the absolute (visible) alignmentPosition to find the previous
-   *          hidden column for
-   */
-  public int getHiddenBoundaryLeft(int alPos)
-  {
-    try
-    {
-      LOCK.readLock().lock();
-
-      if (!hiddenColumns.isEmpty())
-      {
-        int index = cursor.findRegionForColumn(alPos).getRegionIndex();
-
-        if (index > 0)
-        {
-          int[] region = hiddenColumns.get(index - 1);
-          return region[1];
-        }
-      }
-      return alPos;
-    } finally
-    {
-      LOCK.readLock().unlock();
-    }
-  }
-
-
-  /**
    * Answers if a column in the alignment is visible
    * 
    * @param column
@@ -736,67 +706,6 @@ public class HiddenColumns
   }
 
   /**
-   * Get the visible sections of a set of sequences
-   * 
-   * @param start
-   *          sequence position to start from
-   * @param end
-   *          sequence position to end at
-   * @param seqs
-   *          an array of sequences
-   * @return an array of strings encoding the visible parts of each sequence
-   */
-  public String[] getVisibleSequenceStrings(int start, int end,
-          SequenceI[] seqs)
-  {
-    try
-    {
-      LOCK.readLock().lock();
-      int iSize = seqs.length;
-      String[] selections = new String[iSize];
-      if (!hiddenColumns.isEmpty())
-      {
-        for (int i = 0; i < iSize; i++)
-        {
-          StringBuilder visibleSeq = new StringBuilder();
-
-          Iterator<int[]> blocks = new VisibleContigsIterator(start,
-                  end + 1, hiddenColumns);
-
-          while (blocks.hasNext())
-          {
-            int[] block = blocks.next();
-            if (blocks.hasNext())
-            {
-              visibleSeq
-                      .append(seqs[i].getSequence(block[0], block[1] + 1));
-            }
-            else
-            {
-              visibleSeq
-                      .append(seqs[i].getSequence(block[0], block[1]));
-            }
-          }
-
-          selections[i] = visibleSeq.toString();
-        }
-      }
-      else
-      {
-        for (int i = 0; i < iSize; i++)
-        {
-          selections[i] = seqs[i].getSequenceAsString(start, end);
-        }
-      }
-
-      return selections;
-    } finally
-    {
-      LOCK.readLock().unlock();
-    }
-  }
-
-  /**
    * Locate the first position visible for this sequence. If seq isn't visible
    * then return the position of the left side of the hidden boundary region.
    * 
@@ -911,6 +820,7 @@ public class HiddenColumns
     {
       LOCK.readLock().lock();
       int hashCode = 1;
+
       for (int[] hidden : hiddenColumns)
       {
         hashCode = HASH_MULTIPLIER * hashCode + hidden[0];