All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.polyglotted.common.util.EnumCache Maven / Gradle / Ivy

package io.polyglotted.common.util;

import com.google.common.collect.ImmutableMap;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static com.google.common.base.Preconditions.checkArgument;
import static io.polyglotted.common.util.ReflectionUtil.isEnum;

@SuppressWarnings({"unchecked", "unused", "WeakerAccess"})
public abstract class EnumCache {
    private static final ConcurrentMap, Map> enumValuesMap = new ConcurrentHashMap<>();

    public static  E fetchEnumFor(Class enumClass, String value) { return (E) fetchEnumValueFor(enumClass, value); }

    public static Object fetchEnumValueFor(Class enumClass, String value) {
        if (!enumValuesMap.containsKey(enumClass)) putEnumValuesInMap(enumClass);
        return enumValuesMap.get(enumClass).get(value);
    }

    private static  void putEnumValuesInMap(Class enumClass) {
        checkArgument(isEnum(enumClass));
        ImmutableMap.Builder values = ImmutableMap.builder();
        for (E value : enumClass.getEnumConstants()) {
            values.put(value.name(), value);
            if (!value.toString().equals(value.name())) { values.put(value.toString(), value); }
        }
        enumValuesMap.putIfAbsent(enumClass, values.build());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy