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.ocpsoft.pretty.faces.config.convert.TrailingSlashConverter.java

@SuppressWarnings("rawtypes")
public Object convert(final Class type, final Object value) {
    if (value instanceof String) {
        return Enum.valueOf(TrailingSlash.class, ((String) value).toUpperCase());
    }//ww  w.  j a va2s  .com
    throw new ConversionException("Could not convert value: [" + value + "] to TrailingSlash type.");
}

From source file:Main.java

/**
 * convert value to given type./*from www  .  j a  v  a2s  .  c  o m*/
 * null safe.
 *
 * @param value value for convert
 * @param type  will converted type
 * @return value while converted
 */
public static Object convertCompatibleType(Object value, Class<?> type) {

    if (value == null || type == null || type.isAssignableFrom(value.getClass())) {
        return value;
    }
    if (value instanceof String) {
        String string = (String) value;
        if (char.class.equals(type) || Character.class.equals(type)) {
            if (string.length() != 1) {
                throw new IllegalArgumentException(String.format("CAN NOT convert String(%s) to char!"
                        + " when convert String to char, the String MUST only 1 char.", string));
            }
            return string.charAt(0);
        } else if (type.isEnum()) {
            return Enum.valueOf((Class<Enum>) type, string);
        } else if (type == BigInteger.class) {
            return new BigInteger(string);
        } else if (type == BigDecimal.class) {
            return new BigDecimal(string);
        } else if (type == Short.class || type == short.class) {
            return Short.valueOf(string);
        } else if (type == Integer.class || type == int.class) {
            return Integer.valueOf(string);
        } else if (type == Long.class || type == long.class) {
            return Long.valueOf(string);
        } else if (type == Double.class || type == double.class) {
            return Double.valueOf(string);
        } else if (type == Float.class || type == float.class) {
            return Float.valueOf(string);
        } else if (type == Byte.class || type == byte.class) {
            return Byte.valueOf(string);
        } else if (type == Boolean.class || type == boolean.class) {
            return Boolean.valueOf(string);
        } else if (type == Date.class) {
            try {
                return new SimpleDateFormat(DATE_FORMAT).parse((String) value);
            } catch (ParseException e) {
                throw new IllegalStateException("Failed to parse date " + value + " by format " + DATE_FORMAT
                        + ", cause: " + e.getMessage(), e);
            }
        } else if (type == Class.class) {
            return forName((String) value);
        }
    } else if (value instanceof Number) {
        Number number = (Number) value;
        if (type == byte.class || type == Byte.class) {
            return number.byteValue();
        } else if (type == short.class || type == Short.class) {
            return number.shortValue();
        } else if (type == int.class || type == Integer.class) {
            return number.intValue();
        } else if (type == long.class || type == Long.class) {
            return number.longValue();
        } else if (type == float.class || type == Float.class) {
            return number.floatValue();
        } else if (type == double.class || type == Double.class) {
            return number.doubleValue();
        } else if (type == BigInteger.class) {
            return BigInteger.valueOf(number.longValue());
        } else if (type == BigDecimal.class) {
            return BigDecimal.valueOf(number.doubleValue());
        } else if (type == Date.class) {
            return new Date(number.longValue());
        }
    } else if (value instanceof Collection) {
        Collection collection = (Collection) value;
        if (type.isArray()) {
            int length = collection.size();
            Object array = Array.newInstance(type.getComponentType(), length);
            int i = 0;
            for (Object item : collection) {
                Array.set(array, i++, item);
            }
            return array;
        } else if (!type.isInterface()) {
            try {
                Collection result = (Collection) type.newInstance();
                result.addAll(collection);
                return result;
            } catch (Throwable e) {
                e.printStackTrace();
            }
        } else if (type == List.class) {
            return new ArrayList<>(collection);
        } else if (type == Set.class) {
            return new HashSet<>(collection);
        }
    } else if (value.getClass().isArray() && Collection.class.isAssignableFrom(type)) {
        Collection collection;
        if (!type.isInterface()) {
            try {
                collection = (Collection) type.newInstance();
            } catch (Throwable e) {
                collection = new ArrayList<>();
            }
        } else if (type == Set.class) {
            collection = new HashSet<>();
        } else {
            collection = new ArrayList<>();
        }
        int length = Array.getLength(value);
        for (int i = 0; i < length; i++) {
            collection.add(Array.get(value, i));
        }
        return collection;
    }
    return value;
}

