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 io.polyglotted.common.model.MapResult;
import io.polyglotted.common.util.MapBuilder.ImmutableMapBuilder;

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

import static io.polyglotted.common.util.Assertions.checkBool;
import static io.polyglotted.common.util.MapBuilder.immutableMapBuilder;
import static io.polyglotted.common.util.ReflectionUtil.isEnum;

@SuppressWarnings({"unchecked", "unused", "WeakerAccess"})
public abstract class EnumCache {
    private static final ConcurrentMap, MapResult> 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) {
        checkBool(isEnum(enumClass), enumClass + " not an enum class");
        ImmutableMapBuilder values = immutableMapBuilder();
        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.result());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy