JAL-2591 Further refactoring (still incomplete)
[jalview.git] / src / jalview / datamodel / HiddenColumns.java
index be10721..5966312 100644 (file)
@@ -67,13 +67,16 @@ public class HiddenColumns
     {
       LOCK.readLock().lock();
       StringBuilder regionBuilder = new StringBuilder();
-      for (int[] range : hiddenColumns)
+      if (hiddenColumns != null)
       {
-        regionBuilder.append(delimiter).append(range[0]).append(between)
-                .append(range[1]);
-      }
+        for (int[] range : hiddenColumns)
+        {
+          regionBuilder.append(delimiter).append(range[0]).append(between)
+                  .append(range[1]);
+        }
 
-      regionBuilder.deleteCharAt(0);
+        regionBuilder.deleteCharAt(0);
+      }
       return regionBuilder.toString();
     } finally
     {
@@ -331,29 +334,37 @@ public class HiddenColumns
     try
     {
       LOCK.readLock().lock();
-      List<Integer> positions = new ArrayList<>(
-              hiddenColumns.size());
+      List<Integer> positions = null;
 
-      positions.add(hiddenColumns.elementAt(0)[0]);
-      for (int i = 1; i < hiddenColumns.size(); ++i)
+      if (hiddenColumns != null)
       {
+        positions = new ArrayList<>(hiddenColumns.size());
 
-        int result = 0;
-        if (hiddenColumns != null)
+        positions.add(hiddenColumns.elementAt(0)[0]);
+        for (int i = 1; i < hiddenColumns.size(); ++i)
         {
-          int index = 0;
-          int gaps = 0;
-          do
+
+          int result = 0;
+          if (hiddenColumns != null)
           {
-            int[] region = hiddenColumns.elementAt(index);
-            gaps += region[1] + 1 - region[0];
-            result = region[1] + 1;
-            index++;
-          } while (index <= i);
+            int index = 0;
+            int gaps = 0;
+            do
+            {
+              int[] region = hiddenColumns.elementAt(index);
+              gaps += region[1] + 1 - region[0];
+              result = region[1] + 1;
+              index++;
+            } while (index <= i);
 
-          result -= gaps;
+            result -= gaps;
+          }
+          positions.add(result);
         }
-        positions.add(result);
+      }
+      else
+      {
+        positions = new ArrayList<>();
       }
 
       return positions;
@@ -665,6 +676,13 @@ public class HiddenColumns
     return copy;
   }
 
+  /**
+   * Returns a copy of the vector of hidden regions, as a vector. Before using
+   * this method please consider if you really need access to the hidden regions
+   * - a new (or existing!) method on HiddenColumns might be more appropriate.
+   * 
+   * @return hidden regions as vector of [start,end] pairs
+   */
   public Vector<int[]> getHiddenColumnsCopy()
   {
     try
@@ -677,6 +695,14 @@ public class HiddenColumns
     }
   }
 
+  /**
+   * Returns a copy of the vector of hidden regions, as an ArrayList. Before
+   * using this method please consider if you really need access to the hidden
+   * regions - a new (or existing!) method on HiddenColumns might be more
+   * appropriate.
+   * 
+   * @return hidden regions as an ArrayList of [start,end] pairs
+   */
   public ArrayList<int[]> getHiddenColumnsCopyAsList()
   {
     try
@@ -1711,4 +1737,37 @@ public class HiddenColumns
 
   }
 
+  /**
+   * Finds the hidden region (if any) which starts or ends at res
+   * 
+   * @param res
+   *          visible residue position, unadjusted for hidden columns
+   * @return region as [start,end] or null if no matching region is found
+   */
+  public int[] getRegionWithEdgeAtRes(int res)
+  {
+    try
+    {
+      LOCK.readLock().lock();
+      int adjres = adjustForHiddenColumns(res);
+
+      int[] reveal = null;
+      if (hiddenColumns != null)
+      {
+        for (int[] region : hiddenColumns)
+        {
+          if (adjres + 1 == region[0] || adjres - 1 == region[1])
+          {
+            reveal = region;
+            break;
+          }
+        }
+      }
+      return reveal;
+    } finally
+    {
+      LOCK.readLock().unlock();
+    }
+  }
+
 }