5

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;
    }
}
3
  • ENUMS should be used as constants and not as dynamic values. You should be better off using your own implementation. Commented Apr 21, 2011 at 16:06
  • @Abhishek, it is a good and perfectly valid practice to represent a known and finite range of (primitive) values with an enum. Commented Apr 21, 2011 at 16:10
  • @peter-torok I agree on finite ranges but he is trying to retrieve te values from a database so my assumption was the values are not known only the Range(A-Z & 0-9) is known. Commented Apr 21, 2011 at 16:15

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help. I also need to be able to print a description for each value so I guess I could so something like: ANTIQUE ('A', "Antique") and update the constructor.
@Tom, indeed, you can freely add further properties (or even polymorphic behaviour) to your Java enum.
5

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;
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.