An application running in java 1.8 have to run in few boxes with java 1.4. The application uses a lot of constants (thousands of them) and everything is implemented using functional enums. Which is the best way to make it reverse compatible?
EDIT :
I have already seen few answers but none of them are satisfactory. So to make clear what i am trying to achieve here please take a look at the a small example as 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 will be consumed by something like
for (SomeType.TYPE type: SomeType.TYPE.values())
{
if(nodeValue.equalsIgnoreCase(type.getType()))
{
value=type.getDesc();
break;
}
}
So this will never be compatible in 1.4 and so i would have to write a lot of boilerplate code as explained by @Gene in the link provided by him. As there are so many classes like this holding very large list of constants in them, i feel the need for a better approach. So the question is a search for a better solution.
You could use an interface in all the places that consume the enums - in this way you don't have to change your enum implementation in Java 5+.
The interface implementation in Java 5+ would be the same:
In Java 5- you would have to implement the Type using either Enum from apache-commons or custom implementation ( the best would be to have some code generator the takes enum and converts it to pre-Java 5 class)
The consuming code:
where types is Type[]. I don't know if you use switch statements, but loops will work fine.
So this way you wouldn't have to have separate code for the enum consumers but still need to rewrite the enum to Enum.