Daniel Moura
Daniel Moura

Reputation: 7966

Alternative to enum in Java 1.4

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?

Upvotes: 14

Views: 24502

Answers (4)

Roland Schneider
Roland Schneider

Reputation: 3635

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.

Upvotes: 28

John Meagher
John Meagher

Reputation: 24698

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

Upvotes: 6

bhavik shah
bhavik shah

Reputation: 593

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.

Upvotes: 0

Nick Holt
Nick Holt

Reputation: 34301

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.

Upvotes: 8

Related Questions