List of usage examples for java.lang Enum valueOf
public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name)
From source file:Main.java
/** * Extracts the enum constant of the specified enum class with the * specified name. The name must match exactly an identifier used * to declare an enum constant in the given class. * * @param clazz the {@code Class} object of the enum type from which * to return a constant.//from w w w . j av a 2s . c om * @param name the name of the constant to return. * @return the enum constant of the specified enum type with the * specified name. * * @throws IllegalArgumentException if the specified enum type has * no constant with the specified name, or the specified * class object does not represent an enum type. * * @see {@link Enum#valueOf(Class, String)} */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static Object getEnumConstant(Class<?> clazz, String name) { if (clazz == null || name == null || name.isEmpty()) { return null; } return Enum.valueOf((Class<Enum>) clazz, name); }
From source file:Main.java
/** * Gets an enum value in the SharedPreference. * * @param sharedPreference a {@link SharedPreferences} to be loaded. * @param key a key name// w w w . j a v a 2s . c o m * @param type a class of enum value * @param defaultValue default value if the {@link SharedPreferences} doesn't have corresponding * entry. * @param conversionRecoveryValue default value if unknown value is stored. * For example, if the value is "ALPHA" and {@code type} doesn't have "ALPHA" entry, * this argument is returned. */ public static <T extends Enum<T>> T getEnum(SharedPreferences sharedPreference, String key, Class<T> type, T defaultValue, T conversionRecoveryValue) { if (sharedPreference == null) { return defaultValue; } String name = sharedPreference.getString(key, null); if (name != null) { try { return Enum.valueOf(type, name); } catch (IllegalArgumentException e) { return conversionRecoveryValue; } } return defaultValue; }
From source file:com.jaeksoft.searchlib.util.EnumerationUtils.java
public static <E extends Enum<E>> E lookup(Class<E> enumClass, String value, E defaultEnum) { if (value == null) return defaultEnum; try {// ww w .j av a 2s . c o m return Enum.valueOf(enumClass, value); } catch (IllegalArgumentException e) { return defaultEnum; } }
From source file:Main.java
public static <E extends Enum<E>> EnumSet<E> getEnumSetFromString(Class<E> enumType, String enumString) { EnumSet<E> es = EnumSet.noneOf(enumType); if (!Strings.isNullOrEmpty(enumString)) { String[] split = enumString.split(","); for (String s : split) { es.add(Enum.valueOf(enumType, s)); }//from w w w .j a v a 2 s.c o m } return es; }
From source file:Main.java
/** * Returns the enum constant of the specified enum type with the specified name. The * name must match exactly an identifier used to declare an enum constant in this * type. If it does not, this method returns <code>null</code>. * * @param enumType//from w ww . j a v a 2 s .co m * the Class object of the enum type from which to return a constant * @param name * the name of the constant to return * @return the enum constant of the specified enum type with the specified name or * <code>null</code> if not found * @see {link Enum.valueOf} */ public static <E extends Enum<E>> E valueOf(final Class<E> enumType, final String name) { try { return Enum.valueOf(enumType, name); } catch (final IllegalArgumentException e) { return null; } catch (final NullPointerException e) { return null; } }
From source file:org.raml.parser.utils.ConvertUtils.java
/** * <p>convertTo.</p>//from w w w. jav a2s . c om * * @param value a {@link java.lang.String} object. * @param type a {@link java.lang.Class} object. * @param <T> a T object. * @return a T object. */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> T convertTo(String value, Class<T> type) { if (type.isEnum()) { return type.cast(Enum.valueOf((Class) type, value.toUpperCase())); } Class<T> clazz = type; if (type.isPrimitive()) { clazz = ClassUtils.primitiveToWrapper(type); } if (clazz.getName().equals(Boolean.class.getName())) { return clazz.cast(booleanConverter.convert(Boolean.class, value)); } try { Constructor constructor = type.getConstructor(String.class); return (T) constructor.newInstance(value); } catch (Exception e) { //ignore; } return clazz.cast(org.apache.commons.beanutils.ConvertUtils.convert(value, type)); }
From source file:com.google.walkaround.util.server.flags.JsonFlags.java
private static <T extends Enum<T>> T parseEnumValue(Class<T> enumType, String key, String value) throws FlagFormatException { try {/*w w w . ja v a 2s. c om*/ return Enum.valueOf(enumType, value); } catch (IllegalArgumentException e) { throw new FlagFormatException("Invalid flag enum value " + value + " for key " + key + "; valid values: " + Arrays.toString(enumType.getEnumConstants()), e); } }
From source file:com.blackducksoftware.integration.hub.detect.util.EnumUtilExtension.java
public static <T extends Enum<T>> List<T> parseCommaDelimitted(String commaDelimitedEnumString, Class<T> enumClass) { return Arrays.stream(commaDelimitedEnumString.split(",")).map(String::trim).filter(StringUtils::isNotBlank) .map(token -> Enum.valueOf(enumClass, token)).collect(Collectors.toList()); }
From source file:Main.java
public static <T extends Enum<T>> T getAttributeEnum(Node node, String attributeName, Class<T> enumClass, T defaultValue, boolean throwOnError) { String value = getAttribute(node, attributeName, null); if (value == null || value == "") return defaultValue; try {// w w w . ja va2 s .c o m return Enum.valueOf(enumClass, value.toUpperCase()); } catch (IllegalArgumentException e) { if (throwOnError) throw e; // TODO: Log warning. return defaultValue; } }
From source file:com.metamx.cache.CacheExecutorFactory.java
@JsonCreator public static CacheExecutorFactory from(String str) { return Enum.valueOf(CacheExecutorFactory.class, str.toUpperCase()); }