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:com.cndatacom.core.orm.PropertyFilter.java

/**
 * @param filterName ,???. //from  ww w  .j  av a  2s  .co m
 *                   eg. LIKES_NAME_OR_LOGIN_NAME
 * @param value .
 */
public PropertyFilter(final String filterName, final String value) {

    this.filterName = filterName;
    this.value = value;

    String matchTypeStr = StringUtils.substringBefore(filterName, "_");
    //,EQ
    String matchTypeCode = StringUtils.substring(matchTypeStr, 0, matchTypeStr.length() - 1);
    //?,S?I
    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 = StringUtils.split(propertyNameStr, PropertyFilter.OR_SEPARATOR);
    propertyNames = propertyNameStr.split(PropertyFilter.OR_SEPARATOR);

    Assert.isTrue(propertyNames.length > 0,
            "filter??" + filterName + ",??.");
    //entity property.
    if (propertyNameStr.indexOf(LEFT_JION) != -1) {
        isLeftJion = true;
    }
    //
    Object value_ = null;
    if (null == value || value.equals("")) {

        this.propertyValue = null;

    } else {
        this.propertyValue = ReflectionUtils.convertStringToObject(value, propertyType);
        value_ = propertyValue;
        //?1
        if (propertyType.equals(Date.class) && filterName.indexOf("LTD") > -1) {
            propertyValue = DateUtils.addDays((Date) propertyValue, 1);
        }
    }
    //request?
    String key = propertyNames[0].replace(".", "_").replace(":", "_");
    //      if(propertyType!=Date.class)
    ////      Struts2Utils.getRequest().setAttribute(key, propertyValue);
    ////      else{
    ////         if(Struts2Utils.getRequest().getAttribute(key)!=null){
    ////            String time_begin=Struts2Utils.getRequest().getAttribute(key)+"";
    ////            Struts2Utils.getRequest().setAttribute(key, time_begin+"="+com.cndatacom.common.utils.DateUtil.format((Date)value_,"yyyy-MM-dd"));         
    ////         }else{
    ////      Struts2Utils.getRequest().setAttribute(key, com.cndatacom.common.utils.DateUtil.format((Date)value_,"yyyy-MM-dd"));   
    ////         }
    //      
    //      }

}

From source file:org.ovirt.engine.api.common.util.EnumValidator.java

public static <E extends Enum<E>> E validateEnum(String reason, String detail, Class<E> clz, String name,
        boolean toUppercase) {
    try {//from   w  w w. j  a v a 2 s .  com
        return Enum.valueOf(clz, toUppercase ? name.toUpperCase() : name);
    } catch (IllegalArgumentException | NullPointerException e) {
        detail = detail + getPossibleValues(clz);
        throw new WebApplicationException(
                response(reason, MessageFormat.format(detail, name, clz.getSimpleName())));
    }
}

From source file:com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck.java

/**
 * Set the option to enforce./*from   w w w .  j a  v a  2s. c o m*/
 * @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 " + abstractOption, iae);
    }
}

From source file:org.apache.cassandra.config.ColumnDefinition.java

public static ColumnDefinition inflate(org.apache.cassandra.config.avro.ColumnDef cd) {
    byte[] name = new byte[cd.name.remaining()];
    cd.name.get(name, 0, name.length);//  w  ww  .j  a v  a2 s.  c  om
    IndexType index_type = cd.index_type == null ? null : Enum.valueOf(IndexType.class, cd.index_type.name());
    String index_name = cd.index_name == null ? null : cd.index_name.toString();
    try {
        return new ColumnDefinition(name, cd.validation_class.toString(), index_type, index_name);
    } catch (ConfigurationException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.ocs.dynamo.importer.impl.BaseImporter.java

@SuppressWarnings("unchecked")
private Object getFieldValue(PropertyDescriptor d, U unit, XlsField field) {
    Object obj = null;//from  w  w w  .jav  a 2 s .c o m
    if (String.class.equals(d.getPropertyType())) {
        String value = getStringValueWithDefault(unit, field);
        if (value != null) {
            value = value.trim();
        }
        obj = StringUtils.isEmpty(value) ? null : value;
    } else if (d.getPropertyType().isEnum()) {
        String value = getStringValueWithDefault(unit, field);
        if (value != null) {
            value = value.trim();
            try {
                obj = Enum.valueOf(d.getPropertyType().asSubclass(Enum.class), value.toUpperCase());
            } catch (IllegalArgumentException ex) {
                throw new OCSImportException(
                        "Value " + value + " cannot be translated to a valid enumeration value", ex);
            }
        }

    } else if (Number.class.isAssignableFrom(d.getPropertyType())) {
        // numeric field

        Double value = getNumericValueWithDefault(unit, field);
        if (value != null) {

            // if the field represents a percentage but it is
            // received as a
            // fraction, we multiply it by 100
            if (field.percentage()) {
                if (isPercentageCorrectionSupported()) {
                    value = PERCENTAGE_FACTOR * value;
                }
            }

            // illegal negative value
            if (field.cannotBeNegative() && value < 0.0) {
                throw new OCSImportException(
                        "Negative value " + value + " found for field '" + d.getName() + "'");
            }

            if (Integer.class.equals(d.getPropertyType())) {
                obj = new Integer(value.intValue());
            } else if (BigDecimal.class.equals(d.getPropertyType())) {
                obj = BigDecimal.valueOf(value.doubleValue());
            } else {
                // by default, use a double
                obj = value;
            }
        }
    } else if (Boolean.class.isAssignableFrom(d.getPropertyType())) {
        return getBooleanValueWithDefault(unit, field);
    }
    return obj;
}

From source file:technology.tikal.customers.dao.objectify.ContactDaoOfy.java

@Override
public List<ContactOfy> consultarTodos(CustomerOfy parent, ContactFilter filtro,
        PaginationDataDual<Long, String> pagination) {
    if (filtro.getIndex().compareTo("Role") == 0) {
        String indexOfy = "normalizedRole";
        return contactRolePaginator.consultarTodosTemplate(
                new PaginationContext<CustomerOfy>(indexOfy, filtro.getIndex(), filtro, pagination, parent));
    } else {/*from  w w w . ja  v  a 2s  . co  m*/
        NamePriorityFilterValues namePriority;
        try {
            namePriority = Enum.valueOf(NamePriorityFilterValues.class, filtro.getIndex());
        } catch (IllegalArgumentException ex) {
            throw new MessageSourceResolvableException(new DefaultMessageSourceResolvable(
                    new String[] { "IllegalIndex.ContactDaoOfy.consultarTodos" },
                    new String[] { filtro.getIndex() }, "Invalid Index"));
        }
        String indexOfy = "normalized" + filtro.getIndex();
        if (namePriority != NamePriorityFilterValues.Name) {
            indexOfy = "name." + indexOfy;
        }
        return contactNamePaginator.consultarTodosTemplate(
                new PaginationContext<CustomerOfy>(indexOfy, filtro.getIndex(), filtro, pagination, parent));
    }
}

From source file:com.esofthead.mycollab.schedule.email.format.I18nFieldFormat.java

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override/* w  w w .  jav a  2  s  . c  om*/
public String formatField(MailContext<?> context, String value) {
    try {
        Enum valueEnum = Enum.valueOf(enumKey, value.toString());
        return LocalizationHelper.getMessage(context.getLocale(), valueEnum);
    } catch (Exception e) {
        log.error("Can not generate of object field: " + fieldName + " and " + value, e);
        return new Span().write();
    }
}

From source file:de.erdesignerng.dialect.ModelItemProperties.java

public void initializeFrom(T aObject) {
    ModelProperties theProperties = aObject.getProperties();

    try {/*  ww  w.j  a  v a2s .c  o m*/
        for (PropertyDescriptor theDescriptor : PropertyUtils.getPropertyDescriptors(this)) {
            if (theDescriptor.getReadMethod() != null && theDescriptor.getWriteMethod() != null) {
                String theValue = theProperties.getProperty(theDescriptor.getName());
                if (!StringUtils.isEmpty(theValue)) {
                    Class theType = theDescriptor.getPropertyType();

                    if (theType.isEnum()) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(),
                                Enum.valueOf(theType, theValue));
                    }
                    if (String.class.equals(theType)) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(), theValue);
                    }
                    if (Long.class.equals(theType) || long.class.equals(theType)) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(), Long.parseLong(theValue));
                    }
                    if (Integer.class.equals(theType) || int.class.equals(theType)) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(), Integer.parseInt(theValue));
                    }
                    if (Boolean.class.equals(theType) || boolean.class.equals(theType)) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(),
                                Boolean.parseBoolean(theValue));
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

From source file:gov.nih.nci.caarray.plugins.illumina.AbstractCsvDesignHelper.java

boolean isHeaderLine(List<String> values) {
    if (values.size() < requiredColumns.size()) {
        return false;
    }//w w  w  .jav  a 2s. c  om
    Collection<T> headers = new ArrayList<T>(values.size());
    try {
        for (String v : values) {
            T h = Enum.valueOf(headerEnumClass, v.toUpperCase(Locale.getDefault()));
            headers.add(h);
        }
    } catch (IllegalArgumentException e) {
        return false;
    }

    return EnumSet.allOf(headerEnumClass).containsAll(headers);
}

From source file:com.netflix.simianarmy.aws.AbstractRecorder.java

/**
 * Value to enum. Converts a "name|type" string back to an enum.
 *
 * @param value/*from   ww w  .j a  va 2 s .c o m*/
 *            the value
 * @return the enum
 */
@SuppressWarnings("unchecked")
protected static Enum valueToEnum(String value) {
    // parts = [enum value, enum class type]
    String[] parts = value.split("\\|", 2);
    if (parts.length < 2) {
        throw new RuntimeException("value " + value + " does not appear to be an internal enum format");
    }

    Class enumClass;
    try {
        enumClass = Class.forName(parts[1]);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("class for enum value " + value + " not found");
    }
    if (enumClass.isEnum()) {
        final Class<? extends Enum> enumSubClass = enumClass.asSubclass(Enum.class);
        return Enum.valueOf(enumSubClass, parts[0]);
    }
    throw new RuntimeException("value " + value + " does not appear to be an enum type");
}