From source file:com.yahoo.elide.utils.coerce.converters.ToEnumConverter.java

/**
 * Convert string to enum.//  w  w  w  . j a  v  a 2s.c  o  m
 *
 * @param cls enum to convert to
 * @param value value to convert
 * @param <T> enum type
 * @return enum
 */
private static <T> T stringToEnum(Class<?> cls, String value) {
    return (T) Enum.valueOf((Class<Enum>) cls, value);
}

From source file:com.hp.autonomy.frontend.find.core.beanconfiguration.AbstractEnumCondition.java

private T getProperty(final ConditionContext context) {
    return Enum.valueOf(typeToken, context.getEnvironment().getProperty(systemProperty, defaultValue.name()));
}

From source file:com.ocpsoft.pretty.faces.config.convert.RedirectConverter.java

@SuppressWarnings("rawtypes")
public Object convert(final Class type, final Object value) {
    if (value instanceof String) {
        String item = (String) value;
        if ("301".equals(item)) {
            item = "PERMANENT";
        } else if ("302".equals(item)) {
            item = "TEMPORARY";
        }/* w ww  .  j a  va2 s  .  com*/
        return Enum.valueOf(Redirect.class, item.toUpperCase());
    }
    throw new ConversionException("Could not convert value: [" + value + "] to Redirect type.");
}

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

/**
 * //from  w w  w  .  j  a v a2s  . c o  m
 * @param filterName EQ_S_NAME
 * @param value
 */
public PropertyFilter(final String filterName, final Object value) {

    String matchTypeCode = StringUtils.substringBefore(filterName, "_");
    try {
        matchType = Enum.valueOf(MatchType.class, matchTypeCode.toUpperCase());
    } catch (RuntimeException e) {
        throw new IllegalArgumentException("filter??,.",
                e);
    }

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

    this.value = value;
}

From source file:com.senacor.wbs.web.core.EnumConverter.java

public Object convertToObject(String value, Locale locale) {
    String upper = value.toUpperCase(locale).trim();
    String enumName = StringUtils.replace(upper, " ", "_");
    try {//from   w  ww  .j a v  a 2s  . c o  m
        return Enum.valueOf(enumType, enumName);
    } catch (IllegalArgumentException ex) {
        throw newConversionException("Cannot parse '" + value + "' using enum type " + enumType.getName(),
                value, locale);
    }
}

From source file:com.astamuse.asta4d.data.convertor.String2Enum.java

@Override
public DataValueConvertor<String, Enum> convert(final Class<Enum> targetType) {
    return new DataValueConvertor<String, Enum>() {
        @SuppressWarnings("unchecked")
        @Override/*  w  ww. j  a v a  2 s.  c  o  m*/
        public Enum convert(String s) throws UnsupportedValueException {
            if (StringUtils.isEmpty(s)) {
                return null;
            }
            return Enum.valueOf(targetType, s);
        }
    };
}

From source file:ca.sqlpower.dao.session.EnumConverter.java

@SuppressWarnings("unchecked")
public T convertToComplexType(String convertFrom) throws ConversionException {
    return (T) Enum.valueOf(enumType, convertFrom);
}

From source file:com.cnd.greencube.web.base.EnumConverter.java

/**
 * ?/*from  w  w w  .  ja  va2 s .com*/
 * 
 * @param type
 *            
 * @param value
 *            
 * @return 
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected Object convertToType(Class type, Object value) {
    String stringValue = value.toString().trim();
    return Enum.valueOf(type, stringValue);
}