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.solenopsis.checkstyle.checks.AbstractOptionCheck.java

/**
 * Set the option to enforce./*from w ww.  j  a  va  2s  . c om*/
 * @param optionStr string to decode option from
 * @throws ConversionException if unable to decode
 */
public void setOption(String optionStr) {
    try {
        abstractOption = Enum.valueOf(optionClass, optionStr.trim().toUpperCase(Locale.ENGLISH));
    } catch (IllegalArgumentException iae) {
        throw new ConversionException("unable to parse " + optionStr, iae);
    }
}

From source file:common.util.PropertyFilter.java

/**
 * @param filterName/*from w  w  w  .j  ava2 s. c  o m*/
 * @param value
 */
public PropertyFilter(final String filterName, final String value) {

    String matchTypeStr;
    String matchPattenCode = LikeMatchPatten.ALL.toString();
    String matchTypeCode;
    String propertyTypeCode;

    if (filterName.contains("LIKE") && filterName.charAt(0) != 'L') {
        matchTypeStr = StringUtils.substringBefore(filterName, PARAM_PREFIX);
        matchPattenCode = StringUtils.substring(matchTypeStr, 0, 1);
        matchTypeCode = StringUtils.substring(matchTypeStr, 1, matchTypeStr.length() - 1);
        propertyTypeCode = StringUtils.substring(matchTypeStr, matchTypeStr.length() - 1,
                matchTypeStr.length());
    } else {
        matchTypeStr = StringUtils.substringBefore(filterName, PARAM_PREFIX);
        matchTypeCode = StringUtils.substring(matchTypeStr, 0, matchTypeStr.length() - 1);
        propertyTypeCode = StringUtils.substring(matchTypeStr, matchTypeStr.length() - 1,
                matchTypeStr.length());
    }

    try {
        matchType = Enum.valueOf(MatchType.class, matchTypeCode);
        likeMatchPatten = Enum.valueOf(LikeMatchPatten.class, matchPattenCode);
    } catch (RuntimeException e) {
        throw new IllegalArgumentException("filter name: " + filterName
                + "Not prepared in accordance with rules, not get more types of property.", e);
    }

    try {
        propertyType = Enum.valueOf(PropertyType.class, propertyTypeCode).getValue();
    } catch (RuntimeException e) {
        throw new IllegalArgumentException("filter name: " + filterName
                + "Not prepared in accordance with the rules, attribute value types can not be.", e);
    }

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

    Validate.isTrue(propertyNames.length > 0, "filter name: " + filterName
            + "Not prepared in accordance with the rules, property names can not be.");

    this.propertyValue = ConvertUtils.convert(value, propertyType);
}

From source file:org.lunarray.model.descriptor.converter.def.DelegatingEnumConverterTool.java

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
// We're checking after the fact.
@Override/* ww w  . j  a  v a 2  s  . c o m*/
public <T> T convertToInstance(final Class<T> type, final String stringValue) throws ConverterException {
    Validate.notNull(type, DelegatingEnumConverterTool.TYPE_NULL);
    T result = null;
    if (type.isEnum()) {
        DelegatingEnumConverterTool.LOGGER.debug("Converting to instance type {} with value: {}", type,
                stringValue);
        @SuppressWarnings("rawtypes")
        // No other way?
        final Class<? extends Enum> enumType = (Class<? extends Enum>) type;
        if (!StringUtil.isEmptyString(stringValue)) {
            result = (T) Enum.valueOf(enumType, stringValue);
        }
    } else {
        result = this.converterTool.convertToInstance(type, stringValue);
    }
    return result;
}

From source file:org.fornax.cartridges.sculptor.framework.propertyeditor.EnumEditor.java

/**
 * Parse the value from the given text is not supported by this editor
 *//*from w w w  .  ja va2  s  .c om*/
public void setAsText(String text) throws IllegalArgumentException {
    if (text == null || text.equals("")) {
        setValue(null);
        return;
    }
    Enum<?> value = Enum.valueOf(enumClass, text);
    setValue(value);
}

From source file:org.opentestsystem.delivery.testreg.domain.EnumBasedXStreamConverter.java

@Override
@SuppressWarnings("unchecked")
public String toString(final Object obj) {
    String name = (caseStyleSource == null) ? obj.toString()
            : (isFullyCapitalize ? caseStyleSource.changeCaseFully(obj.toString())
                    : caseStyleSource.changeCase(obj.toString()));

    if (caseStyleTarget.equals(CaseStyle.TITLECASE_HANDLE_NULL_EMPTY) && isNullOrEmpty(name)) {
        return "";
    }/*from   w  w w  .ja v  a 2 s .  c o m*/
    try {
        Enum enumVal = Enum.valueOf(enumType, name);
        return caseStyleTarget.changeCase(enumVal.toString());
    } catch (IllegalArgumentException iae) {
        return name;
    }
}

From source file:org.pushio.webapp.support.persistence.SearchFilter.java

/**
 * searchParamskey?LIKES_name/*from ww w .  j a v  a2s . c  o m*/
 */
