I'm trying to use an enum to represent different colors from within the code. The idea is that I receive a hex code, ie #FF0000
of type String, and I want to associate that with a color value from an enum.
Something like:
- I accept the hex code "#FF0000"
- I look at my ColorList enum, and see that #FF0000 is associated with RED
- I get the RED value from the enum
So I'm thinking it has to be something like this:
public enum ColorList {
RED("#FF0000")
ORANGE("FFA500")
private String value;
public String getValue() { return value; }
ColorList(String value) { this.value = value; }
}
public void handleColor(String hexColor) {
// hexColor is = "#FF0000"
ColorList myColor = ColorList.valueOf(hexColor);
doWithColor(myColor);
}
public void doWithColor(ColorList myColor) {
System.out.println(myColor.name());
// I expect RED
System.out.println(myColor.getValue());
// I expect "#FFEDAB"
}
Not sure how to fill in the gaps here. I think the only issue is trying to get the ENUM from an arbitrary value (the hex code), so I can't just use valueOf(hexColor).
CodePudding user response:
You can do something like :
public enum ColorList {
RED("#FF0000"),
ORANGE("FFA500");
private String value;
public String getValue() { return value; }
ColorList(String value) { this.value = value; }
public static ColorList fromHex(String hexColor) {
// if needed instead of loop,
// you can create a static map to search by hex code
for (ColorList color : ColorList.values()) {
if (color.getValue().equals(hexColor)) {
return color;
}
}
return null;
}
// I'm not sure if you need this function.
public static ColorList fromHexOrName(String hexOrName) {
// search by Hexadecimal value
ColorList color = fromHex(hexOrName);
if (color != null) {
return color;
}
// else search by name
try {
return ColorList.valueOf(hexOrName);
}catch (IllegalArgumentException | NullPointerException e) {
return null;
}
}
}
public void handleColor(String hexColor) {
// hexColor is = "#FF0000"
ColorList myColor = ColorList.fromHex(hexColor);
// OR this call below if needed :
// ColorList myColor = ColorList.fromHexOrName(hexColor);
doWithColor(myColor);
}
public void doWithColor(ColorList myColor) {
System.out.println(myColor.name());
// I expect RED
System.out.println(myColor.getValue());
// I expect "#FFEDAB"
}
CodePudding user response:
One option is below:
public void handleColor(String hexColor) {
..
for(ColorList ls :ColorList.values()){
if(ls.getValue().equalIgnoreCase(hexColor)){
System.out.println(ls.name()); // this prints RED for hexColor #FF0000
}
}
..
}
CodePudding user response:
I would do something like this:
public enum Color {
RED("#FF0000"),
ORANGE("FFA500");
private final String hexCode;
private static final Map<String, ColorList> BY_HEX_CODE = new ConcurrentHashMap<>();
static {
for (Color color : values()) {
BY_HEX_CODE.put(color.hexCode, color);
}
}
private Color(String hexCode) {
this.hexCode = hexCode;
}
private hexCode() {
return hexCode;
}
public static AccessType valueOfHexCode(String hexCode) throws NullPointerException {
return BY_HEX_CODE.get(hexCode.toUpperCase());
}
}