1

I have an enum declared like this

 public enum KDErrors {

  KDR_280(280, "Blue"),
  KDR_281(281, "Red"),
  KDR_282(282, "Green"),
  KDR_284(284, "Yellow");

  private final int code;
  private final String color;

  private KDErrors(int code, String color) {
    this.code = code;
    this.color = color;
  }

  public static String getColorByCode(int colorCode) {

return ???
  }
}

I want to get the value by passing the code, how can I make it work?

Jens
  • 67,715
  • 15
  • 98
  • 113
M Mahrous
  • 25
  • 7
  • If you are not using Enum instance directly anywhere, just declare a static map and add key-values as code-color and skip the entire enum creation. – Jimmy Jul 13 '16 at 14:57
  • I think it is not a soublicate. OP do not want the Enum, OP wants an other Attribute from the enum – Jens Jul 13 '16 at 15:02

3 Answers3

2

Change code to:

public enum KDErrors {

    KDR_280(280, "Blue"), KDR_281(281, "Red"), KDR_282(282, "Green"), KDR_284(284, "Yellow");

    private final int code;

    private final String color;

    private KDErrors(int code, String color) {
        this.code = code;
        this.color = color;
    }

    public int getCode() {
        return code;
    }

    public String getColor() {
        return color;
    }

    public static String getColorByCode(int colorCode) {
        for (KDErrors error : KDErrors.values()) {
            if (error.getCode() == colorCode)
                return error.getColor();
        }
        return null;
    }

}
Jens
  • 67,715
  • 15
  • 98
  • 113
1

You could use a reverse lookup map of either you enum or code

Below is an example using reverse look up for enum based on code :

public enum KDErrors {

  private static Map<Integer, KDErrors> reverseLookUp = new HashMap<>();

  static{
    for (KDErrors error : KDErrors.values()) {
      reverseLookUp.put(error.code, error);
    }
  }
  //you method would look like
  public static String getColorByCode(int colorCode) {
    if(reverseLookUp.get(colorCode) == null)
      return null;
    else 
      return reverseLookUp.get(colorCode).color;
  }
}
Abubakkar
  • 15,488
  • 8
  • 55
  • 83
  • 1
    This will throw a `NullPointerException` if the color code is not found in the map. You should do `Map` or check if the return value of `get` is `null` before accessing a field on it. – Mad Physicist Jul 13 '16 at 14:53
  • You don't need to call `get` twice. Do something like `err = reverseLookUp.get(colorCode); return err == null ? null : err.color;` You can replace the ternary op with `if(err != null) return err.color; return null;` if you have a preference that way. – Mad Physicist Jul 13 '16 at 14:57
1

You have a couple of straightforward options available to you. One is to use a Map<Integer, String> and another is to to a linear search.

Using a map is probably much more efficient, but will take up more space. This is probably not an issue for a small number of enums.

public enum KDErrors {

  KDR_280(280, "Blue"),
  KDR_281(281, "Red"),
  KDR_282(282, "Green"),
  KDR_284(284, "Yellow"),

  private static Map<Integer, String> codeMap = new HashMap<>();

  private final int code;
  private final String color;

  private KDErrors(int code, String color) {
    this.code = code;
    this.color = color;
    codeMap.put(code, color);
  }

  public static String getColorByCode(int colorCode) {
   return codeMap(colorCode);
  }
}

Doing a linear search prevents you from having to allocate additional structures, but it will be a little slower for the average case than a lookup in a HashMap:

public enum KDErrors {

  KDR_280(280, "Blue"),
  KDR_281(281, "Red"),
  KDR_282(282, "Green"),
  KDR_284(284, "Yellow"),

  private final int code;
  private final String color;

  private KDErrors(int code, String color) {
    this.code = code;
    this.color = color;
  }

  public static String getColorByCode(int colorCode) {
   for(KDErrors err : KDErrors.values()) {
       if(err.getCode() == colorCode)
           return error.getColor();
    }
    return null;
  }
}
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • @Jens. Because it is generally a better approach than doing a linear search over a whole rack of items. It won't matter much if there are only a couple of items in the enum, but as the number grows, the map approach will start to shine. – Mad Physicist Jul 13 '16 at 14:58
  • @Jens Thanks for the catch. It was just a stupid copy-n-paste error. Fixed now. – Mad Physicist Jul 13 '16 at 15:03