java - Get enum based on addition value? -
this question has answer here:
- can enum based on value of field? 3 answers
i have enum defined such:
private static enum color { black(color.black,"black"), green(color.green,"green"); private color color; private string name; color(string n, color c) { this.name = n; this.color = c; } im trying find way enum constant based on string (which second additional parameter. so, hypothetical example, id like
color.getenumfromstring("green")
public static color getenumfromstring(final string value) { if (value == null) { throw new illegalargumentexception(); } (color v : values()) { if (value.equalsignorecase(v.getvalue())) { return v; } } throw new illegalargumentexception(); }
Comments
Post a Comment