JAL-3560 Platform fixes.
[jalview.git] / src / jalview / util / Platform.java
index 3bce7f0..b779c20 100644 (file)
@@ -36,7 +36,9 @@ import java.io.InputStreamReader;
 import java.io.Reader;
 import java.net.URL;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Properties;
+import java.util.Set;
 import java.util.logging.ConsoleHandler;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -450,8 +452,10 @@ public class Platform
 
   public static void addJ2SBinaryType(String ext)
   {
-
-    jsutil.addBinaryFileType(ext);
+    if (isJS())
+    {
+      jsutil.addBinaryFileType(ext);
+    }
   }
 
   /**
@@ -508,8 +512,7 @@ public class Platform
 
   public static Object parseJSON(String json) throws ParseException
   {
-    return (isJS() ? JSON.parse(json)
-            : new JSONParser().parse(json));
+    return (isJS() ? JSON.parse(json) : new JSONParser().parse(json));
   }
 
   public static Object parseJSON(Reader r)
@@ -595,7 +598,6 @@ public class Platform
   @SuppressWarnings("unused")
   public static void getURLCommandArguments()
   {
-
     if (!isJS())
     {
       return;
@@ -626,14 +628,20 @@ public class Platform
 
   public static void ensureJmol()
   {
-    jsutil.loadResourceIfClassUnknown("core/core_jvjmol.z.js",
-            "org.jmol.viewer.Viewer");
+    if (isJS())
+    {
+      jsutil.loadResourceIfClassUnknown("core/core_jvjmol.z.js",
+              "org.jmol.viewer.Viewer");
+    }
   }
 
   public static void ensureRegex()
   {
+    if (isJS())
+    {
     jsutil.loadResourceIfClassUnknown("core/core_stevesoft.z.js",
             "com.stevesoft.pat.Regex");
+    }
   }
 
   public static Regex newRegex(String searchString, String replaceString)
@@ -747,9 +755,43 @@ public class Platform
    * 
    * @return Map for static singleton classes unique to a given applet
    */
-  public static HashMap<?,?> getJSSingletons()
+  public static HashMap<?, ?> getJSSingletons()
   {
     return (isJS() ? jsutil.getJSContext("jssingletons") : null);
   }
 
+  /**
+   * By designating initialCapacity and loadFactor, we tell SwingJS to use a
+   * standard (slower) Java HashMap to back this HashSet, thus providing exactly
+   * the same iterator order (until a new Java version changes it!)
+   * 
+   * @return a standard Java HashSet
+   */
+  public static Set<String> getJavaOrderedHashSet()
+  {
+    return new HashSet<>(16, 0.75f);
+  }
+
+  /**
+   * Switch the flag in SwingJS to use or not use the JavaScript Map object in
+   * any Hashtable, HashMap, or HashSet. Default is enabled.
+   * 
+   * For testing purposes only.
+   * 
+   */
+  public static boolean setJavaScriptMapObjectEnabled(boolean enabled)
+  {
+    if (!isJS())
+    {
+      return false;
+    }
+    jsutil.setJavaScriptMapObjectEnabled(enabled);
+    HashSet<String> hs = new HashSet<>();
+    // Java hash table iterator in HashMap will return "one" before "two"
+    // because of its hash code;
+    // JavaScript Map object will return "two" first because it was added first.
+    hs.add("two");
+    hs.add("one");
+    return (hs.iterator().next() == (enabled ? "two" : "one"));
+  }
 }