JAL-2591 simplifying hidden columns usage
[jalview.git] / src / jalview / datamodel / HiddenColumns.java
index da0b854..9722c0a 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();
   
@@ -567,18 +568,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 +578,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 +1587,14 @@ public class HiddenColumns
     }
   }
 
+  @Override
+  public Iterator<int[]> iterator()
+  {
+    if (hiddenColumns == null)
+    {
+      return Collections.<int[]> emptyList().iterator();
+    }
+    return hiddenColumns.iterator();
+  }
+
 }