package water.util;

import water.H2O;

import java.util.Arrays;
import java.util.HashMap;

/** Utilities to deal with Java enums. */
public class EnumUtils {

  // Helper for .valueOf()
  private static HashMap<Class<? extends Enum>, HashMap<String, Enum>> enumMappings = new HashMap<>();

  /**
   * Return an array of Strings of all the enum levels.
   * <p>
   * Taken from http://stackoverflow.com/questions/13783295/getting-all-names-in-an-enum-as-a-string.
   */
  public static String[] getNames(Class<? extends Enum<?>> e) {
    return Arrays.toString(e.getEnumConstants()).replaceAll("^.|.$", "").split(", ");
  }

  /**
   * This is like Enum.valueOf() only better: it matches enum constants very loosely: case-insensitive and disregarding
   * any non-alphanumeric characters (e.g. "_"). For example, if Enum declares constant LOG_NORMAL, then all of the
   * following would also match to this constant:
   *     log_normal, logNormal, LogNormal, __LoGnOrmaL___, "LogNormal", $Log.Normal, ツlognormal, etc.
   *
   * @param <T> The enum type whose constant is to be returned
   * @param clz the {@code Class} object of the enum type from which to return a constant
   * @param name the name of the constant to return
   * @return the enum constant of the specified enum type with the specified name
   */
  public static <T extends Enum<T>> T valueOf(Class<T> clz, String name) {
    HashMap<String, Enum> map = enumMappings.get(clz);
    if (map == null) {
      map = new HashMap<>();
      enumMappings.put(clz, map);
      for (Enum item : clz.getEnumConstants()) {
        String origName = item.name();
        String unifName = origName.toUpperCase().replaceAll("[^0-9A-Z]", "");
        if (map.containsKey(origName)) throw H2O.fail("Unexpected key " + origName + " in enum " + clz);
        if (map.containsKey(unifName)) throw H2O.fail("Non-unique key " + unifName + " in enum " + clz);
        map.put(origName, item);
        map.put(unifName, item);
      }
    }

    Enum value = map.get(name);
    if (value == null && name != null)
      value = map.get(name.toUpperCase().replaceAll("[^0-9A-Z]", ""));

    if (value == null)
      throw new IllegalArgumentException("No enum constant " + clz.getCanonicalName() + "." + name);
    if (name == null)
      throw new NullPointerException("Name is null");

    // noinspection unchecked
    return (T) value;
  }

}

@@ -1496,42 +1496,8 @@
 ();%0A
-      enumMappings.put(clz, map);%0A
     
@@ -1942,16 +1942,560 @@
       %7D%0A
+      // Put the map into %7BenumMappings%7D no sooner than it is fully constructed. If there are multiple threads%0A      // accessing the same enum mapping, then it is possible they'll begin constructing the map simultaneously and%0A      // then overwrite each other's results. This is harmless.%0A      // However it would be an error to put the %7Bmap%7D into %7BenumMappings%7D before it is filled, because then the%0A      // other thread would think that the map is complete, and may not find some of the legitimate keys.%0A      enumMappings.put(clz, map);%0A
     %7D%0A%0A 
@@ -2567,31 +2567,35 @@
 ull)
+ %7B
 %0A      
-value = map.get(
+String unifName = 
 name
@@ -2636,18 +2636,188 @@
 Z%5D%22, %22%22)
-)
 ;
+%0A      value = map.get(unifName);%0A      // Save the mapping name -%3E value, so that subsequent requests with the same name will be faster.%0A      map.put(name, value);%0A    %7D
 %0A%0A    if

Solve a race condition in EnumUtils.valueOf, and also ensure that the names that were looked up once don't have to be canonicalized on every subsequent access.