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.opensaas.jaudit.test.BeanTest.java

/**
 * Returns test values based on the passed in value type. This method may be
 * overridden for types the base BeanTest class does not support. Currently
 * delegates to the Map returned by {@link #newClassToValues()}.
 * /*  w w w  . ja  v  a2s  .co m*/
 * @param valueType
 *            Class to resolve values for.
 * 
 * @return Object[] matching values to test.
 */
protected Object[] getTestValues(final Class<?> valueType) {

    final Object[] values = CLASS_TO_VALUES.get(valueType);
    if (values == null) {

        // check if enum
        if (valueType.isEnum()) {
            final Object[] enumConsts = valueType.getEnumConstants();
            final List<Object> consts = new ArrayList<Object>(enumConsts.length + 1);
            consts.addAll(Arrays.asList(enumConsts));
            consts.add(null);
            return consts.toArray();
        }

        return EMPTY_ARRAY;
    }
    return values;
}

From source file:com.intuit.wasabi.api.pagination.filters.PaginationFilter.java

/**
 * Performs a fulltext search on all non-excluded fields (see {@link #excludeFromFulltext}).
 * Any successful test causes this method to return true, otherwise it returns false.
 *
 * @param object   the object/*from  w  w  w  . j av  a 2 s .  c  om*/
 * @param enumType the property values
 * @param <V>      the property value enum type
 * @return the test result
 */
/*test*/
final <V extends Enum<V> & PaginationFilterProperty> boolean testFulltext(T object, Class<V> enumType) {
    return Arrays.asList(enumType.getEnumConstants()).parallelStream()
            .filter(field -> !excludeFromFulltext.contains(field))
            .anyMatch(field -> filterByProperty(object,
                    modifyFilterForKey(field, getFulltextPartOfFilter(filter)), field.getPropertyExtractor(),
                    field.getFilterPredicate()));
}

From source file:nc.noumea.mairie.organigramme.core.viewmodel.AbstractViewModel.java

/**
 * Mthode utilitaire, pour lister les valeurs d'une numration (dans
 * l'ordre de leur dclaration), avec la possibilit d'insrer en tte la
 * valeur null./*  w  w w .  ja  v a  2  s. c o m*/
 * 
 * @param enumClassName
 *            nom complet de la classe (avec le package, ex :
 *            "nc.noumea.mairie.organigramme.enums.Civilite")
 * @param insertNull
 *            indique s'il faut insrer en tte de la liste rsultat la
 *            valeur null
 * @return la liste des valeurs numres, dans l'ordre de leur dclaration
 *         (avec null en tte optionnellement)
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public ListModelList<?> getListeEnum(String enumClassName, boolean insertNull) {
    try {
        Class<?> classe = Class.forName(enumClassName);
        ListModelList result = new ListModelList(classe.getEnumConstants());
        if (insertNull) {
            result.add(0, null);
        }
        return result;
    } catch (ClassNotFoundException e) {
        LOGGER.error("erreur sur getListeEnum", e);
        Messagebox.show(e.toString());
    }
    return null;
}

From source file:com.streamsets.pipeline.maven.rbgen.RBGenMojo.java

