我有一个实现(编码)枚举的现有数据库:
一个
乙
ç
我需要转换为(自我描述)XML枚举:
苹果
香蕉
胡萝卜
Java中是否存在已建立的模式?我当前的方法感觉很笨(因为你不能继承/扩展java枚举,所以不能重复用于其他枚举)
public enum Food {
Apple("A", "Apple"),
Banana("B", "Banana"),
Carrot("C", "Carrot");
private final String databaseCode;
private final String xmlCode;
private static final Map<String, Food> databaseCodeToFood =
new HashMap<String, Food>();
static {
for(Food f: values()) {
databaseCodeToFood.put(f,getDatabaseCode(), f);
}
}
private Food(String databaseCode, String xmlCode) {
this.databaseCode = databaseCode;
this.xmlCode = xmlCode;
}
public String getDatabaseCode() {
return databaseCode;
}
public String getXmlCode() {
return xmlCode;
}
public static Food fromDatabaseCode(String databaseCode) {
return databaseCodeToFood.get(databaseCode);
}
}