枚举和int / String之间的方便地映射 [英] Conveniently map between enum and int / String

查看:131
本文介绍了枚举和int / String之间的方便地映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用只能获得有限数量值的变量/参数时,我尝试始终使用Java的枚举,如

When working with variables/parameters that can only take a finite number of values, I try to always use Java's enum, as in

public enum BonusType {
  MONTHLY, YEARLY, ONE_OFF
}

只要我留在我的代码中,那就行了。但是,我经常需要与其他代码进行接口,这些代码使用了简单的 int (或 String )值,或者我需要从数据库中读取/写入数据,数据存储为数字或字符串。

As long as I stay inside my code, that works fine. However, I often need to interface with other code that uses plain int (or String) values for the same purpose, or I need to read/write from/to a database where the data is stored as a number or string.

在这种情况下,我想要一个方便的将每个枚举值与一个整数相关联的方法,以便我可以转换两种方式(换句话说,我需要一个可逆枚举)。

In that case, I'd like to have a convenient way to associate each enum value with a an integer, such that I can convert both ways (in other words, I need a "reversible enum").

从枚举到int很容易:

Going from enum to int is easy:

public enum BonusType {
  public final int id;

  BonusType(int id) {
    this.id = id;
  }
  MONTHLY(1), YEARLY(2), ONE_OFF(3);
}

然后我可以访问int值作为 BonusType x = MONTHLY; int id = x.id;

Then I can access the int value as BonusType x = MONTHLY; int id = x.id;.

但是,我可以看到没有什么好的方法,即从int到枚举。 BonusType bt = BonusType.getById(2);理想情况下,像

However, I can see no nice way for the reverse, i.e. going from int to enum. Ideally, something like

BonusType bt = BonusType.getById(2); 

我可以想出的唯一解决方案是:

The only solutions I could come up with are:


  • 将查找方法放入枚举中,该枚举使用 BonusType.values()填充地图int - >枚举然后缓存并使用它进行查找。会工作,但我必须将这个方法相同地复制到我使用的每个枚举中: - (。

  • 将查找方法放入静态实用程序类中,然后我只需要一个查找方法,但是我不得不反思,让它为任意的枚举工作。

  • Put a lookup method into the enum, which uses BonusType.values() to fill a map "int -> enum", then caches that and uses it for lookups. Would work, but I'd have to copy this method identically into each enum I use :-(.
  • Put the lookup method into a static utility class. Then I'd only need one "lookup" method, but I'd have to fiddle with reflection to get it to work for an arbitrary enum.

两种方法似乎

任何其他想法/见解?

推荐答案

http://www.javaspecialists.co.za/archive/ Issue113.html

解决方案开始与您的int类似,作为枚举定义的一部分,然后继续创建一个基于泛型查找实用程序:

The solution starts out similar to yours with an int value as part of the enum definition. He then goes on to create a generics-based lookup utility:

public class ReverseEnumMap<V extends Enum<V> & EnumConverter> {
    private Map<Byte, V> map = new HashMap<Byte, V>();
    public ReverseEnumMap(Class<V> valueType) {
        for (V v : valueType.getEnumConstants()) {
            map.put(v.convert(), v);
        }
    }

    public V get(byte num) {
        return map.get(num);
    }
}

这个解决方案很好,不需要'fiddling反思,因为它基于所有枚举类型隐式继承了Enum接口。

This solution is nice and doesn't require 'fiddling with reflection' because it's based on the fact that all enum types implicitly inherit the Enum interface.

这篇关于枚举和int / String之间的方便地映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