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:io.cloudslang.content.couchbase.utils.InputsUtil.java

public static <T extends Enum<T>> String getEnumValidValuesString(Class<T> inputEnum) {
    StringBuilder sb = new StringBuilder();
    for (T enumValue : inputEnum.getEnumConstants()) {
        if (AuthType.class.getCanonicalName().equalsIgnoreCase(enumValue.getClass().getCanonicalName())) {
            sb.append(((AuthType) enumValue).getValue()).append(COMMA).append(BLANK_SPACE);
        } else if (BucketType.class.getCanonicalName()
                .equalsIgnoreCase(enumValue.getClass().getCanonicalName())) {
            sb.append(((BucketType) enumValue).getValue()).append(COMMA).append(BLANK_SPACE);
        } else if (ConflictResolutionType.class.getCanonicalName()
                .equalsIgnoreCase(enumValue.getClass().getCanonicalName())) {
            sb.append(((ConflictResolutionType) enumValue).getValue()).append(COMMA).append(BLANK_SPACE);
        } else if (EvictionPolicy.class.getCanonicalName()
                .equalsIgnoreCase(enumValue.getClass().getCanonicalName())) {
            sb.append(((EvictionPolicy) enumValue).getValue()).append(COMMA).append(BLANK_SPACE);
        } else if (RecoveryType.class.getCanonicalName()
                .equalsIgnoreCase(enumValue.getClass().getCanonicalName())) {
            sb.append(((RecoveryType) enumValue).getValue()).append(COMMA).append(BLANK_SPACE);
        }//from   w  w  w  .  j  a  va2 s.co m
    }

    return isBlank(sb.toString()) ? EMPTY : sb.deleteCharAt(sb.length() - 2).toString().trim();
}

From source file:com.evolveum.openicf.lotus.util.DominoUtils.java

private static Set<String> getAttributeNames(Class<? extends DominoAttribute> clazz) {
    Set<String> names = new HashSet<String>();

    DominoAttribute[] enums = clazz.getEnumConstants();
    for (DominoAttribute attr : enums) {
        names.add(attr.getName());//from   ww w .j  ava  2  s . co  m
    }

    return names;
}

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  ww.j  a va 2  s  .c o m*/
    throw new ReadJsonException(clazz + " has no matching enum for " + json);
}

From source file:org.janusgraph.diskstorage.configuration.ConfigOption.java

public static final <E extends Enum> E getEnumValue(String str, Class<E> enumClass) {
    str = str.trim();//  w  w w.  j  av a  2 s. c om
    if (StringUtils.isBlank(str))
        return null;
    for (E e : enumClass.getEnumConstants()) {
        if (e.toString().equalsIgnoreCase(str))
            return e;
    }
    throw new IllegalArgumentException("Invalid enum string provided for [" + enumClass + "]: " + str);
}

From source file:cc.kave.commons.utils.json.JsonUtils.java

private static <T extends Enum<T>> void registerEnum(GsonBuilder gb, Class<T> e) {
    T[] constants = e.getEnumConstants();
    gb.registerTypeAdapter(e, EnumDeSerializer.create(constants));
}

From source file:management.limbr.test.util.PojoTester.java

@SuppressWarnings("unchecked")
private static <T> T createRichObject(Class<T> type) {
    try {/*from   w  w  w .j a  va2 s. com*/
        if (type.equals(Set.class)) {
            return (T) new HashSet<>();
        } else if (type.equals(List.class)) {
            return (T) new ArrayList<>();
        } else if (type.isEnum()) {
            T[] values = type.getEnumConstants();
            return values[random.nextInt(values.length)];
        } else {
            return type.newInstance();
        }
    } catch (IllegalAccessException | InstantiationException ex) {
        throw new IllegalStateException("Couldn't construct object of type " + type.getName() + ".", ex);
    }
}

From source file:com.expressui.sample.dao.init.TestDataInitializer.java

@SuppressWarnings("rawtypes")
public static <T extends Enum> T random(Class<T> enumType) {
    T[] enumConstants = enumType.getEnumConstants();
    Arrays.asList(enumConstants);

    return enumConstants[ReferenceDataInitializer.random(0, enumConstants.length - 1)];
}

