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.vmware.photon.controller.deployer.helpers.TestHelper.java

public static Object[][] getInvalidStartStages(@Nullable Class<? extends Enum> subStages) {

    if (subStages == null) {

        ///*  w  w w.  jav  a2s.  co m*/
        // N.B. Tasks without defined sub-stages must accept all default start stages.
        //

        throw new IllegalArgumentException("subStages");
    }

    if (!subStages.isEnum() || subStages.getEnumConstants().length == 0) {
        throw new IllegalStateException("Class " + subStages.getName() + " is not a valid enum");
    }

    Enum[] enumConstants = subStages.getEnumConstants();
    List<Object[]> invalidStartStages = new ArrayList<>();
    for (int i = 0; i < enumConstants.length; i++) {
        invalidStartStages.add(new Object[] { TaskState.TaskStage.CREATED, enumConstants[i] });
    }
    invalidStartStages.add(new Object[] { TaskState.TaskStage.STARTED, null });
    for (int i = 0; i < enumConstants.length; i++) {
        invalidStartStages.add(new Object[] { TaskState.TaskStage.FINISHED, enumConstants[i] });
    }
    for (int i = 0; i < enumConstants.length; i++) {
        invalidStartStages.add(new Object[] { TaskState.TaskStage.FAILED, enumConstants[i] });
    }
    for (int i = 0; i < enumConstants.length; i++) {
        invalidStartStages.add(new Object[] { TaskState.TaskStage.CANCELLED, enumConstants[i] });
    }

    Object[][] returnValue = new Object[invalidStartStages.size()][2];
    for (int i = 0; i < invalidStartStages.size(); i++) {
        returnValue[i][0] = invalidStartStages.get(i)[0];
        returnValue[i][1] = invalidStartStages.get(i)[1];
    }

    return returnValue;
}

From source file:net.sf.ipsedixit.core.impl.DefaultDataProvider.java

/**
 * {@inheritDoc}//w ww  .j a v a 2  s. c o  m
 */
public <T extends Enum> T randomEnumValue(Class<? extends T> enumType) {
    return randomArrayElement(enumType.getEnumConstants());
}

From source file:org.openmrs.module.htmlwidgets.web.handler.EnumHandler.java

/** 
 * @see WidgetHandler#parse(String, Class<?>)
 *///w w  w .  ja v a 2  s . c  o m
@Override
public Object parse(String input, Class<?> type) {
    if (StringUtils.isNotBlank(input)) {
        Object[] enums = type.getEnumConstants();
        if (enums != null) {
            for (Object o : enums) {
                if (input.equals(o.toString())) {
                    return o;
                }
            }
        }
    }
    return null;
}

From source file:org.xsystem.bpmn2.formats.xml.EnumConverter.java

@Override
public <T> T convert(Class<T> tClass, Object o) {
    String enumValName = (String) o;
    Enum[] enumConstants = (Enum[]) tClass.getEnumConstants();

    for (Enum enumConstant : enumConstants) {
        if (enumConstant.name().equals(enumValName)) {
            return (T) enumConstant;
        }//from  w w  w  .  j  a v  a  2s  .c o  m
    }

    throw new ConversionException(
            String.format("Failed to convert %s value to %s class", enumValName, tClass.toString()));
}

From source file:org.apache.geode.cache.lucene.LuceneSearchWithRollingUpgradeDUnit.java

public static void createRegion(Object cache, String regionName, String shortcutName) throws Exception {
    Class aClass = Thread.currentThread().getContextClassLoader()
            .loadClass("org.apache.geode.cache.RegionShortcut");
    Object[] enumConstants = aClass.getEnumConstants();
    Object shortcut = null;/*from w ww .  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;
        }
    }

    Method createRegionFactoryMethod = cache.getClass().getMethod("createRegionFactory", aClass);
    createRegionFactoryMethod.setAccessible(true);
    Object regionFactory = createRegionFactoryMethod.invoke(cache, shortcut);
    Method createMethod = regionFactory.getClass().getMethod("create", String.class);
    createMethod.setAccessible(true);
    createMethod.invoke(regionFactory, regionName);
}

From source file:clientapi.value.type.EnumType.java

public EnumType(String name, String parent, String id, String description, Object object, Field field,
        Class<T> enumClass) {
    super(name, parent, id, description, object, field);
    this.values = enumClass.getEnumConstants();
}

From source file:tech.sirwellington.alchemy.generator.ObjectGenerators.java

private static AlchemyGenerator<?> determineGeneratorFor(Class<?> typeOfField, Field field,
        Map<Class<?>, AlchemyGenerator<?>> generatorMappings) {
    AlchemyGenerator<?> generator = generatorMappings.get(typeOfField);

    if (generator != null) {
        //Already found it
        return generator;
    }/* ww  w  .j  a  va2s.co  m*/

    if (isCollectionType(typeOfField)) {

        if (lacksGenericTypeArguments(field)) {
            LOG.warn("POJO {} contains a Collection field {} which is not type-parametrized. Cannot inject.",
                    field.getDeclaringClass(), field);

            return null;
        }

        generator = determineGeneratorForCollectionField(field, typeOfField, generatorMappings);
    } else if (isEnumType(typeOfField)) {
        Object[] enumValues = typeOfField.getEnumConstants();

        if (enumValues == null) {
            LOG.warn("Enum Class {} has no Enum Values: " + typeOfField);
            return null;
        }

        generator = () -> {
            int position = one(integers(0, enumValues.length));
            return enumValues[position];
        };
    } else {
        //Assume Pojo and recurse
        generator = pojos(typeOfField);
    }

    return generator;
}

From source file:com.github.reinert.jjschema.v1.EnumSchemaWrapper.java

private <T> void processEnum(Class<T> type) {
    ArrayNode enumArray = getNode().putArray("enum");
    for (T constant : type.getEnumConstants()) {
        String value = constant.toString();
        // Check if value is numeric
        try {//from w  ww .  jav  a2s.  c  o  m
            // First verifies if it is an integer
            Long integer = Long.parseLong(value);
            enumArray.add(integer);
            setType("integer");
        }
        // If not then verifies if it is an floating point number
        catch (NumberFormatException e) {
            try {
                BigDecimal number = new BigDecimal(value);
                enumArray.add(number);
                setType("number");
            }
            // Otherwise add as String
            catch (NumberFormatException e1) {
                enumArray.add(value);
                setType("string");
            }
        }
    }
}

From source file:org.web4thejob.web.util.ZkUtil.java

private static Combobox getComboForEnumType(Class<?> clazz) {
    Combobox enumbox = new Combobox();
    enumbox.setReadonly(true);//from  ww  w . ja  va 2 s.c  om
    enumbox.setWidth("65%");

    Map<String, L10nString> map = new HashMap<String, L10nString>();
    for (L10nString lstring : L10nUtil.getLocalizableResources(clazz)) {
        map.put(lstring.getKey(), lstring);
    }

    for (Object constant : clazz.getEnumConstants()) {
        Comboitem comboitem = new Comboitem();
        comboitem.setParent(enumbox);
        comboitem.setValue(constant);

        String label = null;
        if (map.containsKey(constant.toString())) {
            label = map.get(constant.toString()).toString();
        }

        if (label == null) {
            label = constant.toString();
        }

        comboitem.setLabel(label);
    }

    return enumbox;
}