JAL-3253 preliminary static fixes for JavaScript
[jalview.git] / src / jalview / analysis / AlignmentSorter.java
index 7ecce49..e5038ba 100755 (executable)
@@ -29,6 +29,7 @@ import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
 import jalview.datamodel.SequenceNode;
+import jalview.util.Platform;
 import jalview.util.QuickSort;
 
 import java.util.ArrayList;
@@ -53,38 +54,77 @@ import java.util.List;
  */
 public class AlignmentSorter
 {
+
+  static AlignmentSorter instance;
+
+  public static AlignmentSorter getInstance()
+  {
+
+    // BH 2019.05.08 need to isolate static fields in JavaScript
+
+    AlignmentSorter i = instance;
+    @SuppressWarnings("unused")
+    ThreadGroup g = null;
+    if (Platform.isJS())
+    {
+      g = Thread.currentThread().getThreadGroup();
+      /**
+       * @j2sNative i = g._jalviewScoreModelsInstance;
+       * 
+       */
+    }
+    if (i == null)
+    {
+      i = new AlignmentSorter();
+
+      if (Platform.isJS())
+      {
+        /**
+         * @j2sNative g._jalviewScoreModelsInstance = i;
+         * 
+         */
+      }
+      else
+      {
+        instance = i;
+      }
+    }
+    return i;
+  }
+
   /*
    * todo: refactor searches to follow a basic pattern: (search property, last
    * search state, current sort direction)
    */
-  static boolean sortIdAscending = true;
+  boolean sortIdAscending = true;
 
-  static int lastGroupHash = 0;
+  int lastGroupHash = 0;
 
-  static boolean sortGroupAscending = true;
+  boolean sortGroupAscending = true;
 
-  static AlignmentOrder lastOrder = null;
+  AlignmentOrder lastOrder = null;
 
-  static boolean sortOrderAscending = true;
+  boolean sortOrderAscending = true;
 
-  static TreeModel lastTree = null;
+  TreeModel lastTree = null;
 
-  static boolean sortTreeAscending = true;
+  boolean sortTreeAscending = true;
 
-  /*
+
+  /**
    * last Annotation Label used for sort by Annotation score
    */
-  private static String lastSortByAnnotation;
+  private String lastSortByAnnotation;
 
-  /*
-   * string hash of last arguments to sortByFeature
-   * (sort order toggles if this is unchanged between sorts)
+  /**
+   * string hash of last arguments to sortByFeature (sort order toggles if this
+   * is unchanged between sorts)
    */
-  private static String sortByFeatureCriteria;
+  private String sortByFeatureCriteria;
 
-  private static boolean sortByFeatureAscending = true;
+  private boolean sortByFeatureAscending = true;
 
-  private static boolean sortLengthAscending;
+  private boolean sortLengthAscending;
 
   /**
    * Sorts sequences in the alignment by Percentage Identity with the given
@@ -222,7 +262,8 @@ public class AlignmentSorter
 
     QuickSort.sort(ids, seqs);
 
-    if (sortIdAscending)
+    AlignmentSorter as = getInstance();
+    if (as.sortIdAscending)
     {
       setReverseOrder(align, seqs);
     }
@@ -231,7 +272,7 @@ public class AlignmentSorter
       setOrder(align, seqs);
     }
 
-    sortIdAscending = !sortIdAscending;
+    as.sortIdAscending = !as.sortIdAscending;
   }
 
   /**
@@ -255,7 +296,9 @@ public class AlignmentSorter
 
     QuickSort.sort(length, seqs);
 
-    if (sortLengthAscending)
+    AlignmentSorter as = getInstance();
+
+    if (as.sortLengthAscending)
     {
       setReverseOrder(align, seqs);
     }
@@ -264,7 +307,7 @@ public class AlignmentSorter
       setOrder(align, seqs);
     }
 
-    sortLengthAscending = !sortLengthAscending;
+    as.sortLengthAscending = !as.sortLengthAscending;
   }
 
   /**
@@ -281,14 +324,16 @@ public class AlignmentSorter
     // ORDERS BY GROUP SIZE
     List<SequenceGroup> groups = new ArrayList<>();
 
-    if (groups.hashCode() != lastGroupHash)
+    AlignmentSorter as = getInstance();
+
+    if (groups.hashCode() != as.lastGroupHash)
     {
-      sortGroupAscending = true;
-      lastGroupHash = groups.hashCode();
+      as.sortGroupAscending = true;
+      as.lastGroupHash = groups.hashCode();
     }
     else
     {
-      sortGroupAscending = !sortGroupAscending;
+      as.sortGroupAscending = !as.sortGroupAscending;
     }
 
     // SORTS GROUPS BY SIZE
@@ -328,7 +373,7 @@ public class AlignmentSorter
       }
     }
 
-    if (sortGroupAscending)
+    if (as.sortGroupAscending)
     {
       setOrder(align, seqs);
     }
@@ -401,16 +446,18 @@ public class AlignmentSorter
     // Get an ordered vector of sequences which may also be present in align
     List<SequenceI> tmp = order.getOrder();
 
-    if (lastOrder == order)
+    AlignmentSorter as = getInstance();
+
+    if (as.lastOrder == order)
     {
-      sortOrderAscending = !sortOrderAscending;
+      as.sortOrderAscending = !as.sortOrderAscending;
     }
     else
     {
-      sortOrderAscending = true;
+      as.sortOrderAscending = true;
     }
 
-    if (sortOrderAscending)
+    if (as.sortOrderAscending)
     {
       setOrder(align, tmp);
     }
@@ -473,18 +520,20 @@ public class AlignmentSorter
   {
     List<SequenceI> tmp = getOrderByTree(align, tree);
 
+    AlignmentSorter as = getInstance();
+
     // tmp should properly permute align with tree.
-    if (lastTree != tree)
+    if (as.lastTree != tree)
     {
-      sortTreeAscending = true;
-      lastTree = tree;
+      as.sortTreeAscending = true;
+      as.lastTree = tree;
     }
     else
     {
-      sortTreeAscending = !sortTreeAscending;
+      as.sortTreeAscending = !as.sortTreeAscending;
     }
 
-    if (sortTreeAscending)
+    if (as.sortTreeAscending)
     {
       setOrder(align, tmp);
     }
@@ -658,9 +707,12 @@ public class AlignmentSorter
     }
 
     jalview.util.QuickSort.sort(scores, seqs);
-    if (lastSortByAnnotation != scoreLabel)
+
+    AlignmentSorter as = getInstance();
+
+    if (as.lastSortByAnnotation != scoreLabel)
     {
-      lastSortByAnnotation = scoreLabel;
+      as.lastSortByAnnotation = scoreLabel;
       setOrder(alignment, seqs);
     }
     else
@@ -809,6 +861,8 @@ public class AlignmentSorter
       }
     }
 
+    boolean doSort = false;
+
     if (FEATURE_SCORE.equals(method))
     {
       if (hasScores == 0)
@@ -834,7 +888,7 @@ public class AlignmentSorter
           }
         }
       }
-      QuickSort.sortByDouble(scores, seqs, sortByFeatureAscending);
+      doSort = true;
     }
     else if (FEATURE_DENSITY.equals(method))
     {
@@ -846,9 +900,12 @@ public class AlignmentSorter
         // System.err.println("Sorting on Density: seq "+seqs[i].getName()+
         // " Feats: "+featureCount+" Score : "+scores[i]);
       }
-      QuickSort.sortByDouble(scores, seqs, sortByFeatureAscending);
+      doSort = true;
+    }
+    if (doSort)
+    {
+      QuickSort.sortByDouble(scores, seqs, getInstance().sortByFeatureAscending);
     }
-
     setOrder(alignment, seqs);
   }
 
@@ -883,16 +940,17 @@ public class AlignmentSorter
     /*
      * if resorting on the same criteria, toggle sort order
      */
-    if (sortByFeatureCriteria == null
-            || !scoreCriteria.equals(sortByFeatureCriteria))
+    AlignmentSorter as = getInstance();
+    if (as.sortByFeatureCriteria == null
+            || !scoreCriteria.equals(as.sortByFeatureCriteria))
     {
-      sortByFeatureAscending = true;
+      as.sortByFeatureAscending = true;
     }
     else
     {
-      sortByFeatureAscending = !sortByFeatureAscending;
+      as.sortByFeatureAscending = !as.sortByFeatureAscending;
     }
-    sortByFeatureCriteria = scoreCriteria;
+    as.sortByFeatureCriteria = scoreCriteria;
   }
 
 }