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:springfox.documentation.spring.web.readers.parameter.ExpandedParameterBuilder.java

private List<String> getEnumValues(final Class<?> subject) {
    return transform(Arrays.asList(subject.getEnumConstants()), new Function<Object, String>() {
        @Override/*w w w .j a v  a 2s . c  om*/
        public String apply(final Object input) {
            return input.toString();
        }
    });
}

From source file:com.opengamma.masterdb.security.SecurityTestCase.java

protected static <T> List<T> getTestObjects(final Class<T> clazz, final Class<?> parent) {
    final List<T> objects = new ArrayList<T>();
    if (clazz.isEnum()) {
        for (final T value : clazz.getEnumConstants()) {
            objects.add(value);/*from   www.j a va2s  . c  om*/
        }
    } else {
        final Object key;
        if (Collection.class.equals(clazz)) {
            key = Pair.of(parent, clazz);
        } else if (List.class.equals(clazz)) {
            key = Pair.of(parent, clazz);
        } else {
            key = clazz;
        }
        final TestDataProvider<T> provider = (TestDataProvider<T>) s_dataProviders.get(key);
        if (provider == null) {
            throw new IllegalArgumentException("No random provider for " + clazz);
        }
        provider.getValues(objects);
    }
    Collections.shuffle(objects);
    return objects;
}

From source file:capital.scalable.restdocs.constraints.ConstraintReaderImpl.java

private List<String> getEnumConstraintMessage(Class<?> javaBaseClass, String javaFieldName) {
    // could be getter actually
    Field field = findField(javaBaseClass, javaFieldName);
    if (field == null) {
        return emptyList();
    }//from  w  w  w .  j ava2  s  . c  o m

    Class<?> rawClass = field.getType();
    if (!rawClass.isEnum()) {
        return emptyList();
    }

    Class<Enum> enumClass = (Class<Enum>) rawClass;

    String value = arrayToString(enumClass.getEnumConstants());
    String enumName = enumClass.getCanonicalName();
    String message = constraintDescriptionResolver
            .resolveDescription(new Constraint(enumName, singletonMap(VALUE, (Object) value)));

    // fallback
    if (isBlank(message) || message.equals(enumName)) {
        message = "Must be one of " + value;
    }
    return singletonList(message);
}

From source file:org.apache.geode.internal.cache.rollingupgrade.RollingUpgradeDUnitTest.java

public static void createClientRegion(Object cache, String regionName, String shortcutName) throws Exception {
    Class aClass = Thread.currentThread().getContextClassLoader()
            .loadClass("org.apache.geode.cache.client.ClientRegionShortcut");
    Object[] enumConstants = aClass.getEnumConstants();
    Object shortcut = null;//from  w  w  w .  j  a v a 2 s  .  c o  m
    int length = enumConstants.length;
    for (int i = 0; i < length; i++) {
        Object constant = enumConstants[i];
        if (((Enum) constant).name().equals(shortcutName)) {
            shortcut = constant;
            break;
        }
    }
    Object clientRegionFactory = cache.getClass().getMethod("createClientRegionFactory", shortcut.getClass())
            .invoke(cache, shortcut);
    clientRegionFactory.getClass().getMethod("create", String.class).invoke(clientRegionFactory, regionName);
}

From source file:ru.jts_dev.common.packets.IncomingMessageWrapper.java

public final <E extends Enum<?>> E readIntAs(final Class<E> enumClass) {
    final int value = readInt();

    if (value < 0 || value > enumClass.getEnumConstants().length)
        throw new IndexOutOfBoundsException(
                "value " + value + " not in enum constants of " + enumClass.getName());

    return enumClass.getEnumConstants()[value];
}

From source file:edu.usu.sdl.openstorefront.validation.ValidValueRule.java

@Override
protected boolean validate(Field field, Object dataObject) {
    boolean valid = true;

    ValidValueType validValueType = field.getAnnotation(ValidValueType.class);
    if (validValueType != null) {
        try {/* w w  w. j  av a 2s. c om*/
            String value = BeanUtils.getProperty(dataObject, field.getName());
            //Note: null values are checked by a separate rule so null should be valid
            if (value != null) {
                Set<String> validValueSet = new HashSet<>();
                Service serviceProxy = ServiceProxyFactory.getServiceProxy();
                if (validValueType.lookupClass().length > 0) {
                    for (Class lookupClass : validValueType.lookupClass()) {
                        List<LookupEntity> lookups = serviceProxy.getLookupService().findLookup(lookupClass);
                        lookups.forEach(item -> {
                            validValueSet.add(item.getCode());
                        });
                    }
                }
                validValueSet.addAll(Arrays.asList(validValueType.value()));
                if (validValueType.enumClass().length > 0) {
                    for (Class enumClass : validValueType.enumClass()) {
                        Object enumValues[] = enumClass.getEnumConstants();
                        if (enumValues != null) {
                            for (Object enumValue : enumValues) {
                                validValueSet.add(enumValue.toString());
                            }
                        }
                    }
                }

                if (validValueSet.contains(value) == false) {
                    valid = false;
                }
            }
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
            throw new OpenStorefrontRuntimeException("Unexpected error occur trying to get value from object.",
                    ex);
        }
    }
    return valid;
}

From source file:ctrus.pa.bow.core.UnWeightedMultiBagOfWords.java

public final <E extends Enum<E>> void setBowIdentifiers(final Class<E> bowIdentifiers) {
    // Initialize all terms map with list of identifiers
    E[] values = bowIdentifiers.getEnumConstants();
    for (E value : values) {
        _terms.put(value.toString(), new UnWeightedBagOfWords() {
            @Override// w  ww.j  a va2  s  . c om
            public void create() {
            }
        }.setFilterations(_filterations).setTransformations(_transformations));
    }
}

From source file:nc.noumea.mairie.annuairev2.saisie.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 ww.j  a v  a 2  s.  c  om*/
 * 
 * @param enumClassName
 *            nom complet de la classe (avec le package, ex :
 *            "nc.noumea.mairie.pdc.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) {
        Messagebox.show(e.toString());
    }
    return null;
}

From source file:org.talend.daikon.properties.property.EnumProperty.java

public EnumProperty(Class<T> zeEnumType, String name) {
    super(zeEnumType, name, null);
    // set the possible values accoording with all the enum types.
    T[] enumConstants = zeEnumType.getEnumConstants();
    this.setPossibleValues(enumConstants);

}

From source file:ctrus.pa.bow.DefaultMultiBagOfWords.java

protected <E extends Enum<E>> void writeToOutput(String doc, Class<E> identifier) throws IOException {
    E[] values = identifier.getEnumConstants();
    for (E value : values) {
        String key = value.toString();

        // Check if the identifier directories are created
        if (!_outDir.containsKey(key)) {
            File f = new File(_outParentDir, key);
            if (!f.exists())
                f.mkdirs();/*from  ww  w. ja  va  2  s  . co  m*/
            _outDir.put(key, f);
        }

        if (getTermCount(value) > 0) {
            if (_singleFileOut) {
                // Check if we have a output stream available
                if (!_outs.containsKey(key)) {
                    _outs.put(key, new FileOutputStream(new File(_outDir.get(key), _singleOutputFileName)));
                }
                IOUtils.write(doc + "=", _outs.get(key));
                writeTo(_outs.get(key), value);
            } else {
                // replace special characters in the file name 
                doc = doc.replace('<', '[');
                doc = doc.replace('>', ']');

                FileOutputStream out = new FileOutputStream(new File(_outDir.get(key), doc));
                writeTo(out, value);
                out.close();
                out = null;
            }
        }
    }
}