com.nativelibs4java.util.EnumValues Maven / Gradle / Ivy
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.nativelibs4java.util;
import java.util.*;
/**
* Methods to ease conversion between EnumValue-annotated enums and their integer value.
* @author ochafik
*/
public class EnumValues {
private static class Cache> {
final Map enumsByValue = new LinkedHashMap();
final Map valuesByEnum = new LinkedHashMap();
public Cache(Class enumClass) {
if (ValuedEnum.class.isAssignableFrom(enumClass)) {
for (E e : enumClass.getEnumConstants()) {
long value = ((ValuedEnum)e).value();
enumsByValue.put(value, e);
valuesByEnum.put(e, value);
}
} else {
for (E e : enumClass.getEnumConstants()) {
EnumValue ev = null;
try {
ev = enumClass.getField(e.name()).getAnnotation(EnumValue.class);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
if (ev == null) {
throw new IllegalArgumentException("Enum value is not annotated with the " + EnumValue.class.getName() + " annotation : " + e);
}
long value = ev.value();
enumsByValue.put(value, e);
valuesByEnum.put(e, value);
}
}
}
}
private static final Map>, Cache>> caches = new HashMap>, Cache>>();
@SuppressWarnings("unchecked")
private static synchronized > Cache getCache(Class enumClass) {
Cache cache = (Cache) caches.get(enumClass);
if (cache == null) {
caches.put(enumClass, cache = new Cache(enumClass));
}
return cache;
}
/**
* Get the first enum item in enum class E which EnumValue value is equal to value
* @param type of the enum
* @param value
* @param enumClass
* @return first enum item with matching value, null if there is no matching enum item
*/
public static > E getEnum(long value, Class enumClass) {
return getCache(enumClass).enumsByValue.get(value);
}
/**
* Get the set of all the enum item in enum class E which EnumValue value flags are all present in value
* @param type of the enum
* @param value
* @param enumClass
* @return enum items with matching value flags
*/
public static > EnumSet getEnumSet(long value, Class enumClass) {
EnumSet set = EnumSet.noneOf(enumClass);
for (Map.Entry pair : getCache(enumClass).enumsByValue.entrySet()) {
long ev = pair.getKey();
if ((ev & value) == ev) {
set.add(pair.getValue());
}
}
return set;
}
/**
* Get the integer value associated with an enum item
* @see EnumValue
* @param enumItem
* @return the numeric value of the enum
*/
@SuppressWarnings("unchecked")
public static > long getValue(E enumItem) {
return getCache((Class) enumItem.getDeclaringClass()).valuesByEnum.get(enumItem);
}
/**
* Get the integer value resulting from ORing all the values of all the enum items present in the enum set.
* @see EnumValues#getValue(java.lang.Enum)
* @see EnumValue
* @param set the EnumSet to process
* @return the OR of all the values of the enums in the set
*/
public static > long getValue(EnumSet set) {
long v = 0;
for (E e : set) {
v |= getValue(e);
}
return v;
}
}