Example usage for java.lang Class getEnumConstants

List of usage examples for java.lang Class getEnumConstants

Introduction

In this page you can find the example usage for java.lang Class getEnumConstants.

Prototype

public T[] getEnumConstants() 

Source Link

Document

Returns the elements of this enum class or null if this Class object does not represent an enum type.

Usage

From source file:com.maydesk.base.util.PDUtil.java

public static Object getSopletEntry(String sopletClassName, String sopletName) {
    try {/*ww  w  .  j  ava 2 s  .  c  om*/
        Class enumClass = Class.forName(sopletClassName);
        for (Object enumConstant : enumClass.getEnumConstants()) {
            if (((Enum) enumConstant).name().equals(sopletName)) {
                return enumConstant;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.github.dactiv.fear.commons.Apis.java

/**
 * {@link ValueEnum} ?? class ???/* w w w.j av  a2s.c  om*/
 *
 * @param enumClass  class
 * @param ignore    ?
 *
 * @return key value ? Map ?
 */
public static List<Map<String, Object>> getConfigList(Class<? extends Enum<? extends ValueEnum<?>>> enumClass,
        List ignore) {

    List<Map<String, Object>> result = new ArrayList<>();
    Enum<? extends ValueEnum<?>>[] values = enumClass.getEnumConstants();

    for (Enum<? extends ValueEnum<?>> o : values) {
        ValueEnum<?> ve = (ValueEnum<?>) o;
        Object value = ve.getValue();

        if (ignore != null && !ignore.contains(value)) {
            Map<String, Object> dictionary = new LinkedHashMap<>();

            dictionary.put(DEFAULT_VALUE_NAME, value);
            dictionary.put(DEFAULT_KEY_NAME, ve.getName());

            result.add(dictionary);
        }

    }

    return result;
}

From source file:org.apache.hadoop.mapred.LexicographicalComparerHolder.java

/**
 * Returns the Unsafe-using Comparer, or falls back to the pure-Java
 * implementation if unable to do so./* w  w w.  jav a2s  .co  m*/
 */
static Comparer<byte[]> getBestComparer() {
    try {
        Class<?> theClass = Class.forName(UNSAFE_COMPARER_NAME);

        // yes, UnsafeComparer does implement Comparer<byte[]>
        @SuppressWarnings("unchecked")
        Comparer<byte[]> comparer = (Comparer<byte[]>) theClass.getEnumConstants()[0];
        return comparer;
    } catch (Throwable t) { // ensure we really catch *everything*
        LOG.error("Loading lexicographicalComparerJavaImpl...");
        return lexicographicalComparerJavaImpl();
    }
}

From source file:com.github.dozermapper.protobuf.util.ProtoUtils.java

/**
 * Unwrap {@link Descriptors.EnumValueDescriptor} or a {@link Collection} to a raw {@link Enum}
 * If the value is neither {@link Descriptors.EnumValueDescriptor} or a {@link Collection}, the value is returned.
 *
 * @param value         {@link Descriptors.EnumValueDescriptor} or a {@link Collection}
 * @param beanContainer {@link BeanContainer} instance
 * @return {@link Enum} if value is {@link Descriptors.EnumValueDescriptor}, else a {@link Collection} of {@link Enum}
 *///from w  w  w  . ja va  2 s  .  c o m
@SuppressWarnings("unchecked")
public static Object unwrapEnums(Object value, BeanContainer beanContainer) {
    if (value instanceof Descriptors.EnumValueDescriptor) {
        Descriptors.EnumValueDescriptor descriptor = (Descriptors.EnumValueDescriptor) value;
        Class<? extends Enum> enumClass = getEnumClassByEnumDescriptor(descriptor.getType(), beanContainer);

        for (Enum enumValue : enumClass.getEnumConstants()) {
            if (((Descriptors.EnumValueDescriptor) value).getName().equals(enumValue.name())) {
                return enumValue;
            }
        }

        return null;
    }

    if (value instanceof Collection) {
        Collection valueCollection = (Collection) value;
        List modifiedList = new ArrayList(valueCollection.size());

        for (Object element : valueCollection) {
            modifiedList.add(unwrapEnums(element, beanContainer));
        }

        return modifiedList;
    }

    return value;
}

From source file:com.trenako.values.LocalizedEnum.java

/**
 * Builds the list with the provided {@code enum} values.
 *
 * @param enumType      the {@code enum} type
 * @param messageSource the {@code MessageSource} to localize the text strings
 * @param failback      the failback interface to produce default messages
 * @return the values list//from   w  w w.  j a v  a2s.  c  o  m
 */
public static <T extends Enum<T>, F extends MessageFailback<T>> Iterable<LocalizedEnum<T>> list(
        Class<T> enumType, MessageSource messageSource, F failback) {

    if (!enumType.isEnum()) {
        throw new IllegalArgumentException("The provided type is not an enum");
    }

    T[] consts = enumType.getEnumConstants();
    List<LocalizedEnum<T>> items = new ArrayList<LocalizedEnum<T>>(consts.length);

    for (T val : consts) {
        items.add(new LocalizedEnum<T>(val, messageSource, failback));
    }

    return Collections.unmodifiableList(items);
}

From source file:com.google.walkaround.util.server.flags.JsonFlags.java

private static <T extends Enum<T>> T parseEnumValue(Class<T> enumType, String key, String value)
        throws FlagFormatException {
    try {//from ww w  .j  a v a2 s.  c  o m
        return Enum.valueOf(enumType, value);
    } catch (IllegalArgumentException e) {
        throw new FlagFormatException("Invalid flag enum value " + value + " for key " + key
                + "; valid values: " + Arrays.toString(enumType.getEnumConstants()), e);
    }
}

From source file:org.jnetstream.protocol.ProtocolRegistry.java

/**
 * @param protocol//ww w .  j a  va 2s .c  om
 */
private static void fillInFromClassInfo(DefaultProtocolEntry entry, Protocol protocol) {
    Class<? extends Protocol> c = protocol.getClass();
    if (c.isEnum() == false) {
        return;
    }

    Enum<?> constant = null;
    for (Enum<?> e : (Enum[]) c.getEnumConstants()) {
        if (e == protocol) {
            constant = e;
        }
    }

    Package pkg = c.getPackage();
    String suite = c.getSimpleName();
    String name = constant.name();
    String headeri = pkg.getName() + "." + name;
    String headerc = pkg.getName() + "." + name + "Header";
    String headercdc = pkg.getName() + "." + name + "Codec";

    // System.out.printf("suite=%s,\n name=%s,\n headeri=%s,\n headerc=%s\n",
    // suite, name, headeri, headerc);

    entry.setSuite(suite);
    entry.setName(name);
    try {

        entry.setProtocolClass((Class<? extends Header>) Class.forName(headeri));
    } catch (Exception e) {
        logger.warn("missing header: " + headeri);
        logger.debug(e);
    }

    try {
        entry.setCodec((Class<HeaderCodec<? extends Header>>) Class.forName(headercdc));

        HeaderCodec<? extends Header> codec = entry.getCodecClass().newInstance();
        entry.setCodec(codec);

    } catch (Exception e) {
        logger.warn("missing  codec: " + headercdc);
        logger.debug(e);
    }
}

From source file:cop.raml.mocks.MockUtils.java

private static TypeElementMock createEnumElement(@NotNull Class<?> cls) {
    TypeElementMock element = new TypeElementMock(cls.getName(), ElementKind.ENUM);
    element.setType(new TypeMirrorMock(element, TypeKind.DECLARED));
    element.addEnclosedElement(new TypeElementMock("values()", ElementKind.METHOD));

    for (Object obj : cls.getEnumConstants())
        element.addEnclosedElement(new VariableElementMock(obj.toString(), ElementKind.ENUM_CONSTANT));

    return setAnnotations(element, cls);
}

From source file:ypcnv.helpers.EnumHelper.java

/**
 * A method to get a list of a string representations with object's possible
 * values.//from w  w w.j a v  a 2 s.com
 */
public static <T extends Enum<T>> List<String> toStringList(Class<T> target) {
    String wantedMethodName = "getDisplayValue";
    List<String> list = new LinkedList<String>();
    Method wantedMethodValue = null;

    try {
        wantedMethodValue = target.getMethod(wantedMethodName);
    } catch (NoSuchMethodException e) {
        LOG.error("method '" + wantedMethodName + "' is not implemented in '" + target + "'.");
        e.printStackTrace();
    }

    for (Object obj : target.getEnumConstants()) {
        if (wantedMethodValue == null) {
            list.add(obj.toString());
        } else {
            try {
                list.add((String) wantedMethodValue.invoke(obj));
            } catch (Exception e) {
                LOG.error("Failed to create list of possible values.");
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }

    return list;
}

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

private static EClassifier getEEnum(EPackage vvEPackage, Class<? extends Enum<?>> enumClass) {
    if (vvEPackage.getEClassifier(enumClass.getSimpleName()) == null) {
        EEnum result = EcoreFactory.eINSTANCE.createEEnum();
        result.setName(enumClass.getSimpleName());
        result.setInstanceClass(enumClass);
        for (Enum<?> enumValue : enumClass.getEnumConstants()) {
            EEnumLiteral lit = EcoreFactory.eINSTANCE.createEEnumLiteral();
            lit.setValue(enumValue.ordinal());
            lit.setName(enumValue.name());
            result.getELiterals().add(lit);
        }//from   www  .  java 2  s  .c  o  m
        vvEPackage.getEClassifiers().add(result);
    }
    return vvEPackage.getEClassifier(enumClass.getSimpleName());
}