简体   繁体   English

将枚举类型作为参数传递

[英]Pass enum type as parameter

I want to create a method, that:我想创建一个方法,即:

  • Takes the type of an enum and a String as arguments将枚举字符串的类型作为 arguments
    • The String is the name of one specific enum instance String 是一个特定枚举实例的名称
  • Returns the enum instance that fits that name.返回适合该名称的枚举实例。

What I have tried:我试过的:

In class TestUtil.java :在 class TestUtil.java

public static <E extends Enum<E>> E mapToEnum(Enum<E> mappingEnum, String data) {

    return mappingEnum.valueOf(E, data); // Not working, needs Class of Enum and String value
}

The enum:枚举:

public enum TestEnum {
    TEST1("A"),
    TEST2("B");

    private String value;

    private TestEnum(String value) {
        this.value = value;
    }
}

How it should work (For example in main method):它应该如何工作(例如在 main 方法中):

TestEnum x = TestUtil.mapToEnum(TestEnum.class, "TEST1"); // TEST1 is the name of the first enum instance

The problem is, that I can't figure out what I need to pass into the mapToEnum method, so that I can get the valueOf from that Enum.问题是,我无法弄清楚我需要将什么传递给mapToEnum方法,以便我可以从该 Enum 中获取valueOf

If the code you provided is acceptable:如果您提供的代码是可接受的:

public static <E extends Enum<E>> E mapToEnum(Enum<E> mappingEnum, String data) {

    return mappingEnum.valueOf(E, data); // Not working, needs Class of Enum and String value
}

Then all you have to do is fix it.那么你所要做的就是修复它。

Here's the code I tested:这是我测试的代码:

static <T extends Enum<T>> T mapToEnum(Class<T> mappingEnum, String data) {
    return Enum.valueOf(mappingEnum, data);
}

Usage:用法:

@Test
public void test() {
    TestEnum myEnum = mapToEnum(TestEnum.class, "TEST1");
    System.out.println(myEnum.value); //prints "A"
}

Strongly suggest using Apache commons-lang library for boiler plate function like this...强烈建议像这样使用Apache commons-lang库作为样板 function ......

TestEnum x = EnumUtils.getEnum(TestEnum.class, "TEST1");

... which is exactly the code @Fenio demonstrates but handles null or wrong input with a null instead of throwing an Exception. ...这正是@Fenio 演示的代码,但使用null处理null或错误输入,而不是抛出异常。

If you didn't know about this then check out what the rest of the lang3 library holds.如果您对此一无所知,请查看 lang3 库的 rest 包含什么。 I view it as a de-facto standard, saving millions of devs from re-writing minor plumbing utilities.我将其视为事实上的标准,使数百万开发人员免于重新编写小型管道实用程序。

This is how you can iterate the enum class value and match with the parameter you have passed in the method, please check the below-mentioned code.这就是您如何迭代枚举 class 值并匹配您在方法中传递的参数,请检查下面提到的代码。

  enum TestEnum {
    TEST1("test1"),
    TEST2("test2");

   private String value;

   private TestEnum(String value) {
       this.value = value;
   }
   public String getName() {
       return value;
   }
   public static TestEnum mapToEnum(String data) {
       for (TestEnum userType : TestEnum.values()) {
           if (userType.getName().equals(data)) {
               return userType;
           }
       }
       return null;
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM