Java通用枚举功能使用java 8默认方法和实用程序类 [英] Java generic enum functionality using java 8 default methods and utility class

查看:1273
本文介绍了Java通用枚举功能使用java 8默认方法和实用程序类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我想出的尝试失败,请参阅 Java Generic Enum类使用反思

Below is the failed attempt which I came up with, referring to Java Generic Enum class using Reflection.

想要找到更好的方式来做到这一点。使用这种方法找到的几个问题:

Wanted to find a better way to do this. Couple of issues I find with this approach:


  • 每次我需要传递类类型。示例 - EnumUtility.fromKey(Country.class,1)

  • Each and every time I need to pass the class type. Example - EnumUtility.fromKey(Country.class, 1)

fromSet在城市和国家中都是重复的

fromSet is duplicated in both City and Country

public enum City implements BaseEnumInterface {

    TOKYO(0), NEWYORK(1);

    private final int key;

    public static Set<Integer> fromValue(Set<City> enums) {
        return enums.stream().map(City::getKey).collect(Collectors.toSet());
    }

    public int getKey() {
        return key;
    }

    private City(int key) {
        this.key = key;
    }
}


public enum Country implements BaseEnumInterface {

    USA(0), UK(1);

    private final int key;

    public static Set<Integer> fromSet(Set<Country> enums) {
        return enums.stream().map(Country::getKey).collect(Collectors.toSet());
    }

    public int getKey() {
        return key;
    }

    private Country(int key) {
        this.key = key;
    }

}


public class EnumUtility {

    public static <E extends Enum<E> & BaseEnumInterface> E fromKey(Class<E> enumClass, Integer key) {
        for (E type : enumClass.getEnumConstants()) {
            if (key == type.getKey()) {
                return type;
            }
        }
        throw new IllegalArgumentException("Invalid enum type supplied");
    }

    public static <E extends Enum<E> & BaseEnumInterface> Set<Integer> fromSet(Class<E> enumClass, Set<E> enums) {
        return enums.stream().map(BaseEnumInterface::getKey).collect(Collectors.toSet());
    }   
}


interface BaseEnumInterface {

    int getKey();

}


public class EnumTester {

    public static void main(String args[]) {
        System.out.println(EnumUtility.fromKey(Country.class, 1));
    }
}


推荐答案

没有办法避免将枚举类传递给fromKey。你还能知道哪些枚举常数来检查所请求的密钥?注意:该方法中的第二个参数应该是类型 int ,而不是整数。在整数实例中使用 == 不会比较数值,它将比较对象引用!

There is no way to avoid passing in the enum class to fromKey. How else would you know which enum constants to check for the requested key? Note: The second parameter in that method should be of type int, not Integer. Using == on Integer instances will not compare numeric values, it will compare object references!

EnumUtility.fromSet应该工作正常,所以每个枚举类根本不需要一个fromSet方法。注意,EnumUtility.fromSet根本不需要一个Class参数,实际上你的代码没有使用该参数。

EnumUtility.fromSet should work fine, so each enum class does not need a fromSet method at all. Note that EnumUtility.fromSet does not need a Class argument at all, and in fact your code is not using that parameter.

这篇关于Java通用枚举功能使用java 8默认方法和实用程序类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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