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:org.rhq.core.domain.server.PersistenceUtility.java

@SuppressWarnings("unchecked")
// used in hibernate.jsp
public static Object cast(String value, Type hibernateType) {
    if (hibernateType instanceof PrimitiveType) {
        Class<?> type = ((PrimitiveType) hibernateType).getPrimitiveClass();
        if (type.equals(Byte.TYPE)) {
            return Byte.valueOf(value);
        } else if (type.equals(Short.TYPE)) {
            return Short.valueOf(value);
        } else if (type.equals(Integer.TYPE)) {
            return Integer.valueOf(value);
        } else if (type.equals(Long.TYPE)) {
            return Long.valueOf(value);
        } else if (type.equals(Float.TYPE)) {
            return Float.valueOf(value);
        } else if (type.equals(Double.TYPE)) {
            return Double.valueOf(value);
        } else if (type.equals(Boolean.TYPE)) {
            return Boolean.valueOf(value);
        }//from   w  w w.j  a  v a 2s  . c  om
    } else if (hibernateType instanceof EntityType) {
        String entityName = ((EntityType) hibernateType).getAssociatedEntityName();
        try {
            Class<?> entityClass = Class.forName(entityName);
            Object entity = entityClass.newInstance();

            Field primaryKeyField = entityClass.getDeclaredField("id");
            primaryKeyField.setAccessible(true);
            primaryKeyField.setInt(entity, Integer.valueOf(value));
            return entity;
        } catch (Throwable t) {
            throw new IllegalArgumentException("Type[" + entityName + "] must have PK field named 'id'");
        }
    } else if (hibernateType instanceof CustomType) {
        if (Enum.class.isAssignableFrom(hibernateType.getReturnedClass())) {
            Class<? extends Enum<?>> enumClass = hibernateType.getReturnedClass();
            Enum<?>[] enumValues = enumClass.getEnumConstants();
            try {
                int enumOrdinal = Integer.valueOf(value);
                try {
                    return enumValues[enumOrdinal];
                } catch (ArrayIndexOutOfBoundsException aioobe) {
                    throw new IllegalArgumentException("There is no " + enumClass.getSimpleName()
                            + " enum with ordinal '" + enumOrdinal + "'");
                }
            } catch (NumberFormatException nfe) {
                String ucaseValue = value.toUpperCase();
                for (Enum<?> nextEnum : enumValues) {
                    if (nextEnum.name().toUpperCase().equals(ucaseValue)) {
                        return nextEnum;
                    }
                }
                throw new IllegalArgumentException(
                        "There is no " + enumClass.getSimpleName() + " enum with name '" + value + "'");
            }
        }
    }
    return value;
}

From source file:net.sf.eclipsecs.core.config.meta.MetadataFactory.java

@SuppressWarnings("unchecked")
private static void processProperties(Element moduleElement, RuleMetadata moduleMetadata,
        ResourceBundle metadataBundle) throws CheckstylePluginException {

    List<Element> propertyElements = moduleElement.elements(XMLTags.PROPERTY_METADATA_TAG);
    for (Element propertyEl : propertyElements) {

        ConfigPropertyType type = ConfigPropertyType.valueOf(propertyEl.attributeValue(XMLTags.DATATYPE_TAG));

        String name = propertyEl.attributeValue(XMLTags.NAME_TAG).trim();
        String defaultValue = StringUtils.trim(propertyEl.attributeValue(XMLTags.DEFAULT_VALUE_TAG));
        String overrideDefaultValue = StringUtils
                .trim(propertyEl.attributeValue(XMLTags.DEFAULT_VALUE_OVERRIDE_TAG));

        ConfigPropertyMetadata property = new ConfigPropertyMetadata(type, name, defaultValue,
                overrideDefaultValue);/*from w w  w  .j  a  va 2 s  . com*/

        moduleMetadata.getPropertyMetadata().add(property);

        // get description
        String description = propertyEl.elementTextTrim(XMLTags.DESCRIPTION_TAG);
        description = localize(description, metadataBundle);
        property.setDescription(description);

        // get property enumeration values
        Element enumEl = propertyEl.element(XMLTags.ENUMERATION_TAG);
        if (enumEl != null) {
            String optionProvider = enumEl.attributeValue(XMLTags.OPTION_PROVIDER);
            if (optionProvider != null) {

                try {
                    Class<?> providerClass = CheckstylePlugin.getDefault().getAddonExtensionClassLoader()
                            .loadClass(optionProvider);

                    if (IOptionProvider.class.isAssignableFrom(providerClass)) {

                        IOptionProvider provider = (IOptionProvider) providerClass.newInstance();
                        property.getPropertyEnumeration().addAll(provider.getOptions());
                    } else if (Enum.class.isAssignableFrom(providerClass)) {

                        EnumSet<?> values = EnumSet.allOf((Class<Enum>) providerClass);
                        for (Enum e : values) {
                            property.getPropertyEnumeration().add(e.name().toLowerCase());
                        }
                    }
                } catch (ClassNotFoundException e) {
                    CheckstylePluginException.rethrow(e);
                } catch (InstantiationException e) {
                    CheckstylePluginException.rethrow(e);
                } catch (IllegalAccessException e) {
                    CheckstylePluginException.rethrow(e);
                }
            }

            // get explicit enumeration option values
            for (Element optionEl : (List<Element>) enumEl.elements(XMLTags.PROPERTY_VALUE_OPTIONS_TAG)) {
                property.getPropertyEnumeration().add(optionEl.attributeValue(XMLTags.VALUE_TAG));
            }
        }
    }
}

