Skip to content

Instantly share code, notes, and snippets.

@benelog
Created August 29, 2012 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benelog/3508877 to your computer and use it in GitHub Desktop.
Save benelog/3508877 to your computer and use it in GitHub Desktop.
Java basic
public enum OilProduct {
GASOLINE("휘발유", 1), HIGH_GRADE_GASOLINE("고급휘발유", 2), DIESEL("경유", 3);
private String name;
private int productId;
private static Map<Integer, OilProduct> enumMap = new HashMap<Integer, OilProduct>();
static {
for (OilProduct each : OilProduct.values()) {
enumMap.put(each.getProductId(), each);
}
}
private OilProduct(String name, int productId) {
this.name = name;
this.productId = productId;
}
public String getName() {
return name;
}
public int getProductId() {
return productId;
}
public static OilProduct fromProductId(int id) {
OilProduct matched = enumMap.get(id);
if (matched == null) {
throw newIllegalArgumentException("cannot find OilProduct for productId:" + id);
}
return matched;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment