enum of Color Codes

10,456

You can use :

public static Color getColor(int colorCode) {
    for (Color c : Color.values()) {
        if (c.getColorCode() == colorCode)
            return c;
    }
    return NOT_DEFINE;
}

Note I defined another enum NOT_DEFINE(8) in case the code not exit and return it in the end of the getColor.


But I would like to create a Map which hold Map<colorCode, Color> so you can get the colors easily with key without looping over the values of the enum each time

static Map<Integer, Color> mapOfColord = new HashMap<>();

static {
    Map<Integer, Color> colors = new HashMap<>();
    for (Color color : Color.values()) {
        colors.put(color.colorCode, color);
    }
    mapOfColord = Collections.unmodifiableMap(colors);
}

Or you can stick with the first method which take the key and return the Color like so :

private static Map<Integer, Color> mapOfColord = new HashMap<>();

static {
    for (Color color : Color.values()) {
        mapOfColord.put(color.colorCode, color);
    }
}

public static Color getColor(Integer key) {
    return mapOfColord.get(key);
}

So you can get the Color like this for example :

int key = 3;
Color blue = Color.mapOfColord.get(key);

Or :

Color blue = Color.getColor(key);

Thank you @Andy Turner for the information, I appreciate it.

Share:
10,456
Marten
Author by

Marten

Updated on June 04, 2022

Comments

  • Marten
    Marten almost 2 years

    I have a Color enum :

    public enum Color {
        YELLOW(0), RED(1), GREEN(2), BLUE(3), GRAY(4), CYAN(5), BLACK(6), MAGENTA(7);
    
        int colorCode;
    
        Color(int colorCode) {
            this.colorCode = colorCode;
        }
    
        public int getColorCode() {
            return this.colorCode;
        }
    
        public Color getColor(int colorCode) {
            return Color.this.colorCode;
        }
    }
    

    I want to return a Color depending of colorCode, but I got a exception

    incompatibles Types int and Color
    

    How can I solve this? Any help would be appreciated.

    • matoni
      matoni almost 6 years
      Enums have "ordinal" number associated with each value. Ordinal corresponds to "definition index", so you can simply write Color.values()[index] to get Color at specified index.
  • Andy Turner
    Andy Turner almost 6 years
    You should make that method static too. And in any case, you shouldn't refer to NOT_EXIST via this.
  • Andy Turner
    Andy Turner almost 6 years
    you don't want it to be an instance method. That would mean you would need an instance of Color on which to invoke the method to get an instance of Color from an int.
  • Andy Turner
    Andy Turner almost 6 years
    You similarly want the Map to be static, in order that you don't have an copy of that map for every enum value.
  • Marten
    Marten almost 6 years
    Thank you @YCF_L and Andy Turner it helps me a lot.
  • Youcef Laidani
    Youcef Laidani almost 6 years
    @AndyTurner I can't make the Map static It will gives you illegal reference to static field from initializer
  • Andy Turner
    Andy Turner almost 6 years
    @YCL_L given that it is a static field that you are initializing, use a static initializer.
  • Youcef Laidani
    Youcef Laidani almost 6 years
    @AndyTurner Its my first time that I use that maybe thank you for teaching me that, Is that what you mean?
  • Andy Turner
    Andy Turner almost 6 years
    @YCF_L you should also either make the map unmodifiable, or only expose a method to get the Color. Otherwise you can change the values in the map (e.g. remove them all using Color.mapOfColord.clear()).
  • Youcef Laidani
    Youcef Laidani almost 6 years
    Thank you @AndyTurner for the information I really appreciate it, what should I do more :)