1

I'm translating a Java (JDK 1.5) "enum" into Java ME (JDK 1.4).

Many people have suggested retroweaver to parse JDK 1.5 libraries to JDK 1.4 but I've got a lot of problems using it and I really want to get full control of my project due the HW limitations.

What's the best way to translate it or find an equivalent?

/** 
 Authentication enumerates the authentication levels.
*/
public enum Authentication
{
    /** 
     No authentication is used.
    */
    NONE,
    /** 
     Low authentication is used.
    */
    LOW,
    /** 
     High authentication is used.
    */
    HIGH,
    /*
     * High authentication is used. Password is hashed with MD5.
     */
    HIGH_MD5,
    /*
     * High authentication is used. Password is hashed with SHA1.
     */
    HIGH_SHA1,
    /*
     * High authentication is used. Password is hashed with GMAC.
     */
    HIGH_GMAC;

    /*
     * Get integer value for enum.
     */
    public int getValue()
    {
        return this.ordinal();
    }

    /*
     * Convert integer for enum value.
     */
    public static Authentication forValue(int value)
    {
        return values()[value];
    }
}
  • 1
    It really depends on how much you are relying on the unique features of the enum in your code. If there is nothing that tries to serialize or do anything fancy, you can emulate the enum with a class and implement the particular features you are using. You may even just decide to replace the enum with plain constants if you don't need the type safety. – RealSkeptic Jun 26 '15 at 10:25
  • I thing replacing everything with plain constants may work. I'm going to try and see if it works. Tnx – Albert Garcias Mirabent Jun 26 '15 at 10:36

1 Answers1

2

This 1997 article shows how to create enumerated constands in Java.

The idea is to have a final class with a private constructor and public constants. The example used is:

public final class Color {

  private String id;
  public final int ord;
  private static int upperBound = 0;

  private Color(String anID) {
    this.id = anID;
    this.ord = upperBound++;
  }

  public String toString() {return this.id; }
  public static int size() { return upperBound; }

  public static final Color RED = new Color("Red");
  public static final Color GREEN = new Color("Green");
  public static final Color BLUE = new Color("Blue");
}
Telmo Pimentel Mota
  • 4,033
  • 16
  • 22