Java Generic Enum类使用Reflection [英] Java Generic Enum class using Reflection
问题描述
在我们的代码中,有10个枚举类具有相同的模板代码,每次都有一个新的枚举类需要被创建时。
In our code there are 10's of enum classes with same template code pasted each and every time when ever a new enum class needs to be created.
需要一种方式以通用方式避免重复此代码。
Need a way to generic way to avoid duplication of this code.
以下是几个相同的枚举类
Below are couple of same enums classes
Class-1
public enum AccountStatus {
ACTIVE("ACTIVE", 1),
INACTIVE("INACTIVE", 2);
private final int value;
private final String key;
AccountStatus(String key, int value) {
this.value = value;
this.key = key;
}
public int getValue() {
return this.value;
}
public String getKey() {
return this.key;
}
@Override
public String toString() {
return String.valueOf(this.value);
}
@JsonCreator
public static AccountStatus create(String key) {
if (key == null) {
throw new IllegalArgumentException();
}
for (AccountStatus v : values()) {
if (v.getKey().equalsIgnoreCase(key)) {
return v;
}
}
throw new IllegalArgumentException();
}
public static AccountStatus fromValue(Integer value) {
for (AccountStatus type : AccountStatus.values()) {
if (value == type.getValue()) {
return type;
}
}
throw new IllegalArgumentException("Invalid enum type supplied");
}
public static AccountStatus fromValue(String key) {
for (AccountStatus type : AccountStatus.values()) {
if (type.getKey().equalsIgnoreCase(key)) {
return type;
}
}
throw new IllegalArgumentException("Invalid enum type supplied");
}
}
Class-2 / p>
Class-2
public enum Gender {
MALE("MALE", 1),
FEMALE("FEMALE", 2);
private final int value;
private final String key;
Gender(String key, int value) {
this.value = value;
this.key = key;
}
public int getValue() {
return this.value;
}
public String getKey() {
return this.key;
}
@Override
public String toString() {
return String.valueOf(this.value);
}
@JsonCreator
public static Gender create(String key) {
if (key == null) {
throw new IllegalArgumentException();
}
for (Gender v : values()) {
if (v.getKey().equalsIgnoreCase(key)) {
return v;
}
}
throw new IllegalArgumentException();
}
public static Gender fromValue(Integer value) {
for (Gender type : Gender.values()) {
if (value == type.getValue()) {
return type;
}
}
throw new IllegalArgumentException("Invalid enum type supplied");
}
public static Gender fromValue(String gender) {
for (Gender type : Gender.values()) {
if (type.getKey().equalsIgnoreCase(gender)) {
return type;
}
}
throw new IllegalArgumentException("Invalid enum type supplied");
}
}
需要一个通用的解决方案来处理类中的所有方法。
Need a generic a solution which handles all the methods in class.
推荐答案
我可以看到的两个选项,都涉及到接口。在这两种情况下,您仍然需要为您的枚举创建构造函数并声明这些字段。
Two options that I can see, and both involve interfaces. In either case though, you will still need to make the constructor for your enum and declare the fields.
- 在您的枚举上实现一个界面
getValue
和getKey
方法,然后移动创建
和fromValue
方法到一个将枚举类作为参数的实用程序类。例如
- Implement an interface on your enum for the
getValue
andgetKey
methods, and then move thecreate
andfromValue
methods to a utility class that takes the enum class as a parameter. e.g.
EnumUtility.create(AccountStatus.class,ACTIVE);
- 使用JDK8接口声明将继承到您的枚举中的默认方法实现。
这篇关于Java Generic Enum类使用Reflection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!