|
|
@@ -20,41 +20,45 @@ public class EnumHashMap<K,V extends EnumHash<K>> {
|
|
|
private final Map<K,V> mMap = new HashMap<K, V>();
|
|
|
|
|
|
public static class DuplicateEnumKey extends Error {
|
|
|
- private final String mKey;
|
|
|
- private final String mValuePrev;
|
|
|
- private final String mValueCurrent;
|
|
|
-
|
|
|
- public DuplicateEnumKey(Object key, Object valueKey, Object currentValue) {
|
|
|
- mKey = (null==key) ? "" : key.toString();
|
|
|
- mValuePrev = (null==valueKey) ? "" : valueKey.toString();
|
|
|
- mValueCurrent = (null==currentValue) ? "" : currentValue.toString();
|
|
|
+ private final Enum<?> first;
|
|
|
+ private final Enum<?> second;
|
|
|
+ private final Object value;
|
|
|
+
|
|
|
+ public DuplicateEnumKey(Enum<?> first, Enum<?> second, Object value) {
|
|
|
+ this.first = first;
|
|
|
+ this.second = second;
|
|
|
+ this.value = value;
|
|
|
}
|
|
|
-
|
|
|
- public String key() {
|
|
|
- return mKey;
|
|
|
+
|
|
|
+ public Class<?> type() {
|
|
|
+ return first.getDeclaringClass();
|
|
|
}
|
|
|
|
|
|
- public String prevValue() {
|
|
|
- return mValuePrev;
|
|
|
+ public Enum<?> first() {
|
|
|
+ return first;
|
|
|
}
|
|
|
|
|
|
- public String currentValue() {
|
|
|
- return mValueCurrent;
|
|
|
+ public Enum<?> second() {
|
|
|
+ return second;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object value() {
|
|
|
+ return value;
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
public EnumHashMap(Class<V> clazz) {
|
|
|
-
|
|
|
- V[] values = clazz.getEnumConstants();
|
|
|
- if(null == values) {
|
|
|
+ V[] items = clazz.getEnumConstants();
|
|
|
+ if(null == items) {
|
|
|
throw new AssertionError("Attempt to create HashEnumMap from non-enum class");
|
|
|
}
|
|
|
- for(V value : values) {
|
|
|
- K key = value.toEnumKey();
|
|
|
+ for(V current : items) {
|
|
|
+ K key = current.toEnumKey();
|
|
|
if(mMap.containsKey(key) ) {
|
|
|
- throw new DuplicateEnumKey(key, mMap.get(key), value);
|
|
|
+ throw new DuplicateEnumKey((Enum)mMap.get(key), (Enum)current, key);
|
|
|
}
|
|
|
- mMap.put(key, value);
|
|
|
+ mMap.put(key, current);
|
|
|
}
|
|
|
}
|
|
|
|