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:eu.sathra.io.IO.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private Object getValue(JSONObject jObj, String param, Class<?> clazz)
        throws ArrayIndexOutOfBoundsException, IllegalArgumentException, Exception {
    try {/*w  w  w  .  java 2 s  .c  om*/
        if (clazz.equals(String.class)) {
            return jObj.getString(param);
        } else if (clazz.equals(int.class)) {
            return jObj.getInt(param);
        } else if (clazz.equals(long.class)) {
            return jObj.getLong(param);
        } else if (clazz.equals(float.class)) {
            return (float) jObj.getDouble(param);
        } else if (clazz.equals(double.class)) {
            return jObj.getDouble(param);
        } else if (clazz.equals(boolean.class)) {
            return jObj.getBoolean(param);
        } else if (mAdapters.containsKey(clazz)) {
            return mAdapters.get(clazz).load(param, jObj);
        } else if (clazz.isEnum()) {
            return Enum.valueOf((Class<? extends Enum>) clazz, jObj.getString(param));
        } else if (clazz.isArray()) {
            return getValue(jObj.getJSONArray(param), clazz.getComponentType());
        } else {
            return load(jObj.getJSONObject(param), clazz);
        }
    } catch (JSONException e) {
        return null;
    } finally {
        jObj.remove(param);
    }

}

From source file:com.dssmp.agent.config.Configuration.java

/**
 * @param enumType//from   ww w .  j a  v a  2s.  c  o  m
 * @param key
 * @return the enum constant at given key, or <code>fallback</code> if the
 * key does not exist in the configuration.
 * @throws ConfigurationException if the enum constant failed to be returned.
 */
public <E extends Enum<E>> E readEnum(Class<E> enumType, String key) {
    try {
        return Enum.valueOf(enumType, readString(key));
    } catch (Exception e) {
        throw new ConfigurationException(String.format(
                "Failed to return the enum constant of the enum type(%s): %s", enumType.toString(), key), e);
    }
}

From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java

@SuppressWarnings("unchecked")
private Object toExactObject(Object value) {
    DBObject bson = (DBObject) value;/*from www  .  j ava 2 s  . co  m*/

    if (bson.containsField(Constants.TYPE) && bson.containsField(Constants.VALUE)) {
        String t = (String) bson.get(Constants.TYPE);

        if (Constants.CUSTOM_BINARY.equals(t)) {
            Object result = deserializeObject(bson);

            return result;
        } else {
            try {
                @SuppressWarnings("rawtypes")
                Class type = Class.forName(t);

                if (type.isEnum())
                    return Enum.valueOf(type, (String) bson.get(Constants.VALUE));
                else
                    return fromSpecialType((DBObject) value);

            } catch (ClassNotFoundException e) {
            }
        }
    }

    return toDocument(bson);
}

From source file:net.pandoragames.far.ui.swing.component.MacOSXMenuAdapter.java

/**
 * Returns true if a handler (ActionListener) has been defined for the 
 * specified method. Returns false otherwise.
 * @param methodName representing the command to be executed.
 * @return true if a handler has been defined
 *//*from   www  .  java  2s.  c  o  m*/
private boolean canHandle(String methodName) {
    String command = methodName.substring(METHODPREFIX.length());
    try {
        OSXCOMMAND cmd = Enum.valueOf(OSXCOMMAND.class, command);
        return listenerMap.containsKey(cmd.name());
    } catch (IllegalArgumentException iax) {
        logger.warn("Method " + methodName + " is not supported (unknown command: " + command + ")");
        return false;
    }
}

From source file:io.liveoak.security.policy.drools.integration.DroolsPolicyRootResourceTest.java

private void assertAuthzDecision(RequestContext reqCtxToCheck, ResourceState reqResourceState,
        AuthzDecision expectedDecision) throws Exception {
    RequestAttributes attribs = new DefaultRequestAttributes();
    attribs.setAttribute(AuthzConstants.ATTR_REQUEST_CONTEXT, reqCtxToCheck);
    attribs.setAttribute(AuthzConstants.ATTR_REQUEST_RESOURCE_STATE, reqResourceState);
    RequestContext reqCtx = new RequestContext.Builder().requestAttributes(attribs).build();
    ResourceState state = client.read(reqCtx, "/testApp/drools-policy/authzCheck");
    String decision = (String) state.getProperty(AuthzConstants.ATTR_AUTHZ_POLICY_RESULT);
    Assert.assertNotNull(decision);//  ww w.  j ava  2 s.  com
    Assert.assertEquals(expectedDecision, Enum.valueOf(AuthzDecision.class, decision));
}

From source file:org.rhq.plugins.jbossas.JBossASServerOperationsDelegate.java

/**
 * Shuts down the server by dispatching to shutdown via script or JMX. Waits until the server is down.
 * @return The result of the shutdown operation - is successful
 *//*from   w  w  w.  j a v  a  2 s .c  om*/
private String shutdown() {
    JBossASServerShutdownMethod shutdownMethod = Enum.valueOf(JBossASServerShutdownMethod.class,
            this.serverComponent.getPluginConfiguration()
                    .getSimple(JBossASServerComponent.SHUTDOWN_METHOD_CONFIG_PROP).getStringValue());
    String result = JBossASServerShutdownMethod.JMX.equals(shutdownMethod) ? shutdownViaJmx()
            : shutdownViaScript();
    AvailabilityType avail = waitForServerToShutdown();
    if (avail == AvailabilityType.UP) {
        throw new RuntimeException("Server failed to shutdown");
    } else {
        return result;
    }
}

From source file:com.haulmont.restapi.service.filter.RestFilterParser.java

protected Object parseValue(MetaProperty metaProperty, String stringValue) throws RestFilterParseException {
    if (metaProperty.getRange().isDatatype()) {
        try {/*w ww  . j a v  a 2 s.  com*/
            return metaProperty.getRange().asDatatype().parse(stringValue);
        } catch (ParseException e) {
            throw new RestFilterParseException("Cannot parse property value: " + stringValue, e);
        }
    } else if (metaProperty.getRange().isEnum()) {
        try {
            return Enum.valueOf((Class<Enum>) metaProperty.getJavaType(), stringValue);
        } catch (IllegalArgumentException e) {
            throw new RestFilterParseException("Cannot parse enum value: " + stringValue, e);
        }
    }
    throw new RestFilterParseException("Cannot parse the condition value: " + stringValue);
}

From source file:com.amazon.kinesis.streaming.agent.config.Configuration.java

/**
 *
 * @param enumType//from w ww.j  a v  a 2s.  c o  m
 * @param key
 * @return the enum constant at given key.
 * @throws ConfigurationException
 *             if the enum constant failed to be returned.
 */
public <E extends Enum<E>> E readEnum(Class<E> enumType, String key) {
    String stringVal = readString(key, null);
    if (stringVal != null) {
        try {
            return Enum.valueOf(enumType, stringVal);
        } catch (Exception e) {
            throw new ConfigurationException(
                    String.format("Value(%s) is not legally accepted by key: %s. " + "Legal values are %s",
                            stringVal, key, Joiner.on(",").join(enumType.getEnumConstants())),
                    e);
        }
    } else
        throw new ConfigurationException("Required configuration value missing: " + key);
}

From source file:com.intuit.wasabi.api.pagination.filters.PaginationFilter.java

/**
 * Parses through the current filter to find {@code key=value} patterns and tests the corresponding
 * object properties accordingly./*from   ww  w.j  ava  2  s .  com*/
 * <p>
 * Returns true unless an object does not pass a filter.
 *
 * @param object   the object to test
 * @param enumType the allowed properties
 * @param <V>      the property enum type
 * @return the test result
 */
/*test*/
final <V extends Enum<V> & PaginationFilterProperty> boolean testFields(T object, Class<V> enumType) {
    try (Scanner filterScanner = new Scanner(getKeyValuePartOfFilter(filter))) {
        filterScanner.useDelimiter(DELIMITER);

        // iterate over all single field patterns
        String pattern = "[a-zA-Z_]+" + SEPARATOR + ".*";
        while (filterScanner.hasNext(pattern)) {
            String[] keyValuePattern = filterScanner.next(pattern).split(SEPARATOR);

            V key;
            try {
                key = Enum.valueOf(enumType, keyValuePattern[0]);
            } catch (IllegalArgumentException illegalArgumentException) {
                throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE,
                        "The request can not be filtered by key '" + keyValuePattern[0] + "'.",
                        illegalArgumentException);
            }

            String filterValue = modifyFilterForKey(key, keyValuePattern[1]);

            if (!filterByProperty(object, filterValue, key.getPropertyExtractor(), key.getFilterPredicate())) {
                return false;
            }
        }
    }
    return true;
}

From source file:de.highbyte_le.weberknecht.request.RequestWrapper.java

public <T extends Enum<T>> T getParameterAsEnum(String name, Class<T> enumType, T defaultValue) {
    T retVal = defaultValue;//  w  ww . jav  a 2s.  c  om
    String val = getParameter(name);
    try {
        if (val != null) {
            retVal = Enum.valueOf(enumType, val);
        }
    } catch (IllegalArgumentException e) {
        logger.debug("getParameterAsEnum() - " + "invalid enum value '" + val + "' for parameter " + name
                + " and type " + enumType.getCanonicalName());
    }

    return retVal;
}