Example usage for java.lang Enum valueOf

List of usage examples for java.lang Enum valueOf

Introduction

In this page you can find the example usage for java.lang Enum valueOf.

Prototype

public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) 

Source Link

Document

Returns the enum constant of the specified enum type with the specified name.

Usage

From source file:org.opencb.biodata.tools.variant.converter.VariantContextToVariantConverter.java

/**
 * @param variantType/*from w w w. java2  s  .c o m*/
 * @param string
 * @return
 */
private static <E extends Enum<E>> E getEnumFromString(Class<E> variantType, String string) {
    if (variantType != null && string != null) {
        try {
            return Enum.valueOf(variantType, string.trim().toUpperCase());
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Unknown variantType " + string);
        }
    }
    return null;
}

From source file:org.apache.openjpa.persistence.jdbc.XMLPersistenceMappingParser.java

/**
 * Parse map-key-enumerated./*  w  ww.  j  a v  a2s  .com*/
 */
private void endMapKeyEnumerated() {
    String text = currentText();
    if (StringUtils.isEmpty(text))
        return;
    EnumType type = Enum.valueOf(EnumType.class, text);

    FieldMapping fm = (FieldMapping) currentElement();
    String strat = EnumValueHandler.class.getName() + "(StoreOrdinal="
            + String.valueOf(type == EnumType.ORDINAL) + ")";
    fm.getKeyMapping().getValueInfo().setStrategy(strat);
}

From source file:com.buaa.cfs.utils.SecurityUtil.java

public static UserGroupInformation.AuthenticationMethod getAuthenticationMethod(Configuration conf) {
    String value = conf.get(CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION, "simple");
    try {//w w w. j  av  a2  s  .  c o  m
        return Enum.valueOf(UserGroupInformation.AuthenticationMethod.class, StringUtils.toUpperCase(value));
    } catch (IllegalArgumentException iae) {
        throw new IllegalArgumentException("Invalid attribute value for "
                + CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION + " of " + value);
    }
}

From source file:ch.algotrader.esper.EngineImpl.java

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void setVariableValueFromString(String variableName, String value) {

    variableName = variableName.replace(".", "_");
    EPRuntime runtime = this.serviceProvider.getEPRuntime();
    if (runtime.getVariableValueAll().containsKey(variableName)) {
        Class clazz = runtime.getVariableValue(variableName).getClass();

        Object castedObj = null;//from  ww  w  .j  a  v a2  s . com
        if (clazz.isEnum()) {
            castedObj = Enum.valueOf(clazz, value);
        } else {
            castedObj = JavaClassHelper.parse(clazz, value);
        }
        runtime.setVariableValue(variableName, castedObj);
    }
}

From source file:org.dozer.MappingProcessor.java

private <T extends Enum<T>> T mapEnum(Enum<T> srcFieldValue, Class<T> destFieldType) {
    String name = srcFieldValue.name();
    return Enum.valueOf(destFieldType, name);
}

From source file:edu.utah.further.core.api.text.StringUtil.java

/**
 * Convert a string to an enum. Encapsulates unchecked warning suppression.
 * //  w  w  w  . j ava  2 s. c  om
 * @param enumType
 * @param value
 * @return
 */
public static <T extends Enum<T>> T getAsEnum(final Class<T> enumType, final String value) {
    try {
        return Enum.valueOf(enumType, value);
    } catch (final Exception e) {
        return null;
    }
}

From source file:com.heliosphere.demeter.base.runner.AbstractRunner.java

/**
 * Determines the parameters' type.//from   www  .  ja  v a  2s. com
 * <hr>
 * @param enumClass Parameter enumeration class to use to determine the parameters' type.
 * @throws ParameterException Thrown in case an error occurred when determining a parameter type.
 */
@SuppressWarnings({ "unchecked", "rawtypes", "nls" })
private final void determineParameterType(final Class enumClass) throws ParameterException {
    Method method;
    String name = null;
    String value = null;

    this.enumParameterClass = enumClass;
    Object o = Enum.valueOf(enumClass, "UNKNOWN");

    try {
        method = enumClass.getDeclaredMethod("fromName", String.class);

        for (IParameterConfiguration parameter : configuration.getContent().getElements()) {
            value = parameter.getName();
            parameter.setType((Enum<? extends IParameterType>) method.invoke(o, value));
            parameter.setEntityType(((IParameterType) parameter.getType()).getEntityType());
            if (parameter.getType() == null) {
                throw new ParameterException("No parameter definition found for: " + value);
            }
        }

        for (IParameterExecution parameter : execution.getContent().getElements()) {
            name = parameter.getName();
            parameter.setType((Enum<? extends IParameterType>) method.invoke(o, name));
            parameter.setEntityType(((IParameterType) parameter.getType()).getEntityType());
            parameter.setStatus(ParameterStatusType.UNPROCESSED);
            parameter.setConfiguration(configuration.getParameter(parameter.getType()));
        }
    } catch (Exception e) {
        throw new ParameterException(
                String.format("Unable to create enumerated value for enumeration: %1s, parameter: %2s",
                        enumClass.getName(), name == null ? value : name));
    }

    checkParameterDefinitionAgainstEnumeration(o.getClass());
}

From source file:org.apache.tajo.catalog.store.DBStore.java

private Type getDataType(final String typeStr) {
    try {/*from w ww.j a v a  2s  .co m*/
        return Enum.valueOf(Type.class, typeStr);
    } catch (IllegalArgumentException iae) {
        LOG.error("Cannot find a matched type aginst from '" + typeStr + "'");
        return null;
    }
}

From source file:org.jenkinsci.plugins.workflow.structs.DescribableHelper.java

@SuppressWarnings("unchecked")
private static Object coerce(String context, Type type, @Nonnull Object o) throws Exception {
    if (type instanceof Class) {
        o = ReflectionCache.getCachedClass((Class) type).coerceArgument(o);
    }//from  w w w . j a v  a 2  s .  c  o m
    if (type instanceof Class && Primitives.wrap((Class) type).isInstance(o)) {
        return o;
    } else if (o instanceof Map) {
        Map<String, Object> m = new HashMap<String, Object>();
        for (Map.Entry<?, ?> entry : ((Map<?, ?>) o).entrySet()) {
            m.put((String) entry.getKey(), entry.getValue());
        }

        String clazzS = (String) m.remove(CLAZZ);
        Class<?> clazz;
        if (clazzS == null) {
            if (Modifier.isAbstract(((Class) type).getModifiers())) {
                throw new UnsupportedOperationException(
                        "must specify " + CLAZZ + " with an implementation of " + type);
            }
            clazz = (Class) type;
        } else if (clazzS.contains(".")) {
            Jenkins j = Jenkins.getInstance();
            ClassLoader loader = j != null ? j.getPluginManager().uberClassLoader
                    : DescribableHelper.class.getClassLoader();
            clazz = loader.loadClass(clazzS);
        } else if (type instanceof Class) {
            clazz = null;
            for (Class<?> c : findSubtypes((Class<?>) type)) {
                if (c.getSimpleName().equals(clazzS)) {
                    if (clazz != null) {
                        throw new UnsupportedOperationException(clazzS + " as a " + type + " could mean either "
                                + clazz.getName() + " or " + c.getName());
                    }
                    clazz = c;
                }
            }
            if (clazz == null) {
                throw new UnsupportedOperationException(
                        "no known implementation of " + type + " is named " + clazzS);
            }
        } else {
            throw new UnsupportedOperationException("JENKINS-26535: do not know how to handle " + type);
        }
        return instantiate(clazz.asSubclass((Class<?>) type), m);
    } else if (o instanceof String && type instanceof Class && ((Class) type).isEnum()) {
        return Enum.valueOf(((Class) type).asSubclass(Enum.class), (String) o);
    } else if (o instanceof String && type == URL.class) {
        return new URL((String) o);
    } else if (o instanceof String && (type == char.class || type == Character.class)
            && ((String) o).length() == 1) {
        return ((String) o).charAt(0);
    } else if (o instanceof List && type instanceof Class && ((Class) type).isArray()) {
        Class<?> componentType = ((Class) type).getComponentType();
        List<Object> list = mapList(context, componentType, (List) o);
        return list.toArray((Object[]) Array.newInstance(componentType, list.size()));
    } else if (o instanceof List && acceptsList(type)) {
        return mapList(context, ((ParameterizedType) type).getActualTypeArguments()[0], (List) o);
    } else {
        throw new ClassCastException(context + " expects " + type + " but received " + o.getClass());
    }
}

From source file:demo.config.PropertyConverter.java

/**
 * Convert the specified value into a Java 5 enum.
 * /*from  w ww .j  a  va  2s .co m*/
 * @param value
 *            the value to convert
 * @param cls
 *            the type of the enumeration
 * @return the converted value
 * @throws ConversionException
 *             thrown if the value cannot be converted to an enumeration
 * 
 * @since 1.5
 */
static <E extends Enum<E>> E toEnum(Object value, Class<E> cls) throws ConversionException {
    if (value.getClass().equals(cls)) {
        return cls.cast(value);
    } else if (value instanceof String) {
        try {
            return Enum.valueOf(cls, (String) value);
        } catch (Exception e) {
            throw new ConversionException("The value " + value + " can't be converted to a " + cls.getName());
        }
    } else if (value instanceof Number) {
        try {
            E[] enumConstants = cls.getEnumConstants();
            return enumConstants[((Number) value).intValue()];
        } catch (Exception e) {
            throw new ConversionException("The value " + value + " can't be converted to a " + cls.getName());
        }
    } else {
        throw new ConversionException("The value " + value + " can't be converted to a " + cls.getName());
    }
}