JAL-2591 Refactor HiddenColumns to use ArrayList instead of Vector
[jalview.git] / src / jalview / datamodel / HiddenColumns.java
index b53fc8b..65ff9a5 100644 (file)
@@ -38,10 +38,43 @@ public class HiddenColumns
    * list of hidden column [start, end] ranges; the list is maintained in
    * ascending start column order
    */
-  private Vector<int[]> hiddenColumns;
+  private ArrayList<int[]> hiddenColumns;
 
   /**
-   * This Method is used to return all the HiddenColumn regions
+   * Constructor
+   */
+  public HiddenColumns()
+  {
+  }
+
+  /**
+   * Copy constructor
+   * 
+   * @param copy
+   */
+  public HiddenColumns(HiddenColumns copy)
+  {
+    try
+    {
+
+      LOCK.readLock().lock();
+      if (copy != null)
+      {
+        if (copy.hiddenColumns != null)
+        {
+          hiddenColumns = copy.copyHiddenRegionsToArrayList();
+        }
+      }
+    } finally
+    {
+      LOCK.readLock().unlock();
+    }
+  }
+
+  /**
+   * This method is used to return all the HiddenColumn regions and is intended
+   * to remain private. External callers which need a copy of the regions can
+   * call getHiddenColumnsCopyAsList.
    * 
    * @return empty list or List of hidden column intervals
    */
