Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java—如何将字符串转换为实现接口的特定枚举_Java_Spring_Enums_Spring Data Mongodb - Fatal编程技术网

Java—如何将字符串转换为实现接口的特定枚举

Java—如何将字符串转换为实现接口的特定枚举,java,spring,enums,spring-data-mongodb,Java,Spring,Enums,Spring Data Mongodb,考虑以下实现接口的内部枚举: public interface NotificationTypes { public enum CONTACT_LIST implements NotificationTypes{ ADDED("CONTACT_LIST-ADDED"), REMOVED("CONTACT_LIST-REMOVED"); public enum INVITATION implements NotificationTypes{

考虑以下实现接口的内部枚举:

public interface NotificationTypes {

    public enum CONTACT_LIST implements NotificationTypes{
        ADDED("CONTACT_LIST-ADDED"),
        REMOVED("CONTACT_LIST-REMOVED");
        public enum INVITATION implements NotificationTypes{
            ACCEPTED("CONTACT_LIST-INVITATION-ACCEPTED"),
            REJECTED("CONTACT_LIST-INVITATION-REJECTED");

            String name = "";
            private INVITATION(String name){
                this.name = name;
            }

            public String getName(){
                return name;
            }
        };

        String name = "";
        private CONTACT_LIST(String name){
            this.name = name;
        }

        public String getName(){
            return name;
        }
    }
    public String getName();
}

现在考虑数据库/MunGDB中的数据以字符串的形式存储在表/文档中的通知类型。

{
    "_id" : ObjectId("59882ba49e5d82c72ba44fde"),
    "template" : "Contact list Invitation accepted by your friend",
    "type" : "CONTACT_LIST-INVITATION-ACCEPTED"
}
所以我的问题是:如何在运行时将该字符串转换回特定的枚举,而不确切知道要映射的枚举的名称

域类如下所示:

@Document(collection = CollectionNames.XXX_TEMPLATE)
public class XXXTemplate {

    private NotificationTypes type;
    //Other parameters, getters & setters + Constructor
}
我将构建一个地图,并用您拥有的所有实例填充它。然后你可以从那张地图上向上看

我认为编译器在保持同步方面帮不了你多少忙,只不过你可以循环使用EnumType.values,但是你必须记住对所有的enum类型都要这样做

如何在运行时将该字符串转换回特定的枚举,而不确切知道要映射的枚举的名称


通过Enum.valueOf.

基本上只是基于@Thilo的答案,但如果您需要的话,可能是一种更“弹性化”的方式-您可以在配置中定义一个包含所有枚举值的@Bean,如:

@Configuration
public class Config {
    @Bean
    public List<NotificationTypes> notificationTypes() {
        List<NotificationTypes> notificationTypes = new ArrayList<>();

        notificationTypes.addAll(Arrays.asList(NotificationTypes.CONTACT_LIST.values()));
        notificationTypes.addAll(Arrays.asList(NotificationTypes.CONTACT_LIST.INVITATION.values()));

        return notificationTypes;
    }
}
显然,如果找不到枚举,您可能希望得到比只返回null更好的结果,并且您可以在@Bean定义中做一些更聪明的事情来验证枚举是否都有不同的名称,等等。或者,可以想象,在那里使用反射来查找NotificationType的所有实现


我不确定这是否真的比在一个映射中存储所有可能的值有任何额外的好处,但是,正如我所说的,我认为这有点夸张。

但为此,您需要知道它是哪种枚举。它使用标识符,而不是自定义名称。@EJP感谢您的重播。但是这个方法也需要枚举类型,我不知道它在运行时是什么。有太多内部枚举实现同一接口。@Afridi那么您将自己设计到了语言不支持的角落。好的,谢谢您的回复。但如果我的嵌套枚举达到3-4级,那么我必须循环遍历所有枚举,不是吗??有没有什么捷径可以循环所有实现给定interfaceNotificationTypes的枚举?没有,您必须静态地构建映射,在源代码中拼写出所有类型。但是,从该映射进行的查找非常简单和快速。与地图一起生活,或者重新思考你的设计。
@Component
public class NotificationTypeParser {
    @Autowired
    private List<NotificationTypes> notificationTypes;

    public NotificationTypes parseNotificationType(String type) {
        for (NotificationTypes notificationType : notificationTypes) {
            if (notificationType.getName().equals(type)) {
                return notificationType;
            }
        }
        return null;
    }
}