Java从String创建枚举

时间:2016-01-04 11:46:50

标签: java enums

我有枚举类

public enum PaymentType {

    /**
     * This notify type we receive when user make first subscription payment.
     */
    SUBSCRIPTION_NEW("subscr_signup"),

    /**
     * This notify type we receive when user make subscription payment for next
     * month.
     */
    SUBSCRIPTION_PAYMENT("subscr_payment"),

    /**
     * This notify type we receive when user cancel subscription from his paypal
     * personal account.
     */
    SUBSCRIPTION_CANCEL("subscr_cancel"),

    /**
     * In this case the user cannot change the amount or length of the
     * subscription, they can however change the funding source or address
     * associated with their account. Those actions will generate the
     * subscr_modify IPN that we are receiving.
     */
    SUBSCRIPTION_MODIFY("subscr_modify"),

    /**
     * Means that the subscription has expired, either because the subscriber
     * cancelled it or it has a fixed term (implying a fixed number of payments)
     * and it has now expired with no further payments being due. It is sent at
     * the end of the term, instead of any payment that would otherwise have
     * been due on that date.
     */
    SUBSCRIPTION_EXPIRED("subscr_eot"),

    /** User have no money on card, CVV error, another negative errors. */
    SUBSCRIPTION_FAILED("subscr_failed");

    private String type;

    PaymentType(String type) {
        this.type = type;
    }

    String getType() {
        return type;
    }
}

当我尝试创建枚举时:

PaymentType type = PaymentType.valueOf("subscr_signup");

Java向我发出错误:

IllegalArgumentException occured : No enum constant models.PaymentType.subscr_signup

我如何解决这个问题?

5 个答案:

答案 0 :(得分:2)

添加并使用此方法

public static PaymentType parse(String type) {
    for (PaymentType paymentType : PaymentType.values()) {
        if (paymentType.getType().equals(type)) {
            return paymentType;
        }
    }
    return null; //or you can throw exception
}

答案 1 :(得分:1)

Enum没有准备好使用此方法。您需要使用确切的字段名称:

PaymentType.valueOf("SUBSCRIPTION_MODIFY");

或者编写自己的方法,例如:

public static PaymentType fromString(String string) {
  for (PaymentType pt : values()) {
    if (pt.getType().equals(string)) {
      return pt;
    }
  }
  throw new NoSuchElementException("Element with string " + string + " has not been found");
}

所以这段代码:

public static void main(String[] args) {
  System.out.println(PaymentType.fromString("subscr_modify"));
}

打印:

SUBSCRIPTION_MODIFY

答案 2 :(得分:0)

valueOf返回枚举项,其标识符与您传入的字符串匹配 例如:

PaymentType t = PaymentType.valueOf("SUBSCRIPTION_NEW");

如果要获取type字段与给定字符串匹配的枚举项,请在PaymentType上编写一个静态方法,循环遍历PaymentType.values()并返回匹配项。

答案 3 :(得分:0)

Enum.valueOf()方法将传递给它的String与枚举实例的name属性进行比较 - 实例名称的实际值,因此在您的示例中,将是"SUBSCRIPTION_NEW"而不是"subscr_signup"

您需要编写一个静态方法,该方法返回给定值的正确枚举实例。例如:

public static PaymentType byTypeString(String type) {
    for (PaymentType instance : values()) {
        if (instance.type.equals(type)) {
            return instance;
        }
    }
    throw new IllegalArgumentException("No PaymentType enum instance for type: " + type);
}

答案 4 :(得分:0)

subscr_signup

是枚举变量值,而不是枚举值。 因此,如果您想根据其值名称获取枚举,您必须这样做:

PaymentType type = PaymentType.valueOf("SUBSCRIPTION_NEW");