Java Generics根据值返回Enum

时间:2017-03-08 00:23:52

标签: java generics enums java-8

我有多个与他们共享方法的枚举。如果可能的话,我想将它移动到界面,所以我不必复制它,代码看起来会更清晰。但是经过很多努力,我仍然无法将方法移动到接口。

public enum TypeA {
    ValueAA ("Value AA"),
    ValueAB ("Value AB");

    private final String type;

    TypeA (final String type) {
        this.type = type;
    }

    @JsonValue
    public String getType() {
        return this.type;
    }

    @JsonCreator
    public static TypeA fromValue(final String value) {
        for (TypeA t : TypeA.values()) {
            if (t.getType().equalsIgnoreCase(value)) {
                return t;
            }
        }

        StringBuilder allTypes = new StringBuilder();
        boolean bFirstTime = true;
        for (TypeA val : TypeA.values()) {
            allTypes.append(bFirstTime ? "" : ", ").append(val);
            bFirstTime = false;
        }

        throw new IllegalArgumentException(value + " is an invalid value. Supported values are " + allTypes);
    }
}

public enum TypeB {
    ValueBA ("Value BA"),
    ValueBB ("Value BB");

    private final String type;

    TypeB (final String type) {
        this.type = type;
    }

    @JsonValue
    public String getType() {
        return this.type;
    }

    @JsonCreator
    public static TypeB fromValue(final String value) {
        for (TypeB t : TypeB.values()) {
            if (t.getType().equalsIgnoreCase(value)) {
                return t;
            }
        }

        StringBuilder allTypes = new StringBuilder();
        boolean bFirstTime = true;
        for (TypeB val : TypeB.values()) {
            allTypes.append(bFirstTime ? "" : ", ").append(val);
            bFirstTime = false;
        }

        throw new IllegalArgumentException(value + " is an invalid value. Supported values are " + allTypes);
    }
}

使用Generics和Java 8无论如何都要将getTypefromValue方法移动到接口,以便我可以在所有枚举中共享?另请注意杰克逊注释JsonValue& JsonCreator

1 个答案:

答案 0 :(得分:3)

您可以将fromValue实现移动到interface,但是,我想,您必须在具体类型中保留存根以支持JSON工厂注释:

interface TypeX {
    String getType();
    static <T extends Enum<T>&TypeX> T fromValue(String value, Class<T> type) {
        EnumSet<T> all=EnumSet.allOf(type);
        for (T t: all) {
            if (t.getType().equalsIgnoreCase(value)) {
                return t;
            }
        }
        throw new IllegalArgumentException(all.stream().map(t -> t.getType())
            .collect(Collectors.joining(", ",
                value+" is an invalid value. Supported values are ", "")));
    }
}

public enum TypeA implements TypeX {
    ValueAA ("Value AA"),
    ValueAB ("Value AB");

    private final String type;

    TypeA (final String type) {
        this.type = type;
    }

    @JsonValue
    public String getType() {
        return this.type;
    }

    @JsonCreator
    public static TypeA fromValue(final String value) {
        return TypeX.fromValue(value, TypeA.class);
    }
}

enum TypeB implements TypeX {
    ValueBA ("Value BA"),
    ValueBB ("Value BB");

    private final String type;

    TypeB (final String type) {
        this.type = type;
    }

    @JsonValue
    public String getType() {
        return this.type;
    }

    @JsonCreator
    public static TypeB fromValue(final String value) {
        return TypeX.fromValue(value, TypeB.class);
    }
}

为了完整性,由于type属性是不变的,如果我们使用不同的实现,可以将getType方法移动到interface

interface TypeX {
    @Retention(RetentionPolicy.RUNTIME) @interface Type { String value(); }

    @JsonValue default String getType() {
        for(Field f: getDeclaringClass().getDeclaredFields()) try {
            if(f.isEnumConstant() && f.get(null)==this) {
                return f.getAnnotation(Type.class).value();
            }
        } catch(IllegalAccessException ex) {
            throw new AssertionError(ex);
        }
        throw new IllegalStateException();
    }

    Class<? extends TypeX> getDeclaringClass();

    static <T extends Enum<T>&TypeX> T fromValue(String value, Class<T> type) {
        EnumSet<T> all=EnumSet.allOf(type);
        for (T t: all) {
            if (t.getType().equalsIgnoreCase(value)) {
                return t;
            }
        }
        throw new IllegalArgumentException(all.stream().map(t -> t.getType())
            .collect(Collectors.joining(", ",
                value+" is an invalid value. Supported values are ", "")));
    }
}

public enum TypeA implements TypeX {
    @Type("Value AA") ValueAA,
    @Type("Value AB") ValueAB;

    @JsonCreator
    public static TypeA fromValue(final String value) {
        return TypeX.fromValue(value, TypeA.class);
    }
}

enum TypeB implements TypeX {
    @Type("Value BA") ValueBA,
    @Type("Value BB") ValueBB;

    @JsonCreator
    public static TypeB fromValue(final String value) {
        return TypeX.fromValue(value, TypeB.class);
    }
}

但这有几个缺点,例如在编译时不检查基于反射的访问,并且在运行时可能存在性能缺陷。我不知道@JsonValuedefault继承自interface方法时是否会以预期的方式得到尊重。