1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | import java.util.EnumSet; import java.util.HashMap; import java.util.Map; public enum EnumDemo { // For this enum each value has an identification code // other than the internal ordinal value DEMANDED( 10 ), SPECIFIED( 20 ), EVALUATED( 30 ), DUPLICATE( 33 ), ESTIMATED( 35 ), WORKAROUND ( 36 ), ACCEPTED( 37 ), PLANNED( 40 ), BUSY( 50 ), IMPLEMENTED( 60 ), REJECTED( 100 ); private int code; /** * @param code constructor takes code to be associated with enum */ EnumDemo( int code) { this .code = code; } /** * Helper map for reverse lookup in getByCode method */ private static Map<Integer,EnumDemo> enumByCode; static { enumByCode = new HashMap<Integer, EnumDemo>(); for (EnumDemo val: EnumDemo.values()) { enumByCode.put(val.getCode(),val); } } public int getCode() { return code; } /** * reverse lookup * @param code the code of the enum * @return the enum or null if the code does not exist */ public static EnumDemo getByCode( int code){ return enumByCode.get(code); } /** * test if a code exists * @param code * @return true if the code exists */ public static boolean isCodeValid( int code){ return enumByCode.containsKey(code); } /** * enumToString: EnumDemo.DEMANDED to "DEMANDED" * @return enum value name as a String */ public String getName(){ return toString(); } /** * StringToEnum: " demanded" to EnumDemo.DEMANDED * @param name case insensitive and leading and trailing spaces are ignored * @return the enum constant */ public static EnumDemo getByName(String name){ return EnumDemo.valueOf(name.trim().toUpperCase()); } /** * convert between enum and internal ordinal number * not recommended to expose this, just here for demo purposes * @return internal ordinal number of the constant */ public int getOrdinal(){ return getOrdinal(); } /** * convert from ordinal number to enum. Not recommended. * @param ordinal * @return the enum */ public static EnumDemo getByOrdinal( int ordinal){ return EnumDemo.values()[ordinal]; } // some tests and subsets // alternate methods can be written using the codes /** * * @return is this enum in a range? */ public boolean isOK(){ return ACCEPTED.compareTo( this ) <= 0 && IMPLEMENTED.compareTo( this ) >= 0 ; } /** * @return an enum subrange */ public static EnumSet<EnumDemo> getOK(){ return EnumSet.range(ACCEPTED,IMPLEMENTED); } /** * @return is this enum in a subset? */ public boolean isNOK(){ return this == DUPLICATE || this == WORKAROUND || this == REJECTED; } /** * * @return get an enum subset */ public static EnumSet<EnumDemo> getNOK(){ return EnumSet.of(DUPLICATE,WORKAROUND,REJECTED); } } |
15 April 2012
Java enum demo
Labels:
enum,
java,
jhtp,
patternsEE6,
SL-275
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment