-3

I have enum class

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;
    }
}

When I try to create enum :

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

Java throws to me error :

IllegalArgumentException occured : No enum constant models.PaymentType.subscr_signup

How I can fix this?

Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119

5 Answers5

3

Enum does not have ready to use method for this. You either need to use exact field names:

PaymentType.valueOf("SUBSCRIPTION_MODIFY");

Or write your own method, for example:

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");
}

So this code:

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

Prints:

SUBSCRIPTION_MODIFY
Bartosz Firyn
  • 2,664
  • 2
  • 21
  • 18
2

add and use this method

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
}
Rustam
  • 1,397
  • 1
  • 13
  • 17
0

valueOf returns the enum item whose identifier matches the string you passed in.
For instance:

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

If you want to get the enum item whose type field matches a given string, write a static method on PaymentType that loops through PaymentType.values() and returns the matching item.

khelwood
  • 55,782
  • 14
  • 81
  • 108
0

The Enum class .valueOf() method is comparing the String passed to it to the name property of the enum instance - the actual value of the instance name, so in your example, this would be "SUBSCRIPTION_NEW" rather than "subscr_signup".

You will need to write a static method which returns the correct enum instance for the given value. For example:

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);
}
Ross Taylor-Turner
  • 3,687
  • 2
  • 24
  • 33
  • Why not? If you can't modify the PaymentType enum, just add this as a static method somewhere else using the `getType()` accessor rather than `type` directly. – Ross Taylor-Turner Jan 04 '16 at 11:58
0
subscr_signup

is an enum variable value, not enum value. So if you want to get enum based on its value name you have to do it that way:

PaymentType type = PaymentType.valueOf("SUBSCRIPTION_NEW");
over9k
  • 115
  • 1
  • 10