java - Implementing enum for reverse compatibility -
an application running in java 1.8 have run in few boxes java 1.4. application uses lot of constants (thousands of them) , implemented using functional enums. best way make reverse compatible?
edit :
i have seen few answers none of them satisfactory. make clear trying achieve here please take @ small example below
public class sometype { public enum type { error("allerror","4"), input("allinput","5"), offline("alloffline","6"),; private final string type; private final string desc; type(string type, string desc) { this.type = type; this.desc = desc; } public string gettype(){ return this.type; } public string getdesc(){ return this.type; } } } }
which consumed like
for (sometype.type type: sometype.type.values()) { if(nodevalue.equalsignorecase(type.gettype())) { value=type.getdesc(); break; } }
so never compatible in 1.4 , have write lot of boilerplate code explained @gene in link provided him. there many classes holding large list of constants in them, feel need better approach. question search better solution.
you use interface in places consume enums - in way don't have change enum implementation in java 5+.
public interface type { string getdesc(); string gettype(); }
the interface implementation in java 5+ same:
public enum type implements type { error("allerror","4"), input("allinput","5"), offline("alloffline","6"),; private final string type; private final string desc; type(string type, string desc) { this.type = type; this.desc = desc; } public string gettype(){ return this.type; } public string getdesc(){ return this.type; } }
in java 5- have implement type using either enum apache-commons or custom implementation ( best have code generator takes enum , converts pre-java 5 class)
the consuming code:
for (type type: types) { if(nodevalue.equalsignorecase(type.gettype())) { value=type.getdesc(); break; } }
where types type[]. don't know if use switch statements, loops work fine.
so way wouldn't have have separate code enum consumers still need rewrite enum enum.
Comments
Post a Comment