Alternative to enum in Java 1.4
Asked Answered
E

4

14

Since Java 1.4 doesn't have enums I'm am doing something like this:

public class SomeClass {
     public static int SOME_VALUE_1 = 0;
     public static int SOME_VALUE_2 = 1;
     public static int SOME_VALUE_3 = 2;

     public void receiveSomeValue(int someValue) {
            // do something
     }
 }

The caller of receiveSomeValue should pass one those 3 values but he can pass any other int. If it were an enum the caller could only pass one valid value.

Should receiveSomeValue throw an InvalidValueException?

What are good alternatives to Java 5 enums?

Experience answered 24/6, 2009 at 13:26 Comment(3)
I am really curious: what reasons are there for still using Java 1.4?Abbacy
I'd suggest to add this fact (mobile) to the title of the questionSizable
Many people are still using 1.4, I don't see the issue.Leverick
F
28

Best to use in pre 1.5 is the Typesafe Enum Pattern best described in the book Effective Java by Josh Bloch. However it has some limitations, especially when you are dealing with different classloaders, serialization and so on.

You can also have a look at the Apache Commons Lang project and espacially the enum class, like John has written. It is an implementation of this pattern and supports building your own enums.

Flagg answered 24/6, 2009 at 13:31 Comment(3)
Dear Ronald, the first link is broken. Would you please fix it? Thank you.Cargian
I replaced the original link with another one. Hope it helps.Flagg
Dear Ronald, the link is broken. Would you please fix it? Thank you.Innerve
W
8

I'd typically create what I call a constant class, some thing like this:

public class MyConstant 
{
  public static final MyConstant SOME_VALUE = new MyConstant(1);
  public static final MyConstant SOME_OTHER_VALUE = new MyConstant(2);
  ...

  private final int id;


  private MyConstant(int id)
  {
    this.id = id;
  }

  public boolean equal(Object object) 
  {
    ...
  }

  public int hashCode() 
  {
    ...
  }
}

where equals and hashCode are using the id.

Wise answered 24/6, 2009 at 13:33 Comment(0)
F
6

Apache Commons Lang has an Enum class that works well and pretty well covers what Java 5 Enums offer.

Fivefinger answered 24/6, 2009 at 13:29 Comment(1)
The link is broken, can you please fix?Frustrate
D
0

If the application code base is going to use lot of enums, then I would prefer following solution, which I have used in my application.

Base Class

public class Enum {
    protected int _enumValue;
    protected Enum(int enumValue) {
        this._enumValue = enumValue;
    }

    public int Value() {
        return this._enumValue;
    }
}

Your enumerations will then follow these pattern

Actual Enum

public class DATE_FORMAT extends Enum {
    public static final int DDMMYYYY = 1;
    public static final int MMDDYYYY = 2;
    public static final int YYYYMMDD = 3;

    public DATE_FORMAT(int enumValue) {
        super(enumValue);
    }
}

And your code can consume this enum as follows

String getFormattedDate(DATE_FORMAT format) {

    String sDateFormatted = "";

    switch (format.Value()) {

        case DATE_FORMAT.DDMMYYYY : 
            break;
        case DATE_FORMAT.MMDDYYYY :
            break;
        case DATE_FORMAT.YYYYMMDD :
            break;
        default:
            break;
    }

    return sDateFormatted;
}

Caller can use the function as

void callerAPI() {
    DATE_FORMAT format = new DATE_FORMAT(DATE_FORMAT.DDMMYYYY);

    String sFormattedDate = getFormattedDate(format);

}

This is yet not full proof against intitializing derived Enum objects with any integer value. However it can provide good syntactic guideline to work in non-enum environment.

Demineralize answered 5/3, 2013 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.