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:org.raml.parser.utils.ConvertUtils.java

/**
 * <p>canBeConverted.</p>//  ww w . j a  v a  2s  .c om
 *
 * @param value a {@link java.lang.String} object.
 * @param type a {@link java.lang.Class} object.
 * @return a boolean.
 */
public static boolean canBeConverted(String value, Class<?> type) {
    if (type.isEnum()) {
        Object[] enumConstants = type.getEnumConstants();
        for (Object enumConstant : enumConstants) {
            if (enumConstant.toString().equals(value.toUpperCase())) {
                return true;
            }
        }
        return false;
    }
    if (type.isInstance(value)) {
        return true;
    }
    try {
        type.getConstructor(String.class);
        return true;
    } catch (NoSuchMethodException e) {
        //ignore
    }
    try {
        Class<?> wrapperClass = ClassUtils.primitiveToWrapper(type);
        convertTo(value, wrapperClass);
        return true;
    } catch (ClassCastException e) {
        return false;
    } catch (ConversionException e) {
        return false;
    }
}

From source file:AIR.Common.Utilities.JavaPrimitiveUtils.java

public static <E extends java.lang.Enum<E>> boolean enumTryParse(Class<E> enumType, String value,
        boolean ignoreCase, _Ref<E> out) {
    E[] enumsConstants = enumType.getEnumConstants();
    for (E e : enumsConstants) {
        String n = e.name();//w  w w . jav  a2  s . co  m
        if (ignoreCase ? n.equalsIgnoreCase(value) : n.equals(value)) {
            out.set(e);
            return true;
        }
    }
    return false;
}

From source file:com.sqewd.open.dal.services.PropertySchema.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private static <T extends Enum> String[] getEnumValues(Class<?> type) throws Exception {
    T[] enums = (T[]) type.getEnumConstants();
    if (enums != null && enums.length > 0) {
        String[] values = new String[enums.length];
        for (int ii = 0; ii < enums.length; ii++) {
            values[ii] = enums[ii].name();
        }/* ww w.ja  va  2  s.  com*/
        return values;
    }
    return null;
}

From source file:org.ownchan.server.joint.persistence.valuetype.PersistableEnum.java

/**
 * @throws IllegalArgumentException - if the enum type has no constant with the specified id
 * @throws NullPointerException - if id is null
 *//*  w ww.java  2  s .c  o m*/
public static <T extends PersistableEnum<T>> T valueOf(Short id, Class<T> clazz) {
    IllegalArgumentException argEx = new IllegalArgumentException(
            String.format("no matching constant found for id %s", id));

    T[] availableValues = clazz.getEnumConstants();

    if (ArrayUtils.isEmpty(availableValues)) {
        throw argEx;
    }

    return Arrays.stream(availableValues).filter(id::equals).findAny().orElseThrow(() -> argEx);
}

From source file:org.mrgeo.utils.StringUtils.java

public static <T extends Enum<T>> List<String> enumToStringList(Class<T> enumType) {
    List<String> values = new ArrayList<String>();
    for (T c : enumType.getEnumConstants()) {
        values.add(c.name());/*from w  w  w.j a  v a  2 s . c om*/
    }
    return values;
}

From source file:com.wavemaker.tools.apidocs.tools.parser.util.DataTypeUtil.java

/**
 * It will parse the given {@link Class} as {@link Enum}, returns the list of values of given {@link Enum}
 *
 * @param type data type/*from  ww w. j ava2 s. c  o  m*/
 * @param <E>  data type
 * @return {@link List} of enum name Strings.
 * @throws IllegalArgumentException when given type is not an {@link Enum}.
 */
public static <E> List<String> getEnumValues(Class<E> type) {
    if (isEnum(type)) {
        Class<Enum> enumClass = (Class<Enum>) type;
        List<String> values = new ArrayList<>(enumClass.getEnumConstants().length);
        for (Enum anEnum : enumClass.getEnumConstants()) {
            values.add(anEnum.name());
        }
        return values;
    } else {
        throw new IllegalArgumentException("Given type is not a Enum");
    }
}

From source file:ch.ethz.inf.vs.hypermedia.client.Utils.java

public static <T extends Enum<T>> T selectFromEnum(Class<T> enumType, Scanner scanner, String prompt) {
    T[] consts = enumType.getEnumConstants();
    String options = Stream.of(consts).map(x -> x.ordinal() + ": " + x).collect(Collectors.joining(", "));
    int index = -1;
    do {// w ww.j ava 2 s . c  o m
        System.out.printf("%s (%s): %n", prompt, options);
        String val = scanner.next().trim();
        try {
            index = Integer.parseInt(val);
        } catch (NumberFormatException e) {

        }
    } while (!(index >= 0 && index < consts.length));
    return consts[index];
}

From source file:com.hpcloud.mon.app.validation.Validation.java

/**
 * @throws JsonMappingException if the {@code value} is not valid for the {@code type}
 *///from w w w  . ja  va2s.  co  m
public static <T extends Enum<T>> T parseAndValidate(Class<T> type, String value) throws JsonMappingException {
    for (T constant : type.getEnumConstants())
        if (constant.name().equalsIgnoreCase(value))
            return constant;
    List<String> acceptedValues = new ArrayList<>();
    for (T constant : type.getEnumConstants())
        acceptedValues.add(constant.name());
    throw new JsonMappingException(String.format("%s was not one of %s", value, acceptedValues));
}

From source file:streamflow.model.generator.RandomGenerator.java

public static <T> T randomEnum(Class<T> enumClass) {
    T[] enums = enumClass.getEnumConstants();
    return enums[RandomUtils.nextInt(enums.length)];
}

From source file:nz.co.testamation.testcommon.fixture.SomeFixture.java

public static <T extends Enum> T someEnumOtherThan(Class<T> enumClazz, T... excluded) {
    return someValue(
            Sets.difference(ImmutableSet.copyOf(enumClazz.getEnumConstants()), ImmutableSet.copyOf(excluded)));
}