Please rethink extended enums inclusion
Jose Antonio Illescas Del Olmo
jantonio.illescas at rbcdexia-is.es
Fri Oct 7 04:34:31 PDT 2011
Now we have hundred of enums on our project (finantial application with
120.000 lines) an repeat same code on any enum... (*mark as red*)
* Map our enums with old legacy system codes:
public enum Type {
ONE("01), TWO("03"), THREE("03"), ...;
private Type(String code) {
setCode(code);
}
* private String code;
*
* public String code() {
return code;
}
public void setCode(String code) {
this.code = code;
}
** public static Type fromCode(String code) {
if (code == null) return null;
for(Type type: values()) {
if (type.code.equals(code) return type;
}
return null;
}
** public boolean in(Type... types) { // IMO all enums must have
this method
if (types == null) return false;
for(Type type: types) {
if (this == type) return true;
}
return false;
}
*
Or with cache to avoid for iteration on fromCode(String) method => more
code is necesary
public enum Type {
ONE("01), TWO("03"), THREE("03"), ...;
private Type(String code) {
setCode(code);
}
* private String code;
** private static Map<String, Type> cache= new HashMap<String,Type>();
static {
for(Type type: Type.values()) {
cache.put(type.code(), type);
}
}
*
* public String code() {
return code;
}
public void setCode(String code) {
this.code = code;
}
** public static Type fromCode(String code) {
return cache.get(code);
}
** public boolean in(Type... types) { // IMO all enums must have
this method
if (types == null) return false;
for(Type type: types) {
if (this == type) return true;
}
return false;
}*
/Abstract enums with Generic support reduce dramatically the code of
enums, see next code that use abstract enums:
/
*public enum Type extends MyAbstractEnum<String> {
ONE("01), TWO("03"), THREE("03"), ...;
private Type(String code) {
super(code);
}
}*
More information about the coin-dev
mailing list