Example usage for java.lang Enum name

List of usage examples for java.lang Enum name

Introduction

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

Prototype

String name

To view the source code for java.lang Enum name.

Click Source Link

Document

The name of this enum constant, as declared in the enum declaration.

Usage

From source file:wherehows.util.UrnUtil.java

/**
 * Get the enum name or return default value if enum is null
 * @param e Enum//w w  w  .  j av a  2s. c o  m
 * @param defaultValue String
 * @return String
 */
public static String enumNameOrDefault(@Nullable Enum e, @Nullable String defaultValue) {
    return e == null ? defaultValue : e.name();
}

From source file:uk.org.lidalia.sysoutslf4j.integration.CrossClassLoaderTestUtils.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private static Object getLocalEnumInstance(Enum<?> enumInstance, Class destinationClass) {
    try {/*from  w w w.j  a v  a 2 s  .  c om*/
        return Enum.valueOf(destinationClass, enumInstance.name());
    } catch (Exception e) {
        Exceptions.throwUnchecked(e);
        throw new AssertionError("Unreachable");
    }
}

From source file:org.carrot2.util.simplexml.SimpleXmlWrapperValue.java

/**
 * Wraps the provided value with the serialization wrapper.
 */// w w  w  .j  a va2s .c o  m
static SimpleXmlWrapperValue wrap(Object value) {
    final SimpleXmlWrapperValue wrapper = new SimpleXmlWrapperValue();

    if (value == null) {
        return wrapper;
    }

    final Class<?> valueType = value.getClass();

    if (TO_STRING_VALUE_OF_TYPES.contains(valueType)) {
        wrapper.value = value.toString();
        wrapper.type = valueType.getName();
    } else if (value instanceof Character) {
        wrapper.value = value.toString();
        wrapper.type = Character.class.getName();
    } else if (value instanceof String) {
        wrapper.value = (String) value;
        wrapper.type = null;
    } else if (Class.class.isInstance(value)) {
        wrapper.value = ((Class<?>) value).getName();
        wrapper.type = Class.class.getName();
    } else if (value instanceof Enum<?>) {
        final Enum<?> e = (Enum<?>) value;
        wrapper.value = e.name();
        wrapper.type = e.getDeclaringClass().getName();
    } else if (value.getClass().getAnnotation(Root.class) != null) {
        wrapper.wrapper = value;
    } else {
        // Try to get a wrapper.
        wrapper.wrapper = SimpleXmlWrapperValue.wrapCustom(value);
    }

    return wrapper;
}

From source file:org.ardverk.gibson.EventUtils.java

private static void append(MessageDigest md, Enum<?> value) {
    append(md, value.getClass());
    append(md, value.name());
}

From source file:sk.lazyman.gizmo.web.PageTemplate.java

public static StringResourceModel createStringResourceStatic(Component component, Enum e) {
    String resourceKey = e.getDeclaringClass().getSimpleName() + "." + e.name();
    return createStringResourceStatic(component, resourceKey);
}

From source file:com.flexive.faces.converter.EnumConverter.java

/**
 * Encode an Enum value to a string that can be decoded using {@link EnumConverter#getValue(String)}.
 * <p>This method is exposed in JSF-EL as <code>fx:encodeEnum</code>.</p>
 *
 * @param value the Enum value to be encoded
 * @return  the encoded string representation
 *//*  ww  w.j  a va 2 s.com*/
public static String encodeEnum(Enum value) {
    // replace inner class suffixes of class name and append enum name
    return value.getClass().getName().replaceFirst("\\$\\d+$", "") + "::" + value.name();
}

From source file:org.lman.json.JsonConverter.java

private static Enum<?> readEnum(Object json, Class<?> clazz) throws ReadJsonException {
    if (!(json instanceof String))
        throw new ReadJsonException(json + " not a String, cannot Enumify");
    for (Object asObject : clazz.getEnumConstants()) {
        Enum<?> asEnum = (Enum<?>) asObject;
        if (asEnum.name().equalsIgnoreCase((String) json))
            return asEnum;
    }//from   w w w .ja  va 2 s . c  om
    throw new ReadJsonException(clazz + " has no matching enum for " + json);
}

From source file:org.dozer.util.ProtoUtils.java

