优化java枚举
我的代码包含多个枚举,如下所示。基本上,这有助于通过整数而不是枚举值使用枚举。是否可以应用某种优化,比如继承或其他什么,这样所有人都可以有下面这样的行为优化java枚举,java,enums,Java,Enums,我的代码包含多个枚举,如下所示。基本上,这有助于通过整数而不是枚举值使用枚举。是否可以应用某种优化,比如继承或其他什么,这样所有人都可以有下面这样的行为 public enum DeliveryMethods { STANDARD_DOMESTIC(1), STANDARD_INTERNATIONAL(2), EXPRESS_DOMESTIC(3), EXPRESS_INTERNATIONAL(4); private final int code; private
public enum DeliveryMethods {
STANDARD_DOMESTIC(1), STANDARD_INTERNATIONAL(2), EXPRESS_DOMESTIC(3), EXPRESS_INTERNATIONAL(4);
private final int code;
private DeliveryMethods(int code) {
this.code = code;
}
public int getCode() {
return code;
}
private static final HashMap<Integer, DeliveryMethods> valueMap = new HashMap<>(2);
static {
for (DeliveryMethods type : DeliveryMethods.values()) {
valueMap.put(type.code, type);
}
}
public static DeliveryMethods getValue(int code) {
return valueMap.get(code);
}
}
公共枚举传递方法{
STANDARD_国内(1)、STANDARD_国际(2)、EXPRESS_国内(3)、EXPRESS_国际(4);
私有最终整数码;
私有交付方法(int代码){
this.code=代码;
}
公共int getCode(){
返回码;
}
私有静态最终HashMap valueMap=新HashMap(2);
静止的{
for(DeliveryMethods类型:DeliveryMethods.values()){
valueMap.put(type.code,type);
}
}
公共静态交付方法getValue(int代码){
返回valueMap.get(代码);
}
}
< /代码> 您可以考虑使用EnUM的GealStand()方法,而不是自己维护“代码”。
即使您维护“code”属性,也不必维护“valueMap”。相反,您可以使用EnUM的“值()”方法并遍历所有枚举。< P>您可以考虑使用EnUM的GealStRead()方法,而不是自己维护“代码”。
即使您维护“code”属性,也不必维护“valueMap”。相反,您可以使用Enum的“values()”方法并迭代所有枚举。除非有必要,否则不需要Hashmap
。对于Enum
值,最好使用开关大小写
我已经编写了从整数和字符串中获取枚举的程序
public enum DeliveryMethods {
STANDARD_DOMESTIC(1), STANDARD_INTERNATIONAL(2), EXPRESS_DOMESTIC(3), EXPRESS_INTERNATIONAL(4);
private final int code;
private DeliveryMethods(int code) {
this.code = code;
}
public int getCode() {
return code;
}
public static DeliveryMethods fromString(String code) {
if (code.matches("[1-4]")) {
return fromInteger(Integer.valueOf(code));
}
throw new RuntimeException("No values for code " + code);
}
public static DeliveryMethods fromInteger(int code) {
switch (code) {
case 1:
return STANDARD_DOMESTIC;
case 2:
return STANDARD_INTERNATIONAL;
case 3:
return EXPRESS_DOMESTIC;
case 4:
return EXPRESS_INTERNATIONAL;
}
throw new RuntimeException("No values for code " + code);
}
public static DeliveryMethods fromIntegerType2(int code) {
for (DeliveryMethods d : DeliveryMethods.values()) {
if (d.getCode() == code) {
return d;
}
}
throw new RuntimeException("No values for code " + code);
}
}
除非有必要,否则不需要使用Hashmap
。对于enum
值,最好使用switch case
我已经编写了从整数和字符串中获取枚举的程序
public enum DeliveryMethods {
STANDARD_DOMESTIC(1), STANDARD_INTERNATIONAL(2), EXPRESS_DOMESTIC(3), EXPRESS_INTERNATIONAL(4);
private final int code;
private DeliveryMethods(int code) {
this.code = code;
}
public int getCode() {
return code;
}
public static DeliveryMethods fromString(String code) {
if (code.matches("[1-4]")) {
return fromInteger(Integer.valueOf(code));
}
throw new RuntimeException("No values for code " + code);
}
public static DeliveryMethods fromInteger(int code) {
switch (code) {
case 1:
return STANDARD_DOMESTIC;
case 2:
return STANDARD_INTERNATIONAL;
case 3:
return EXPRESS_DOMESTIC;
case 4:
return EXPRESS_INTERNATIONAL;
}
throw new RuntimeException("No values for code " + code);
}
public static DeliveryMethods fromIntegerType2(int code) {
for (DeliveryMethods d : DeliveryMethods.values()) {
if (d.getCode() == code) {
return d;
}
}
throw new RuntimeException("No values for code " + code);
}
}
下面是一个示例,展示了如何委托给另一个类:
public interface Keyed<K> {
/**
* returns the key of the enum
*/
K getKey();
}
public class KeyEnumMapping<K, E extends Enum<?> & Keyed<K>> {
private Map<K, E> map = new HashMap<>();
public KeyEnumMapping(Class<E> clazz) {
E[] enumConstants = clazz.getEnumConstants();
for (E e : enumConstants) {
map.put(e.getKey(), e);
}
}
public E get(K key) {
return map.get(key);
}
}
public enum Example implements Keyed<Integer> {
A(1),
B(3),
C(7);
private static final KeyEnumMapping<Integer, Example> MAPPING = new KeyEnumMapping<>(Example.class);
private Integer value;
Example(Integer value) {
this.value = value;
}
@Override
public Integer getKey() {
return value;
}
public static Example getByValue(Integer value) {
return MAPPING.get(value);
}
public static void main(String[] args) {
System.out.println(Example.getByValue(3));
}
}
已设置密钥的公共接口{
/**
*返回枚举的键
*/
K getKey();
}
公共类KeyEnumMapping>{
私有映射映射=新的HashMap();
公钥EnumMapping(类clazz,函数keyExtractor){
E[]enumConstants=clazz.getEnumConstants();
对于(E:enumConstants){
map.put(键提取器.apply(e),e);
}
}
公共E-get(K键){
返回map.get(key);
}
}
公共枚举示例{
A(1),
B(3),
C(7);
私有静态最终KeyEnumMapping=
新的KeyEnumMapping(Example.class,Example::getValue);
私有整数值;
示例(整数值){
这个值=值;
}
公共整数getValue(){
返回值;
}
公共静态示例getByValue(整数值){
返回MAPPING.get(值);
}
公共静态void main(字符串[]args){
System.out.println(示例.getByValue(3));
}
}
以下是一个示例,展示了如何委托给另一个类:
public interface Keyed<K> {
/**
* returns the key of the enum
*/
K getKey();
}
public class KeyEnumMapping<K, E extends Enum<?> & Keyed<K>> {
private Map<K, E> map = new HashMap<>();
public KeyEnumMapping(Class<E> clazz) {
E[] enumConstants = clazz.getEnumConstants();
for (E e : enumConstants) {
map.put(e.getKey(), e);
}
}
public E get(K key) {
return map.get(key);
}
}
public enum Example implements Keyed<Integer> {
A(1),
B(3),
C(7);
private static final KeyEnumMapping<Integer, Example> MAPPING = new KeyEnumMapping<>(Example.class);
private Integer value;
Example(Integer value) {
this.value = value;
}
@Override
public Integer getKey() {
return value;
}
public static Example getByValue(Integer value) {
return MAPPING.get(value);
}
public static void main(String[] args) {
System.out.println(Example.getByValue(3));
}
}
已设置密钥的公共接口{
/**
*返回枚举的键
*/
K getKey();
}
公共类KeyEnumMapping>{
私有映射映射=新的HashMap();
公钥EnumMapping(类clazz,函数keyExtractor){
E[]enumConstants=clazz.getEnumConstants();
对于(E:enumConstants){
map.put(键提取器.apply(e),e);
}
}
公共E-get(K键){
返回map.get(key);
}
}
公共枚举示例{
A(1),
B(3),
C(7);
私有静态最终KeyEnumMapping=
新的KeyEnumMapping(Example.class,Example::getValue);
私有整数值;
示例(整数值){
这个值=值;
}
公共整数getValue(){
返回值;
}
公共静态示例getByValue(整数值){
返回MAPPING.get(值);
}
公共静态void main(字符串[]args){
System.out.println(示例.getByValue(3));
}
}
为什么要通过integer使用它?这首先就破坏了使用枚举的一些优势。可能需要一些原因,例如从二进制文件或数据库中解码值。您想要什么“优化”呢?您当前的方法有什么不令人满意的地方?@KevinKrumwiede与MySQL不同,MongoDB存储枚举的字符串值而不是整数值。@ChrisHayes如果我在多个枚举中使用它,此代码将变得重复。所以我在寻找一些我可以调用的函数,或者一些继承,通过它们我可以将这个机制保存在泛型枚举中,以后我可以扩展它。为什么要通过integer使用它?这首先就破坏了使用枚举的一些优势。可能需要一些原因,例如从二进制文件或数据库中解码值。您想要什么“优化”呢?您当前的方法有什么不令人满意的地方?@KevinKrumwiede与MySQL不同,MongoDB存储枚举的字符串值而不是整数值。@ChrisHayes如果我在多个枚举中使用它,此代码将变得重复。因此,我在寻找一些我可以调用的函数或一些继承函数,通过它可以将这个机制保存在泛型枚举中,我可以在以后扩展它。一旦重新排序枚举值或在中间添加一些值,这将中断。GeStRealAL()意味着从0开始的默认整数值,并且与EnUM值无关。如果枚举值的未来位置发生更改,则getOrdinal()将使用DB值失败。@kpavlov yes!确切地说,是的,正如我所说的,如果它符合您的要求,可以考虑使用序数,即使代码不需要维护“ValueMaP”@ DeBurkDek,也没有序号是基本的东西,主要是用DB失败。不同的开发人员可以更改枚举位置。一旦重新排序枚举值或在中间添加一些值,这将中断。GeStRealAL()意味着从0开始的默认整数值,并且与EnUM值无关。如果枚举值的未来位置发生更改,getOrdinal()将使用DB值失败。@kpav