Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enum type with numeric constant

Tags:

java

I'm getting data from a legacy system where a certain one byte field is a code that may contain a letter or a number. I want to map it to an enum but I'm not sure how to handle the numeric values.

public enum UsageCode {
    A ("Antique"),
    F ("Flood Damaged"),
    N ("New");
//  0 ("Unknown")  How to allow for value of "0"?

    private final String description;

    UsageCode(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }
}
like image 443
Tom Avatar asked Apr 21 '11 16:04

Tom


People also ask

Can enum values be numbers?

Numeric EnumNumeric enums are number-based enums i.e. they store string values as numbers. Enums are always assigned numeric values when they are stored. The first value always takes the numeric value of 0, while the other values in the enum are incremented by 1.

Are constants allowed in enum?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.

Can we have integer enum?

No, we can have only strings as elements in an enumeration.

How do you assign a value to the constant of an enum?

To hold the value of each constant you need to have an instance variable (generally, private). You cannot create an object of an enum explicitly so, you need to add a parameterized constructor to initialize the value(s). The initialization should be done only once.


2 Answers

Turn it inside out:

public enum UsageCode {
    ANTIQUE ('A'),
    FLOOD_DAMAGED ('F'),
    NEW ('N');
    UNKNOWN ('0')

    private static final Map<Character, UsageCode> charToEnum
            = new HashMap<Character, UsageCode>();

    static { // Initialize map from legacy code to enum constant
        for (UsageCode code : values())
        charToEnum.put(code.getCode(), code);
    }

    // Returns UsageCode for legacy character code, or null if code is invalid
    public static UsageCode fromLegacyCode(char code) {
        return charToEnum.get(code);
    }

    private final char code;

    UsageCode(char code) {
        this.code = code;
    }

    public char getCode() {
        return code;
    }
}

For converting the incoming character codes into enum values, I added an inner Map<Character, UsageCode> and a static conversion method.

Example adapted from Effective Java 2nd Edition, Item 30.

like image 199
Péter Török Avatar answered Oct 11 '22 18:10

Péter Török


You can do it other way round, having a meaningful constant and storing legacy value representation:

public enum UsageCode {

   ANTIQUE("A"),
   FLOOD_DAMAGED("F"),
   NEW("N"),
   UNKNOWN("0");

   private String legacy;

   private UsageCode(String legacy) {
      this.legacy = legacy;
   }

   public static UsageCode toUsageCode(String legacyOutput) {
      for(UsageCode code : values()) {
         if (code.legacy.equals(legacyOutput)) {
            return code;
         }
      }
      return null;
   }
}
like image 24
mindas Avatar answered Oct 11 '22 18:10

mindas