tech.ytsaurus.core.StringValueEnumResolver Maven / Gradle / Ivy
package tech.ytsaurus.core;
import java.util.HashMap;
import java.util.Map;
public class StringValueEnumResolver & StringValueEnum> {
private final Class enumClass;
private final Map valuesByName = new HashMap<>();
public StringValueEnumResolver(Class enumClass) {
this.enumClass = enumClass;
for (T value : enumClass.getEnumConstants()) {
valuesByName.put(value.value(), value);
}
}
public T fromName(String name) {
if (name == null) {
throw new IllegalArgumentException("value must not be null; for enum of type " + enumClass.getName());
}
if (valuesByName.containsKey(name)) {
return valuesByName.get(name);
}
throw new IllegalArgumentException("no enum of type " + enumClass.getName() + " found with name " + name);
}
public static & StringValueEnum> StringValueEnumResolver of(Class enumClass) {
return new StringValueEnumResolver<>(enumClass);
}
}