Example usage for java.lang Enum toString

List of usage examples for java.lang Enum toString

Introduction

In this page you can find the example usage for java.lang Enum toString.

Prototype

public String toString() 

Source Link

Document

Returns the name of this enum constant, as contained in the declaration.

Usage

From source file:org.idempiere.adinterface.ModelADServiceImpl.java

private Enum validateParameter(String parameterName, Enum action, Table table) {
    String string = null;//from   w ww . j a  va 2  s.  c o  m
    if (action == null)
        string = validateParameter(parameterName, string);
    else
        string = validateParameter(parameterName, action.toString());
    if (string == null)
        return (Enum) table.forInt(-1);
    if (action != null && string.equals(action.toString()))
        return action;
    return (Enum) table.forString(string);
}

From source file:ca.oson.json.Oson.java

private <E> Enum<?> json2Enum(FieldData objectDTO) {
    objectDTO.valueToProcess = StringUtil.unquote(objectDTO.valueToProcess, isEscapeHtml());
    Object valueToProcess = objectDTO.valueToProcess;
    Class<E> returnType = objectDTO.returnType;
    boolean required = objectDTO.required();
    Enum defaultValue = (Enum) objectDTO.defaultValue;
    boolean json2Java = objectDTO.json2Java;

    if (returnType == null || valueToProcess == null) {
        if (required) {
            return defaultValue;
        }/*from  w ww  . j  a v  a2  s. c om*/

        return null;
    }

    String value = (String) valueToProcess;

    Class<Enum> enumType = (Class<Enum>) returnType;

    try {
        Function function = objectDTO.getDeserializer();

        if (function != null) {

            Object returnedValue = null;

            if (function instanceof Json2DataMapperFunction) {
                DataMapper classData = new DataMapper(returnType, value, objectDTO.classMapper, objectDTO.level,
                        getPrettyIndentation());
                returnedValue = ((Json2DataMapperFunction) function).apply(classData);

            } else if (function instanceof Json2FieldDataFunction) {
                Json2FieldDataFunction f = (Json2FieldDataFunction) function;
                FieldData fieldData = objectDTO.clone();

                returnedValue = f.apply(fieldData);

            } else if (function instanceof Json2EnumFunction) {
                return (Enum<?>) ((Json2EnumFunction) function).apply(value);
            } else {
                returnedValue = function.apply(value);
            }

            if (returnedValue instanceof Optional) {
                returnedValue = ObjectUtil.unwrap(returnedValue);
            }

            if (returnedValue == null) {
                return null;
            }

            Class type = returnedValue.getClass();

            if (Enum.class.isAssignableFrom(type)) {
                return (Enum<?>) returnedValue;

            } else if (Number.class.isAssignableFrom(type)) {
                int ordinal = ((Number) returnedValue).intValue();

                for (Enum enumValue : enumType.getEnumConstants()) {
                    if (enumValue.ordinal() == ordinal) {
                        return enumValue;
                    }
                }

            } else {
                String name = returnedValue.toString();

                for (Enum enumValue : enumType.getEnumConstants()) {
                    if (enumValue.toString().equalsIgnoreCase(name)
                            || enumValue.name().equalsIgnoreCase(name)) {
                        return enumValue;
                    }
                }
            }

        }

    } catch (Exception ex) {
    }

    for (Method method : enumType.getDeclaredMethods()) {
        for (Annotation annotation : method.getDeclaredAnnotations()) {
            String aname = annotation.annotationType().getName();

            switch (aname) {
            case "com.fasterxml.jackson.annotation.JsonCreator":
            case "org.codehaus.jackson.annotate.JsonCreator":
                return ObjectUtil.getMethodValue(null, method, value);

            case "ca.oson.json.annotation.FieldMapper":
                ca.oson.json.annotation.FieldMapper fieldMapper = (ca.oson.json.annotation.FieldMapper) annotation;
                if (fieldMapper.jsonCreator() != null && fieldMapper.jsonCreator() == BOOLEAN.TRUE) {
                    return ObjectUtil.getMethodValue(null, method, value);
                }
            }
        }

    }

    String fieldName = null;
    for (Field field : enumType.getDeclaredFields()) {
        String name = null;
        ca.oson.json.annotation.FieldMapper fieldMapper = field
                .getAnnotation(ca.oson.json.annotation.FieldMapper.class);
        if (fieldMapper != null) {
            name = fieldMapper.name();

            if (value.equalsIgnoreCase(name)) {
                fieldName = field.getName();
                break;
            }

        } else {
            for (Annotation annotation : field.getAnnotations()) {
                name = ObjectUtil.getName(annotation);
                if (value.equalsIgnoreCase(name)) {
                    fieldName = field.getName();
                    break;
                }
            }
        }
    }
    if (fieldName != null) {
        try {
            return Enum.valueOf(enumType, fieldName.toUpperCase());
        } catch (IllegalArgumentException ex) {
        }
    }

    try {
        return Enum.valueOf(enumType, value.toUpperCase());
    } catch (IllegalArgumentException ex) {
    }

    for (Enum enumValue : enumType.getEnumConstants()) {
        if (enumValue.toString().equalsIgnoreCase(value) || enumValue.name().equalsIgnoreCase(value)) {
            return enumValue;
        }
    }

    FieldData fieldData = new FieldData(value, Integer.class, true);
    Integer ordinal = json2Integer(fieldData);

    if (ordinal != null) {
        for (Enum enumValue : enumType.getEnumConstants()) {
            if (enumValue.ordinal() == ordinal) {
                return enumValue;
            }
        }
    }

    return null;
}