package au.com.southsky.jfreesane;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

/**
 * Utilities for dealing with instances of {@link SaneEnum}.
 *
 * @author James Ring (sjr@jdns.org)
 */
final class SaneEnums {
  private static Map<Class<?>, Map<Integer, ?>> cachedTypeMaps = Maps.newHashMap();

  // no public constructor
  private SaneEnums() {}

  @SuppressWarnings("unchecked")
  private static synchronized <T extends Enum<T> & SaneEnum> Map<Integer, T> mapForType(
      Class<T> enumType) {
    if (cachedTypeMaps.containsKey(enumType)) {
      return (Map<Integer, T>) cachedTypeMaps.get(enumType);
    }

    ImmutableMap.Builder<Integer, T> mapBuilder = ImmutableMap.builder();

    for (T value : enumType.getEnumConstants()) {
      mapBuilder.put(value.getWireValue(), value);
    }

    Map<Integer, T> result = mapBuilder.build();

    cachedTypeMaps.put(enumType, result);
    return result;
  }

  /**
   * Returns a set of {@code T} obtained by treating {@code wireValue} as a bit vector whose bits
   * represent the wire values of the enum constants of the given {@code enumType}.
   */
  public static <T extends Enum<T> & SaneEnum> Set<T> enumSet(Class<T> enumType, int wireValue) {
    T[] enumConstants = enumType.getEnumConstants();
    List<T> values = Lists.newArrayListWithCapacity(enumConstants.length);

    for (T value : enumConstants) {
      if ((wireValue & value.getWireValue()) != 0) {
        values.add(value);
      }
    }

    return Sets.immutableEnumSet(values);
  }

  public static <T extends Enum<T> & SaneEnum> T valueOf(Class<T> enumType, int valueType) {
    return mapForType(enumType).get(valueType);
  }

  public static <T extends Enum<T> & SaneEnum> T valueOf(Class<T> enumType, SaneWord value) {
    return valueOf(enumType, value.integerValue());
  }
}

@@ -46,20 +46,27 @@
 va.util.
-List
+Collections
 ;%0Aimport
@@ -76,19 +76,23 @@
 va.util.
-Map
+EnumSet
 ;%0Aimport
@@ -106,138 +106,37 @@
 til.
-Set;%0A%0Aimport com.google.common.collect.ImmutableMap;%0Aimport com.google.common.collect.Lists;%0Aimport com.google.common.collect
+HashMap;%0Aimport java.util
 .Map
-s
 ;%0Aim
@@ -144,38 +144,21 @@
 ort 
-com.google.common.collect
+java.util
 .Set
-s
 ;%0A%0A/
@@ -358,23 +358,21 @@
 s = 
-Maps.
 new
+ 
 HashMap
+%3C%3E
 ();%0A
@@ -698,74 +698,43 @@
     
-ImmutableMap.Builder%3CInteger, T%3E mapBuilder = ImmutableMap.builder
+Map%3CInteger, T%3E map = new HashMap%3C%3E
 ();%0A
@@ -793,23 +793,16 @@
      map
-Builder
 .put(val
@@ -868,29 +868,42 @@
 t = 
-mapBuilder.build(
+Collections.unmodifiableMap(map
 );%0A
-%0A
     
@@ -1252,24 +1252,70 @@
 ireValue) %7B%0A
+    Set%3CT%3E result = EnumSet.noneOf(enumType);%0A
     T%5B%5D enum
@@ -1358,83 +1358,8 @@
 s();
-%0A    List%3CT%3E values = Lists.newArrayListWithCapacity(enumConstants.length);
 %0A%0A  
@@ -1453,22 +1453,22 @@
         
-values
+result
 .add(val
@@ -1502,37 +1502,14 @@
 urn 
-Sets.immutableEnumSet(values)
+result
 ;%0A  

remove guava dependencies from SaneEnums