public static Object unwrapEnums(Object value) {
    if (value instanceof Descriptors.EnumValueDescriptor) {
        Descriptors.EnumValueDescriptor descriptor = (Descriptors.EnumValueDescriptor) value;
        Class<? extends Enum> enumClass = getEnumClassByEnumDescriptor(descriptor.getType());
        Enum[] enumValues = enumClass.getEnumConstants();
        for (Enum enumValue : enumValues) {
            if (((Descriptors.EnumValueDescriptor) value).getName().equals(enumValue.name()))
                return enumValue;
        }/*from w  ww.ja v  a  2s.com*/
        return null;
    }
    if (value instanceof Collection) {
        List modifiedList = new ArrayList(((List) value).size());
        for (Object element : (List) value) {
            modifiedList.add(unwrapEnums(element));
        }
        return modifiedList;
    }
    return value;
}

From source file:de.iteratec.iteraplan.businesslogic.exchange.common.vbb.impl.util.VisualVariableHelper.java

/**
 * Sets the value of a given visual variable to a beanMap. If no such variable exists or the variable is null, then no value is set.
 * @param target the beanMap to which the variable should be set.
 * @param source the instance holding the values of the visual variables.
 * @param vv the visual variable to set to the beanMap.
 *///from w  ww  .j  a  v  a2s.c o  m
@SuppressWarnings("unchecked")
public static void setVisualVariableValue(BeanMap target, EObject source, EAttribute vv) {
    if (source.eIsSet(vv) && source.eGet(vv) != null && target.containsKey(vv.getName())) {
        Object value = source.eGet(vv);
        //TODO handle multivalued attributes here
        if (vv.getEAttributeType() instanceof EEnum) {
            Class<Enum<?>> enumType = (Class<Enum<?>>) ((EEnum) vv.getEAttributeType()).getInstanceClass();
            for (Enum<?> ec : enumType.getEnumConstants()) {
                if (ec.name().equals(((EEnumLiteral) value).getName())) {
                    value = ec;
                }
            }
        }
        target.put(vv.getName(), value);
    }
}

From source file:org.hdiv.web.servlet.tags.form.SelectedValueComparatorHDIV.java

private static boolean exhaustiveCompare(Object boundValue, Object candidate, PropertyEditor editor,
        Map<PropertyEditor, Object> convertedValueCache) {

    String candidateDisplayString = ValueFormatterHDIV.getDisplayString(candidate, editor, false);
    if (boundValue instanceof LabeledEnum) {
        LabeledEnum labeledEnum = (LabeledEnum) boundValue;
        String enumCodeAsString = ObjectUtils.getDisplayString(labeledEnum.getCode());
        if (enumCodeAsString.equals(candidateDisplayString)) {
            return true;
        }/*from   ww w. ja  v a 2s  .  c  o  m*/
        String enumLabelAsString = ObjectUtils.getDisplayString(labeledEnum.getLabel());
        if (enumLabelAsString.equals(candidateDisplayString)) {
            return true;
        }
    } else if (boundValue.getClass().isEnum()) {
        Enum boundEnum = (Enum) boundValue;
        String enumCodeAsString = ObjectUtils.getDisplayString(boundEnum.name());
        if (enumCodeAsString.equals(candidateDisplayString)) {
            return true;
        }
        String enumLabelAsString = ObjectUtils.getDisplayString(boundEnum.toString());
        if (enumLabelAsString.equals(candidateDisplayString)) {
            return true;
        }
    } else if (ObjectUtils.getDisplayString(boundValue).equals(candidateDisplayString)) {
        return true;
    } else if (editor != null && candidate instanceof String) {
        // Try PE-based comparison (PE should *not* be allowed to escape creating thread)
        String candidateAsString = (String) candidate;
        Object candidateAsValue;
        if (convertedValueCache != null && convertedValueCache.containsKey(editor)) {
            candidateAsValue = convertedValueCache.get(editor);
        } else {
            editor.setAsText(candidateAsString);
            candidateAsValue = editor.getValue();
            if (convertedValueCache != null) {
                convertedValueCache.put(editor, candidateAsValue);
            }
        }
        if (ObjectUtils.nullSafeEquals(boundValue, candidateAsValue)) {
            return true;
        }
    }
    return false;
}