@Override protected String getEnumName(Enum e) { String name = enumClassToNameMap.get(e.getClass()); if (name == null) { throw new IllegalArgumentException("Enum class not known to factory:" + e.getClass()); } return name; }
/** * Helper method that can be used to dynamically figure out formal enumeration type (class) for * given enumeration. This is either class of enum instance (for "simple" enumerations), or its * superclass (for enums with instance fields or methods) */ @SuppressWarnings("unchecked") public static Class<? extends Enum<?>> findEnumType(Enum<?> en) { // enums with "body" are sub-classes of the formal type Class<?> ec = en.getClass(); if (ec.getSuperclass() != Enum.class) { ec = ec.getSuperclass(); } return (Class<? extends Enum<?>>) ec; }
@SuppressWarnings("unchecked") @Override protected Enum createEnum(String name, String value) { Class enumClass = nameToEnumClassMap.get(name); if (enumClass == null) { throw new IllegalArgumentException("Enum name not known to factory:" + name); } Set<Enum> enums = valueToEnumMap.get(value); if (enums == null) { return Enum.valueOf(enumClass, value); } for (Enum e : enums) { if (e.getClass() == enumClass) { return e; } } throw new IllegalArgumentException("Enum value not known to factory:" + value); }
public static Enum<?> setUpEnumAttribute(Scanner scan, Attributes.AttributeEnum attribute) { int num; Attributes.printEnumNValuesOfEnumAttribute(attribute); num = makeSureValInRange( scan, 1, Attributes.getEnumAttrValues(attribute).length, attribute.toString(), ValType.INTEGER, true, true) .intValue(); Enum<?> attr = Attributes.getEnumAttrByVal(num, attribute); System.out.println( inputString(attr.getClass().getSimpleName(), attr.toString(), StringOption.SELECTED, true)); return attr; }