问题描述
我正在尝试创建一个场景,其中 enum类A
中的枚举常量具有关联的 enum类B
和 enum类C
包含自己的常量.枚举类B
和枚举类C
中的常量对 enum类D
中的常量子集进行分组.以下是我想要达到的基本目的:
I am trying to create a scenario where by an enum constant in enum class A
has associated sub enum class B
and enum class C
containing their own constants. The constants in enum class B
and enum class C
group subsets of constants from enum class D
. Below is a basically what I am trying to achieve:
enum A {
CONST_1 ("const_1", B), // B is the associated enum
CONST_2 ("const_2", C); // C in the associated enum
private final String strVal;
private final Enum associatedEnum;
private A (String strVal, Enum associatedEnum) {
this.strVal = strVal;
this.associatedEnum = associatedEnum;
}
public Enum getAssociatedEnum() {
return this.associatedEnum;
}
public String toString() {
return this.strVal;
}
// Associated Enum contained subset of grouped constants
enum B {
CONST_3 (D.CONST_7.toString()),
CONST_4 (D.CONST_8.toString());
private final String strVal;
private B (String strVal) {
this.strVal = strVal;
}
public String toString() {
return this.strVal;
}
}
// Associated Enum contained subset of grouped constants
enum C {
CONST_5 (D.CONST_9.toString()),
CONST_6 (D.CONST_10.toString());
private final String strVal;
private C (String strVal) {
this.strVal = strVal;
}
public String toString() {
return this.strVal;
}
}
}
// Separate Enum containing all ungrouped constants
enum D {
CONST_7 ("const_7"),
CONST_8 ("const_8");
CONST_9 ("const_9"),
CONST_10 ("const_10");
private final String strVal;
private D (String strVal) {
this.strVal = strVal;
}
public String toString() {
return this.strVal;
}
}
很显然,这种语法不适用于OOTB,因为您不能以这种方式传递Java中的类.但是有人能建议我实现这一目标的方法吗?
Obviously this syntax doesn't work OOTB because you cannot pass classes in Java this way. But can anyone suggest a way in which I could achieve this?
我希望使用它来验证客户端应用程序中的静态结构化分组.
I am hoping to use it to validate static structured groupings in a client-side application.
推荐答案
这有望完成您想做的事情.我提供了一个示例用法,其中列出了子枚举类型的值.
This should hopefully do what you want. I've included an example usage where it'll list the sub enum type values.
package a.b.c;
public class EnumsTest {
public enum A {
A1( B.class ),
A2( C.class );
private final Class<? extends Enum<?>> enumClazz;
A( final Class<? extends Enum<?>> enumClazz ) {
this.enumClazz = enumClazz;
}
public Enum<?>[] getSubEnumConstants() {
return enumClazz.getEnumConstants();
}
/**
* @param value
* @return Never null
* @throws IllegalArgumentException To be consistent with Enum.valueOf()
*/
public <T> Enum<?> valueOfSubEnum( final String value ) throws IllegalArgumentException {
for( Enum<?> enumInstance : getSubEnumConstants() ) {
if( enumInstance.name().equals( value ) ) {
return enumInstance;
}
}
throw new IllegalArgumentException( "valueOf for " + enumClazz.getName() + " could not be resoled with the value of " + value );
}
}
public enum B {
B1,
B2;
}
public enum C {
C1,
C2;
}
public static void main( String[] args ) {
for( A a : A.values() ) {
for( Enum<?> enumInstance : a.getSubEnumConstants() ) {
System.out.println( a.name() + ":" + enumInstance.name() );
}
}
Enum<?> valueOfSubEnum = A.A1.valueOfSubEnum( "B2" );
System.out.println( valueOfSubEnum.name() );
}
}
注意:如果要将子类型锁定为特定的集合,可以使它们实现一个接口.
Note: If you want to lock the subtypes down to a specific set, you can make them implement an interface.
这篇关于Java枚举关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!