JAL-2591 updated HiddenColumns::findHiddenRegionPositions + test
[jalview.git] / src / jalview / datamodel / HiddenColumns.java
index da0b854..1e1a58b 100644 (file)
@@ -6,11 +6,12 @@ import jalview.util.ShiftList;
 import java.util.ArrayList;
 import java.util.BitSet;
 import java.util.Collections;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Vector;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
-public class HiddenColumns
+public class HiddenColumns implements Iterable<int[]>
 {
   private static final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
   
@@ -272,39 +273,41 @@ public class HiddenColumns
   }
 
   /**
-   * Use this method to determine where the next hiddenRegion starts
+   * Use this method to determine the set of hiddenRegion start positions
    * 
-   * @param hiddenRegion
-   *          index of hidden region (counts from 0)
-   * @return column number in visible view
+   * @return list of column number in visible view where hidden regions start
    */
-  public int findHiddenRegionPosition(int hiddenRegion)
+  public List<Integer> findHiddenRegionPositions()
   {
     try
     {
       lock.readLock().lock();
-      int result = 0;
-      if (hiddenColumns != null)
+      List<Integer> positions = new ArrayList<>(
+              hiddenColumns.size());
+
+      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);
-          if (hiddenRegion == 0)
+          int index = 0;
+          int gaps = 0;
+          do
           {
-            return region[0];
-          }
+            int[] region = hiddenColumns.elementAt(index);
+            gaps += region[1] + 1 - region[0];
+            result = region[1] + 1;
+            index++;
+          } while (index <= i);
 
-          gaps += region[1] + 1 - region[0];
-          result = region[1] + 1;
-          index++;
-        } while (index <= hiddenRegion);
-
-        result -= gaps;
+          result -= gaps;
+        }
+        positions.add(result);
       }
 
-      return result;
+      return positions;
     }
     finally
     {
@@ -567,18 +570,7 @@ public class HiddenColumns
       {
         if (copy.hiddenColumns != null)
         {
-          hiddenColumns = new Vector<>(copy.hiddenColumns.size());
-          for (int i = 0, j = copy.hiddenColumns.size(); i < j; i++)
-          {
-            int[] rh, cp;
-            rh = copy.hiddenColumns.elementAt(i);
-            if (rh != null)
-            {
-              cp = new int[rh.length];
-              System.arraycopy(rh, 0, cp, 0, rh.length);
-              hiddenColumns.addElement(cp);
-            }
-          }
+          hiddenColumns = copy.copyHiddenRegions();
         }
       }
     }
@@ -588,6 +580,64 @@ public class HiddenColumns
     }
   }
 
+  private Vector<int[]> copyHiddenRegions()
+  {
+    Vector<int[]> copy = new Vector<>(hiddenColumns.size());
+    for (int i = 0, j = hiddenColumns.size(); i < j; i++)
+    {
+      int[] rh, cp;
+      rh = hiddenColumns.elementAt(i);
+      if (rh != null)
+      {
+        cp = new int[rh.length];
+        System.arraycopy(rh, 0, cp, 0, rh.length);
+        copy.addElement(cp);
+      }
+    }
+    return copy;
+  }
+
+  private ArrayList<int[]> copyHiddenRegionsToArrayList()
+  {
+    ArrayList<int[]> copy = new ArrayList<>(hiddenColumns.size());
+    for (int i = 0, j = hiddenColumns.size(); i < j; i++)
+    {
+      int[] rh, cp;
+      rh = hiddenColumns.elementAt(i);
+      if (rh != null)
+      {
+        cp = new int[rh.length];
+        System.arraycopy(rh, 0, cp, 0, rh.length);
+        copy.add(cp);
+      }
+    }
+    return copy;
+  }
+
+  public void getHiddenColumnsCopy(Vector<int[]> copy)
+  {
+    try
+    {
+      lock.readLock().lock();
+      copy = copyHiddenRegions();
+    } finally
+    {
+      lock.readLock().unlock();
+    }
+  }
+
+  public void getHiddenColumnsCopy(ArrayList<int[]> copy)
+  {
+    try
+    {
+      lock.readLock().lock();
+      copy = copyHiddenRegionsToArrayList();
+    } finally
+    {
+      lock.readLock().unlock();
+    }
+  }
+
   /**
    * propagate shift in alignment columns to column selection
    * 
@@ -1539,4 +1589,14 @@ public class HiddenColumns
     }
   }
 
+  @Override
+  public Iterator<int[]> iterator()
+  {
+    if (hiddenColumns == null)
+    {
+      return Collections.<int[]> emptyList().iterator();
+    }
+    return hiddenColumns.iterator();
+  }
+
 }