@@ -186,7 +219,7 @@ public class HiddenColumns
       {
         for (int i = 0; i < hiddenColumns.size(); i++)
         {
-          int[] region = hiddenColumns.elementAt(i);
+          int[] region = hiddenColumns.get(i);
           if (result >= region[0])
           {
             result += region[1] - region[0] + 1;
@@ -221,7 +254,7 @@ public class HiddenColumns
         int[] region;
         do
         {
-          region = hiddenColumns.elementAt(index++);
+          region = hiddenColumns.get(index++);
           if (hiddenColumn > region[1])
           {
             result -= region[1] + 1 - region[0];
@@ -340,7 +373,7 @@ public class HiddenColumns
       {
         positions = new ArrayList<>(hiddenColumns.size());
 
-        positions.add(hiddenColumns.elementAt(0)[0]);
+        positions.add(hiddenColumns.get(0)[0]);
         for (int i = 1; i < hiddenColumns.size(); ++i)
         {
 
@@ -351,7 +384,7 @@ public class HiddenColumns
             int gaps = 0;
             do
             {
-              int[] region = hiddenColumns.elementAt(index);
+              int[] region = hiddenColumns.get(index);
               gaps += region[1] + 1 - region[0];
               result = region[1] + 1;
               index++;
@@ -392,7 +425,7 @@ public class HiddenColumns
         int index = 0;
         do
         {
-          int[] region = hiddenColumns.elementAt(index);
+          int[] region = hiddenColumns.get(index);
           if (alPos < region[0])
           {
             return region[0];
@@ -428,7 +461,7 @@ public class HiddenColumns
       int index = hiddenColumns.size() - 1;
       do
       {
-        int[] region = hiddenColumns.elementAt(index);
+          int[] region = hiddenColumns.get(index);
         if (alPos > region[1])
         {
           return region[1];
@@ -464,7 +497,7 @@ public class HiddenColumns
       int index = hiddenColumns.size() - 1;
       do
       {
-        int[] region = hiddenColumns.elementAt(index);
+          int[] region = hiddenColumns.get(index);
         if (pos > region[1])
         {
           return index;
@@ -511,7 +544,7 @@ public class HiddenColumns
 
       if (hiddenColumns == null)
       {
-        hiddenColumns = new Vector<>();
+        hiddenColumns = new ArrayList<>();
       }
 
       /*
@@ -520,14 +553,14 @@ public class HiddenColumns
        */
       for (int i = 0; i < hiddenColumns.size(); i++)
       {
-        int[] region = hiddenColumns.elementAt(i);
+        int[] region = hiddenColumns.get(i);
 
         if (end < region[0] - 1)
         {
           /*
            * insert discontiguous preceding range
            */
-          hiddenColumns.insertElementAt(new int[] { start, end }, i);
+          hiddenColumns.add(i, new int[] { start, end });
           return;
         }
 
@@ -574,7 +607,7 @@ public class HiddenColumns
     /*
      * remaining case is that the new range follows everything else
      */
-    hiddenColumns.addElement(new int[] { start, end });
+      hiddenColumns.add(new int[] { start, end });
     } finally
     {
       if (!alreadyLocked)
@@ -608,56 +641,6 @@ public class HiddenColumns
     }
   }
 
-  /**
-   * ColumnSelection
-   */
-  public HiddenColumns()
-  {
-  }
-
-  /**
-   * Copy constructor
-   * 
-   * @param copy
-   */
-  public HiddenColumns(HiddenColumns copy)
-  {
-    try
-    {
-
-      LOCK.readLock().lock();
-      if (copy != null)
-      {
-        if (copy.hiddenColumns != null)
-        {
-          hiddenColumns = copy.copyHiddenRegions();
-        }
-      }
-    }
-    finally
-    {
-      LOCK.readLock().unlock();
-    }
-  }
-
-  private Vector<int[]> copyHiddenRegions()
-  {
-    Vector<int[]> copy = new Vector<>(hiddenColumns.size());
-    for (int i = 0, j = hiddenColumns.size(); i < j; i++)
-    {
-      int[] rh;
-      int[] 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()
   {
     int size = 0;
@@ -671,7 +654,7 @@ public class HiddenColumns
     {
       int[] rh;
       int[] cp;
-      rh = hiddenColumns.elementAt(i);
+      rh = hiddenColumns.get(i);
       if (rh != null)
       {
         cp = new int[rh.length];
@@ -682,26 +665,7 @@ 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
-    {
-      LOCK.readLock().lock();
-      return copyHiddenRegions();
-    } finally
-    {
-      LOCK.readLock().unlock();
-    }
-  }
-
+  
   /**
    * 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
@@ -710,7 +674,7 @@ public class HiddenColumns
    * 
    * @return hidden regions as an ArrayList of [start,end] pairs
    */
-  public ArrayList<int[]> getHiddenColumnsCopyAsList()
+  public ArrayList<int[]> getHiddenColumnsCopy()
   {
     try
     {
@@ -744,12 +708,12 @@ public class HiddenColumns
         int hSize = hiddenColumns.size();
         for (int i = 0; i < hSize; i++)
         {
-          int[] region = hiddenColumns.elementAt(i);
+          int[] region = hiddenColumns.get(i);
           if (region[0] > start && start + change > region[1])
           {
             deletedHiddenColumns.add(region);
 
-            hiddenColumns.removeElementAt(i);
+            hiddenColumns.remove(i);
             i--;
             hSize--;
             continue;
@@ -796,7 +760,7 @@ public class HiddenColumns
       {
         for (int i = 0; i < hiddenColumns.size(); i++)
         {
-          int[] region = hiddenColumns.elementAt(i);
+          int[] region = hiddenColumns.get(i);
           if (region[0] >= start)
           {
             region[0] -= change;
@@ -807,7 +771,7 @@ public class HiddenColumns
           }
           if (region[1] < region[0])
           {
-            hiddenColumns.removeElementAt(i--);
+            hiddenColumns.remove(i--);
           }
 
           if (region[0] < 0)
@@ -902,7 +866,7 @@ public class HiddenColumns
     {
       LOCK.readLock().lock();
       int iSize = seqs.length;
-      String selections[] = new String[iSize];
+      String[] selections = new String[iSize];
       if (hiddenColumns != null && hiddenColumns.size() > 0)
       {
         for (int i = 0; i < iSize; i++)
@@ -1247,7 +1211,7 @@ public class HiddenColumns
       {
         for (int i = 0; i < hiddenColumns.size(); i++)
         {
-          int[] region = hiddenColumns.elementAt(i);
+          int[] region = hiddenColumns.get(i);
           for (int j = region[0]; j < region[1] + 1; j++)
           {
             sel.addElement(j);
@@ -1276,7 +1240,7 @@ public class HiddenColumns
       LOCK.writeLock().lock();
       for (int i = 0; i < hiddenColumns.size(); i++)
       {
-        int[] region = hiddenColumns.elementAt(i);
+        int[] region = hiddenColumns.get(i);
         if (start == region[0])
         {
           for (int j = region[0]; j < region[1] + 1; j++)
@@ -1284,7 +1248,7 @@ public class HiddenColumns
             sel.addElement(j);
           }
 
-          hiddenColumns.removeElement(region);
+          hiddenColumns.remove(region);
           break;
         }
       }
@@ -1307,16 +1271,16 @@ public class HiddenColumns
    * @param intervals
    * @return
    */
-  private boolean pruneIntervalVector(final List<int[]> shifts,
-          Vector<int[]> intervals)
+  private boolean pruneIntervalList(final List<int[]> shifts,
+          ArrayList<int[]> intervals)
   {
     boolean pruned = false;
     int i = 0;
     int j = intervals.size() - 1;
     int s = 0;
     int t = shifts.size() - 1;
-    int hr[] = intervals.elementAt(i);
-    int sr[] = shifts.get(s);
+    int[] hr = intervals.get(i);
+    int[] sr = shifts.get(s);
     while (i <= j && s <= t)
     {
       boolean trailinghn = hr[1] >= sr[0];
@@ -1324,7 +1288,7 @@ public class HiddenColumns
       {
         if (i < j)
         {
-          hr = intervals.elementAt(++i);
+          hr = intervals.get(++i);
         }
         else
         {
@@ -1352,12 +1316,12 @@ public class HiddenColumns
       {
         if (trailinghc)
         { // deleted hidden region.
-          intervals.removeElementAt(i);
+          intervals.remove(i);
           pruned = true;
           j--;
           if (i <= j)
           {
-            hr = intervals.elementAt(i);
+            hr = intervals.get(i);
           }
           continue;
         }
@@ -1411,7 +1375,7 @@ public class HiddenColumns
       // delete any intervals intersecting.
       if (hiddenColumns != null)
       {
-        pruneIntervalVector(shifts, hiddenColumns);
+        pruneIntervalList(shifts, hiddenColumns);
         if (hiddenColumns != null && hiddenColumns.size() == 0)
         {
           hiddenColumns = null;