Java EnumSet Usage getDataInEnumClass(String enumClassName)

Here you can find the source of getDataInEnumClass(String enumClassName)

Description

get Data In Enum Class

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static HashMap<String, String> getDataInEnumClass(String enumClassName) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.EnumSet;
import java.util.HashMap;

public class Main {
    @SuppressWarnings("unchecked")
    public static HashMap<String, String> getDataInEnumClass(String enumClassName) {
        try {// ww w.jav a  2 s. c om
            @SuppressWarnings("rawtypes")
            Class clazz = Class.forName(enumClassName);
            // Object enumObj = Class.forName(fullClassName).newInstance();
            if (clazz.isEnum()) {
                return getDataFromEnum(clazz);
            }

        } catch (ClassNotFoundException e) {
            //DO nothing, just return empty map in the end
        }
        return new HashMap<String, String>();
    }

    /**
     * This method return all enums in class.
     * @param enumClass from which will be extracted enums.
     * @return HashMap of value in enum. Key is enum name and concrete value is Enum.toString() result.
     */
    private static <E extends Enum<E>> HashMap<String, String> getDataFromEnum(Class<E> enumClass) {
        HashMap<String, String> enumValues = new HashMap<String, String>();
        for (E enumField : EnumSet.allOf(enumClass)) {
            String key = enumField.name();
            String value = enumField.toString();
            enumValues.put(key, value);
        }
        return enumValues;
    }
}

Related

  1. deepCloneEnumSet(final EnumSet set)
  2. encode(EnumSet set)
  3. extractTypes(final Class type)
  4. findEnumIgnoreCase(Class enumClass, String string, T defValue)
  5. getDataFromEnum(Class enumClass)
  6. getEnumFromString(Class enumClass, String enumValue, boolean compareByValue)
  7. nativeLoadEnumDefaultValues(Class enumType)
  8. nextEnum(T ce)
  9. possibilities(Class enumClass)