@SuppressWarnings("unchecked")
private LinkedHashMap<String, String> extractResources(Class klass) throws Exception {
    LinkedHashMap<String, String> map = new LinkedHashMap<>();
    if (klass.isEnum()) {
        Object[] enums = klass.getEnumConstants();
        for (Object e : enums) {
            String name = ((Enum) e).name();
            String text = name;/*  w  w w . j  a  v  a2  s  .  c  om*/
            if (errorCodeClass.isAssignableFrom(klass)) {
                text = invokeMessageMethod(errorCodeClass, "getMessage", e);
            }
            if (labelClass.isAssignableFrom(klass)) {
                text = invokeMessageMethod(labelClass, "getLabel", e);
            }
            map.put(name, text);
        }
    } else if (stageClass.isAssignableFrom(klass)) {
        Annotation stageDef = klass.getAnnotation(stageDefClass);
        if (stageDef != null) {
            String labelText = invokeMessageMethod(stageDefClass, "label", stageDef);
            map.put("stageLabel", labelText);
            String descriptionText = invokeMessageMethod(stageDefClass, "description", stageDef);
            map.put("stageDescription", descriptionText);
            Annotation errorStage = klass.getAnnotation(errorStageClass);
            if (errorStage != null) {
                String errorLabelText = invokeMessageMethod(errorStageClass, "label", errorStage);
                if (errorLabelText.isEmpty()) {
                    errorLabelText = labelText;
                }
                map.put("errorStageLabel", errorLabelText);
                String errorDescriptionText = invokeMessageMethod(errorStageClass, "description", errorStage);
                if (!errorDescriptionText.isEmpty()) {
                    errorDescriptionText = descriptionText;
                }
                map.put("errorStageDescription", errorDescriptionText);

            }
        }
        for (Field field : klass.getFields()) {
            Annotation configDef = field.getAnnotation(configDefClass);
            if (configDef != null) {
                String labelText = invokeMessageMethod(configDefClass, "label", configDef);
                map.put("configLabel." + field.getName(), labelText);
                String descriptionText = invokeMessageMethod(configDefClass, "description", configDef);
                map.put("configDescription." + field.getName(), descriptionText);
            }
        }

    }
    return map;
}

From source file:com.microsoft.aad.adal.example.userappwithbroker.AcquireTokenFragment.java

void bindSpinnerChoice(final Spinner spinner, final Class<? extends Enum> spinnerChoiceClass) {
    final ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item,
            new ArrayList<String>() {
                {/*w  w w  .j ava  2  s . co  m*/
                    for (Enum choice : spinnerChoiceClass.getEnumConstants())
                        add(choice.name());
                }
            });

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
}

From source file:org.openengsb.core.ekb.common.EDBConverter.java

/**
 * Gets an enum value out of an object./*from  w w  w.j av  a 2s. c o  m*/
 */
private Object getEnumValue(Class<?> type, Object value) {
    Object[] enumValues = type.getEnumConstants();
    for (Object enumValue : enumValues) {
        if (enumValue.toString().equals(value.toString())) {
            value = enumValue;
            break;
        }
    }
    return value;
}

From source file:net.uncontended.precipice.reporting.registry.ToJSON.java

public String write(Summary<Result, Rejected> summary) {
    Slice<Result, Rejected>[] slices = summary.getSlices();

    Class<Result> resultClazz = summary.resultClazz;
    Class<Rejected> rejectedClazz = summary.rejectedClazz;

    StringWriter w = new StringWriter();
    try {//from  w w  w .jav  a 2 s  . com
        JsonGenerator generator = jsonFactory.createGenerator(w);
        generator.writeStartObject();
        generator.writeObjectFieldStart("result-to-success?");
        for (Result r : resultClazz.getEnumConstants()) {
            generator.writeObjectField(r.toString(), r.isFailure());
        }
        generator.writeEndObject();
        generator.writeArrayFieldStart("rejected");
        for (Rejected r : rejectedClazz.getEnumConstants()) {
            generator.writeString(r.toString());
        }
        generator.writeEndArray();
        generator.writeArrayFieldStart("slices");
        writeSlice(generator, slices[0]);
        generator.writeEndArray();
        generator.writeEndObject();
        generator.flush();
    } catch (IOException e) {
        e.printStackTrace();
        return EMPTY;
    }

    return w.toString();
}

From source file:com.watabou.utils.Bundle.java

public <E extends Enum<E>> E getEnum(String key, Class<E> enumClass) {
    try {//from  w  w  w  .  j  a  va  2 s.c o m
        return Enum.valueOf(enumClass, data.getString(key));
    } catch (JSONException e) {
        return enumClass.getEnumConstants()[0];
    }
}

From source file:com.jaceace.utils.Bundle.java

public <E extends Enum<E>> E getEnum(String key, Class<E> enumClass) {
    try {//from   w ww.  jav  a  2  s  .  com
        return (E) Enum.valueOf(enumClass, data.getString(key));
    } catch (JSONException e) {
        return enumClass.getEnumConstants()[0];
    }
}