Created
August 29, 2012 09:08
-
-
Save benelog/3508877 to your computer and use it in GitHub Desktop.
Java basic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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