From source file:com.omertron.themoviedbapi.AbstractTests.java

/**
 * Build up a full list of the AppendToResponse methods into a string for
 * use in the URL/*from w  w  w  .  ja va  2 s  . co  m*/
 *
 * @param <T>
 * @param methodClass
 * @return
 */
protected static <T extends AppendToResponseMethod> String appendToResponseBuilder(Class<T> methodClass) {
    boolean first = true;
    StringBuilder atr = new StringBuilder();
    for (AppendToResponseMethod method : methodClass.getEnumConstants()) {
        if (first) {
            first = false;
        } else {
            atr.append(",");
        }
        atr.append(method.getPropertyString());
    }
    return atr.toString();
}

From source file:com.streamsets.datacollector.definition.StageDefinitionExtractor.java

private static void addGroupsToList(Class<?> klass, Set<String> set) {
    ConfigGroups groups = klass.getAnnotation(ConfigGroups.class);
    if (groups != null) {
        Class<? extends Enum> groupKlass = (Class<? extends Enum>) groups.value();
        for (Enum e : groupKlass.getEnumConstants()) {
            set.add(e.name());/* w  w w .j a va2  s  . co  m*/
        }
    }
}

From source file:eu.crisis_economics.utilities.EnumDistribution.java

public static <T extends Enum<T>> EnumDistribution<T> // Immutable
        create(Class<T> token, String sourceFile) throws IOException {
    if (token == null)
        throw new NullArgumentException();
    if (!token.isEnum())
        throw new IllegalArgumentException("EnumDistribution: " + token.getSimpleName() + " is not an enum.");
    if (token.getEnumConstants().length == 0)
        throw new IllegalArgumentException("EnumDistribution: " + token.getSimpleName() + " is an empty enum.");
    EnumDistribution<T> result = new EnumDistribution<T>();
    result.values = token.getEnumConstants();
    result.probabilities = new EnumMap<T, Double>(token);
    Map<String, T> converter = new HashMap<String, T>();
    final int numberOfValues = result.values.length;
    int[] valueIndices = new int[numberOfValues];
    double[] valueProbabilities = new double[numberOfValues];
    BitSet valueIsComitted = new BitSet(numberOfValues);
    {/*www .j a v  a 2  s  . c  om*/
        int counter = 0;
        for (T value : result.values) {
            valueIndices[counter] = counter++;
            result.probabilities.put(value, 0.);
            converter.put(value.name(), value);
        }
    }
    BufferedReader reader = new BufferedReader(new FileReader(sourceFile));
    try {
        String newLine;
        while ((newLine = reader.readLine()) != null) {
            if (newLine.isEmpty())
                continue;
            StringTokenizer tokenizer = new StringTokenizer(newLine);
            final String name = tokenizer.nextToken(" ,:\t"), pStr = tokenizer.nextToken(" ,:\t");
            if (tokenizer.hasMoreTokens())
                throw new ParseException(
                        "EnumDistribution: " + newLine + " is not a valid entry in " + sourceFile + ".", 0);
            final double p = Double.parseDouble(pStr);
            if (p < 0. || p > 1.)
                throw new IOException(pStr + " is not a valid probability for the value " + name);
            result.probabilities.put(converter.get(name), p);
            final int ordinal = converter.get(name).ordinal();
            if (valueIsComitted.get(ordinal))
                throw new ParseException("The value " + name + " appears twice in " + sourceFile, 0);
            valueProbabilities[converter.get(name).ordinal()] = p;
            valueIsComitted.set(ordinal, true);
        }
        { // Check sum of probabilities
            double sum = 0.;
            for (double p : valueProbabilities)
                sum += p;
            if (Math.abs(sum - 1.) > 1e2 * Math.ulp(1.))
                throw new IllegalStateException("EnumDistribution: parser has succeeded, but the resulting "
                        + "probaility sum (value " + sum + ") is not equal to 1.");
        }
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    } finally {
        reader.close();
    }
    result.dice = new EnumeratedIntegerDistribution(valueIndices, valueProbabilities);
    return result;
}