1

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.

RBz
  • 761
  • 2
  • 13
  • 30
  • 1
    Check this link http://stackoverflow.com/questions/1038321/alternative-to-enum-in-java-1-4 – 11thdimension Apr 19 '16 at 03:03
  • 1
    I showed how to duplicate most of what enums do (including member methods) in this answer: http://stackoverflow.com/questions/4709175/what-are-enums-and-why-are-they-useful/15080106#15080106. The main thing this technique doesn't do is play nicely with `switch`. But you an switch on a scalar member value. – Gene Apr 19 '16 at 03:34
  • @11thdimension I did saw this. But my problem is more when it comes to switch cases. – RBz Apr 19 '16 at 05:57
  • @Gene your answer explains it better. As you mentioned in your answer I am "Not Happy"! But i guess i would have to live with that. – RBz Apr 19 '16 at 05:57
  • A source conversion generating 1.4 sources seems most flexible. Enum backports exist, but a conversion should do to. – Joop Eggen Apr 19 '16 at 10:16

1 Answers1

1

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+.

public interface Type {
   String getDesc();
   String getType();
}

The interface implementation in Java 5+ would be the 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- 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:

for (Type type: types)
    {
        if(nodeValue.equalsIgnoreCase(type.getType()))
        {
            value=type.getDesc();
            break;
        }
    }

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.