public static Map<String, SearchFilter> parse(Map<String, Object> searchParams) {
    Map<String, SearchFilter> filters = Maps.newHashMap();
    String filedName;
    Operator operator;
    for (Entry<String, Object> entry : searchParams.entrySet()) {
        // 
        String key = entry.getKey();

        Object value = entry.getValue();
        if (key == null || value == null || StringUtils.isBlank(value.toString())) {
            continue;
        }
        // http:\\192.168.1.1:8080\bigdata\tt.do?db_sum_alias=carCount,busCount&db_query=a=
        // operatorfiledAttribute
        String[] names = StringUtils.split(key, "_");
        if (names.length != 2 && names.length != 3) {
            // throw new IllegalArgumentException(key + " is not a valid search filter name");
            logger.warn(key + " is not a valid search filter name");
            continue;
        }

        Class<?> propertyClass = null;
        if (names.length == 3) {
            try {
                propertyClass = Enum.valueOf(PropertyType.class, names[2]).getValue();
                logger.debug(key + ":" + propertyClass.getName());
                if (propertyClass != null) {
                    if (propertyClass.getName().equals("java.util.Date")) {
                        String str = value.toString();
                        if (str.length() == 10) {
                            if (names[0].equals("GT") || names[0].equals("GTE")) {
                                str += " 00:00:00";
                            } else if (names[0].equals("LT") || names[0].equals("LTE")) {
                                str += " 23:59:59";
                            }
                        }
                        value = dateTimeFormat.parseObject(str);
                    } else {
                        value = ConvertUtils.convert(value);
                    }
                }

            } catch (RuntimeException e) {
                logger.warn(key + " PropertyType is not a valid type!", e);
            } catch (ParseException e) {
                logger.warn(key + " PropertyType is not a valid type!", e);
            }
        }

        filedName = names[1];
        operator = Operator.valueOf(names[0]);
        // searchFilter
        SearchFilter filter = new SearchFilter(filedName, operator, value);
        filters.put(key, filter);
    }

    return filters;
}

From source file:com.anrisoftware.globalpom.format.enums.EnumFormat.java

/**
 * @see #parseObject(String, ParsePosition)
 *//*from   ww  w  . j  a v  a 2 s  .  co  m*/
public EnumType parse(String source, ParsePosition pos) {
    try {
        source = source.substring(pos.getIndex()).toUpperCase();
        @SuppressWarnings("unchecked")
        EnumType item = (EnumType) Enum.valueOf(enumType, source);
        pos.setErrorIndex(-1);
        pos.setIndex(source.length());
        return item;
    } catch (IllegalArgumentException e) {
        pos.setIndex(0);
        pos.setErrorIndex(0);
        return null;
    }
}

From source file:kieker.common.logging.LogFactory.java

/**
 * This method tries to detect the available loggers.
 * /* w  ww . ja  v a  2 s . c  o  m*/
 * @return The found logger.
 */
private static final Logger detectLogger() {
    if (null != JVM_LOGGER) {
        try {
            return Enum.valueOf(Logger.class, JVM_LOGGER);
        } catch (final IllegalArgumentException ex) { // NOPMD NOCS
            // Notify is handled above.
        }
    }
    try {
        if (Class.forName("org.slf4j.impl.StaticLoggerBinder") != null) {
            return Logger.SLF4J; // use SLF4J logging
        }
    } catch (final Exception ex) { // NOPMD NOCS (catch Exception)
        // use default in case of errors ...
    }
    try {
        if (Class.forName("org.apache.commons.logging.Log") != null) {
            return Logger.COMMONS; // use commons logging
        }
    } catch (final Exception ex) { // NOPMD NOCS (catch Exception)
        // use default in case of errors ...
    }
    return Logger.JDK;
}

From source file:com.sunlights.common.utils.PropertyFilter.java

/**
 * @param filterName/*from  ww  w. j av a2 s  . c om*/
 * @param value
 */
public PropertyFilter(final String filterName, final String value) {

    String matchTypeStr;
    String matchPattenCode = LikeMatchPatten.ALL.toString();
    String matchTypeCode;
    String propertyTypeCode;

    if (filterName.contains("LIKE") && filterName.charAt(0) != 'L') {
        matchTypeStr = StringUtils.substringBefore(filterName, PARAM_PREFIX);
        matchPattenCode = StringUtils.substring(matchTypeStr, 0, 1);
        matchTypeCode = StringUtils.substring(matchTypeStr, 1, matchTypeStr.length() - 1);
        propertyTypeCode = StringUtils.substring(matchTypeStr, matchTypeStr.length() - 1,
                matchTypeStr.length());
    } else {
        matchTypeStr = StringUtils.substringBefore(filterName, PARAM_PREFIX);
        matchTypeCode = StringUtils.substring(matchTypeStr, 0, matchTypeStr.length() - 1);
        propertyTypeCode = StringUtils.substring(matchTypeStr, matchTypeStr.length() - 1,
                matchTypeStr.length());
    }

    try {
        matchType = Enum.valueOf(MatchType.class, matchTypeCode);
        likeMatchPatten = Enum.valueOf(LikeMatchPatten.class, matchPattenCode);
    } catch (RuntimeException e) {
        throw new IllegalArgumentException("filter name: " + filterName
                + "Not prepared in accordance with rules, not get more types of property.", e);
    }

    try {
        propertyType = Enum.valueOf(PropertyType.class, propertyTypeCode).getValue();
    } catch (RuntimeException e) {
        throw new IllegalArgumentException("filter name: " + filterName
                + "Not prepared in accordance with the rules, attribute value types can not be.", e);
    }

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

    Validate.isTrue(propertyNames.length > 0, "filter name: " + filterName
            + "Not prepared in accordance with the rules, property names can not be.");
    this.propertyValue = ConvertUtils.convert(value, propertyType);

}

From source file:org.richfaces.tests.metamer.ftest.extension.attributes.coverage.result.SimpleCoverageResult.java

private EnumSet getDefaultIgnoredValues(Class<? extends Enum> enumClass) {
    EnumSet result = EnumSet.noneOf(enumClass);
    for (String ignoredAtt : IGNORED_ATTS) {
        try {/*  www  . j a v  a 2 s.c o m*/
            result.add(Enum.valueOf(enumClass, ignoredAtt));
        } catch (IllegalArgumentException e) {
        }
    }
    return result;
}