From source file:us.ihmc.idl.serializers.extra.JacksonInterchangeSerializer.java

private static Enum<?> str2enum(Enum<?>[] enumConstants, String val) {
    for (Enum<?> enumVal : enumConstants) {
        if (enumVal.name().equals(val)) {
            return enumVal;
        }/*from   ww w .  j  a va  2s.c o  m*/
    }
    return null;
}

From source file:gov.nih.nci.caarray.util.CaArrayUtils.java

/**
 * Return the constant names of the given enum instances.
 * /*from  w w w .  j a v  a 2 s .  c om*/
 * @param <E> the type of the enum instances
 * @param enums the enum instances whose constant names to return
 * @return the names, e.g. the set of enum.name() for each enum in enums
 */
public static <E extends Enum<E>> Set<String> namesForEnums(Iterable<E> enums) {
    final Set<String> names = new HashSet<String>();
    for (final Enum<?> oneEnum : enums) {
        names.add(oneEnum.name());
    }
    return names;
}

From source file:com.vmware.thinapp.common.util.AfUtil.java

/**
 * Convert enumeration values into string values.
 *
 * @param values//  www . j a va 2s  .  c  om
 * @return
 */
public static String[] toNames(Enum<?>... values) {
    List<String> names = new ArrayList<String>();

    for (Enum<?> e : values) {
        names.add(e.name());
    }

    return names.toArray(new String[names.size()]);
}

From source file:net.sf.jsignpdf.SignerOptionsFromCmdLine.java

/**
 * Return comma separated names from enum values array.
 * /*  ww  w .j  a va 2 s  . c  om*/
 * @param aEnumVals
 * @return
 */
private static String getEnumValues(Enum<?>[] aEnumVals) {
    final StringBuilder tmpResult = new StringBuilder();
    boolean tmpFirst = true;
    for (Enum<?> tmpEnu : aEnumVals) {
        if (tmpFirst) {
            tmpFirst = false;
        } else {
            tmpResult.append(", ");
        }
        tmpResult.append(tmpEnu.name());
    }
    return tmpResult.toString();
}

From source file:org.opendaylight.didm.tools.utils.StringUtils.java

/** A convenience overloaded method for {@link #toCamelCase(String)}
 * which takes enumeration constants as the parameter.
 * @param e the enum constant//w w w .  j a va2 s .  c om
 * @return the name of the enum constant, camel-cased
 */
public static String toCamelCase(Enum<?> e) {
    return toCamelCase(e.name());
}

From source file:gdv.xport.feld.Feld.java

/**
 * Ermittelt die FeldInfo aus dem uebergebenen Enum.
 *
 * @param feldX the feld x//from w  w w  .  j  a  v a  2s .  co  m
 * @return the feld info
 */
protected static FeldInfo getFeldInfo(final Enum<?> feldX) {
    try {
        Field field = feldX.getClass().getField(feldX.name());
        return field.getAnnotation(FeldInfo.class);
    } catch (NoSuchFieldException nsfe) {
        throw new InternalError("no field " + feldX + " (" + nsfe + ")");
    }
}

From source file:gdv.xport.feld.Feld.java

private static Object getAsObject(final Enum<?> feldX) {
    try {/*from w  w  w .  j  a va 2  s  .  co  m*/
        Field field = Bezeichner.class.getField(feldX.name());
        return field.get(null);
    } catch (NoSuchFieldException ex) {
        LOG.info("Bezeichner." + feldX.name() + " not found:", ex);
    } catch (IllegalArgumentException ex) {
        LOG.warn(ex);
    } catch (IllegalAccessException ex) {
        LOG.warn("can't access Bezeichner." + feldX.name(), ex);
    }
    return toBezeichnung(feldX);
}

From source file:org.opendaylight.didm.tools.utils.StringUtils.java

/**
 * A convenience overloaded method for//from w w w .  j  a  v  a 2s  . c  o  m
 * {@link #toCamelCase(String, String)}, which uses {@code e.name()}.
 *
 * @param prefix optional prefix
 * @param e the enum constant
 * @return the name of the enum constant, camel-cased
 */
public static String toCamelCase(String prefix, Enum<?> e) {
    return toCamelCase(prefix, e.name());
}