
com.github.wwadge.hbnpojogen.persistence.impl.StringValuedEnumReflect Maven / Gradle / Ivy
The newest version!
/**
* Utility class designed to inspect StringValuedEnums.
*
* @author wallacew
*/
package com.github.wwadge.hbnpojogen.persistence.impl;
public class StringValuedEnumReflect {
/**
* Don't let anyone instantiate this class.
* @throws UnsupportedOperationException Always.
*/
private StringValuedEnumReflect() {
throw new UnsupportedOperationException("This class must not be instantiated.");
}
/**
* All Enum constants (instances) declared in the specified class.
* @param enumClass Class to reflect
* @param Type
* @return Array of all declared EnumConstants (instances).
*/
@SuppressWarnings("unchecked")
private static T[]
getValues(Class enumClass) {
return enumClass.getEnumConstants();
}
/**
* All possible string values of the string valued enum.
* @param enumClass Class to reflect.
* @param Type
* @return Available string values.
*/
@SuppressWarnings("unchecked")
public static String[]
getStringValues(Class enumClass) {
T[] values = getValues(enumClass);
String[] result = new String[values.length];
for (int i = 0; i < values.length; i++) {
result[i] = values[i].getValue();
}
return result;
}
/**
* Name of the enum instance which hold the especified string value.
* If value has duplicate enum instances than returns the first occurency.
* @param enumClass Class to inspect.
* @param value String.
* @param Type
* @return name of the enum instance.
*/
@SuppressWarnings("unchecked")
public static String
getNameFromValue(Class enumClass, Object value) {
T[] values = getValues(enumClass);
for (int i = 0; i < values.length; i++) {
if (values[i].getValue().toString().equals(value.toString())) {
return values[i].name();
}
}
return "";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy