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:och.util.Util.java

public static <T extends Enum<T>> T tryParseEnum(Object ob, Class<T> enumType, T defaultVal) {
    if (ob == null)
        return defaultVal;
    try {//from   w ww  . ja  va 2  s .  c  o m
        return (T) Enum.valueOf(enumType, String.valueOf(ob));
    } catch (Exception e) {
        return defaultVal;
    }
}

From source file:com.abssh.util.PropertyFilter.java

/**
 * @param filterName//from ww  w .  j  a v a  2 s  . com
 *            ,???. eg. LIKES_NAME_OR_LOGIN_NAME
 * @param value
 *            .
 */
@SuppressWarnings("unchecked")
public PropertyFilter(final String filterName, final Object value, boolean flag) {

    String matchTypeStr = StringUtils.substringBefore(filterName, "_");
    String matchTypeCode = StringUtils.substring(matchTypeStr, 0, matchTypeStr.length() - 1);
    String propertyTypeCode = StringUtils.substring(matchTypeStr, matchTypeStr.length() - 1,
            matchTypeStr.length());
    try {
        matchType = Enum.valueOf(MatchType.class, matchTypeCode);
    } catch (RuntimeException e) {
        throw new IllegalArgumentException(
                "filter??" + filterName + ",.", e);
    }

    try {
        propertyType = Enum.valueOf(PropertyType.class, propertyTypeCode).getValue();
    } catch (RuntimeException e) {
        throw new IllegalArgumentException(
                "filter??" + filterName + ",.", e);
    }

    String propertyNameStr = StringUtils.substringAfter(filterName, "_");
    propertyNames = propertyNameStr.split(PropertyFilter.OR_SEPARATOR);

    Assert.isTrue(propertyNames.length > 0,
            "filter??" + filterName + ",??.");
    // entity property.
    if (value != null && (value.getClass().isArray() || Collection.class.isInstance(value))) {
        // IN ?
        Object[] vObjects = null;
        if (value.getClass().isArray()) {
            vObjects = (Object[]) value;
        } else if (Collection.class.isInstance(value)) {
            vObjects = ((Collection) value).toArray();
        } else {
            vObjects = value.toString().split(",");
        }
        this.propertyValue = new Object[vObjects.length];
        for (int i = 0; i < vObjects.length; i++) {
            propertyValue[i] = ReflectionUtils.convertValue(vObjects[i], propertyType);
        }
    } else {
        Object tObject = ReflectionUtils.convertValue(value, propertyType);
        if (tObject != null && matchType == MatchType.LE && propertyType.getName().equals("java.util.Date")) {
            // LED ??tObject2010-08-13 00:00:00
            // ?2010-08-13 23:59:59
            Date leDate = (Date) tObject;
            Calendar c = GregorianCalendar.getInstance();
            c.setTime(leDate);
            c.set(Calendar.HOUR_OF_DAY, 23);
            c.set(Calendar.MINUTE, 59);
            c.set(Calendar.SECOND, 59);
            tObject = c.getTime();
        } else if (tObject != null && matchType == MatchType.LEN
                && propertyType.getName().equals("java.util.Date")) {
        } else if (tObject != null && matchType == MatchType.LIKE) {
            tObject = ((String) tObject).replace("%", "\\%").replace("_", "\\_");
        }
        this.propertyValue = new Object[] { tObject };
    }
}

From source file:com.hortonworks.streamline.streams.catalog.UDFInfo.java

@Override
public Storable fromMap(Map<String, Object> map) {
    ObjectMapper mapper = new ObjectMapper();
    setId((Long) map.get(ID));//from  w  w  w. j av a  2  s  .  com
    setName((String) map.get(NAME));
    setDisplayName((String) map.get(DISPLAYNAME));
    setDescription((String) map.get(DESCRIPTION));
    setClassName((String) map.get(CLASSNAME));
    setJarStoragePath((String) map.get(JARSTORAGEPATH));
    setDigest((String) map.get(DIGEST));
    String typeStr = (String) map.get(TYPE);
    try {
        if (!StringUtils.isEmpty(typeStr)) {
            setType(Enum.valueOf(Type.class, typeStr));
        }
        String argTypesStr = (String) map.get(ARGTYPES);
        if (!StringUtils.isEmpty(argTypesStr)) {
            List<String> argTypes = mapper.readValue(argTypesStr, new TypeReference<List<String>>() {
            });
            setArgTypes(argTypes);
        } else {
            setArgTypes(Collections.<String>emptyList());
        }
        String returnTypeStr = (String) map.get(RETURNTYPE);
        if (!StringUtils.isEmpty(returnTypeStr)) {
            setReturnType(Enum.valueOf(Schema.Type.class, returnTypeStr));
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    return this;
}

From source file:org.restlet.ext.oauth.AccessTokenServerResource.java

/**
 * Get request parameter "grant_type"./*w ww . ja v  a 2 s  .  c  om*/
 * 
 * @param params
 * @return
 * @throws OAuthException
 */
protected GrantType getGrantType(Form params) throws OAuthException {
    String typeString = params.getFirstValue(GRANT_TYPE);
    getLogger().info("Type: " + typeString);
    try {
        GrantType type = Enum.valueOf(GrantType.class, typeString);
        getLogger().fine("Found flow - " + type);
        return type;
    } catch (IllegalArgumentException iae) {
        throw new OAuthException(OAuthError.unsupported_grant_type, "Unsupported flow", null);
    } catch (NullPointerException npe) {
        throw new OAuthException(OAuthError.invalid_request, "No grant_type parameter found.", null);
    }
}

From source file:org.kuali.kra.irb.onlinereview.dao.ProtocolOnlineReviewDaoOjb.java

private CritField getCriteriaEnum(Entry<String, String> entry) {
    String searchKeyName = entry.getKey();
    CritField critField = Enum.valueOf(CritField.class, searchMap.get(searchKeyName));
    critField.setFieldValue(entry.getValue());
    return critField;
}

From source file:com.facebook.stetho.json.ObjectMapper.java

private Enum getEnumValue(String value, Class<? extends Enum> clazz) {
    Method method = getJsonValueMethod(clazz);
    if (method != null) {
        return getEnumByMethod(value, clazz, method);
    } else {//from  w w  w. ja va  2 s .c  om
        return Enum.valueOf(clazz, value);
    }
}

From source file:com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.java

/**
 * Retrieves an {@link Enum Enum} type from a @{link String String}.
 * @param <T> the enum type//w  w w.  j  a va 2s  .  c  o m
 * @param enumClass the enum class
 * @param value the string representing the enum
 * @return the enum type
 */
private static <T extends Enum<T>> T getOption(final Class<T> enumClass, final String value) {
    try {
        return Enum.valueOf(enumClass, value.trim().toUpperCase(Locale.ENGLISH));
    } catch (final IllegalArgumentException iae) {
        throw new ConversionException("unable to parse " + value, iae);
    }
}

From source file:org.sonar.api.server.ws.Request.java

@CheckForNull
public <E extends Enum<E>> E paramAsEnum(String key, Class<E> enumClass) {
    String s = param(key);/*from   www.  j av a2 s.  c  o  m*/
    return s == null ? null : Enum.valueOf(enumClass, s);
}

From source file:be.fedict.eid.pkira.blm.model.reporting.ReportManagerBean.java

/**
 * @param contractType/*from  w ww . j  a  v a 2 s  .c o  m*/
 * @return
 */
private be.fedict.eid.pkira.generated.reports.ContractType mapContractType(ContractType contractType) {
    return Enum.valueOf(be.fedict.eid.pkira.generated.reports.ContractType.class, contractType.name());
}

From source file:com.hortonworks.streamline.streams.catalog.UDF.java

@Override
public Storable fromMap(Map<String, Object> map) {
    ObjectMapper mapper = new ObjectMapper();
    setId((Long) map.get(ID));//from   w w w.j  av a  2  s .  co m
    setName((String) map.get(NAME));
    setDisplayName((String) map.get(DISPLAYNAME));
    setDescription((String) map.get(DESCRIPTION));
    setClassName((String) map.get(CLASSNAME));
    setJarStoragePath((String) map.get(JARSTORAGEPATH));
    setDigest((String) map.get(DIGEST));
    if (map.get(BUILTIN) != null) {
        setBuiltin(Boolean.valueOf(((String) map.get(BUILTIN)).trim()));
    }

    String typeStr = (String) map.get(TYPE);
    try {
        if (!StringUtils.isEmpty(typeStr)) {
            setType(Enum.valueOf(Type.class, typeStr));
        }
        String argTypesStr = (String) map.get(ARGTYPES);
        if (!StringUtils.isEmpty(argTypesStr)) {
            List<String> argTypes = mapper.readValue(argTypesStr, new TypeReference<List<String>>() {
            });
            setArgTypes(argTypes);
        } else {
            setArgTypes(Collections.<String>emptyList());
        }
        String returnTypeStr = (String) map.get(RETURNTYPE);
        if (!StringUtils.isEmpty(returnTypeStr)) {
            setReturnType(Enum.valueOf(Schema.Type.class, returnTypeStr));
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    return this;
}