0

I have a enum class that has values A,B. Here is the class:

@XmlType(name = "MemberType")
@XmlEnum
public enum MemberType {
    A,
    B;
    public String value() {
        return name();
    }

    public static MemberType fromValue(String v) {
        return valueOf(v);
    }
}

I have another enum class similar to this one that has the same values A, and B. This class is generated from my WSDL, and I have no control over its code. What I basically want to do is, equate the two enum values.

Basically say MemberType.A = WSDLClass.A, something like that. What can I try next?

halfer
  • 19,824
  • 17
  • 99
  • 186
rickygrimes
  • 2,637
  • 9
  • 46
  • 69

2 Answers2

1

While you cannot assign one enum type to a different type (and you can't have enum extend some abstract superclass), you can declare a static method (either in MemberType or in some utility class), mapping from WSDLClass to MemberType:

public static MemberType fromWsdl(WSDLClass w) {
    if (w==null) {
      return null;
    } else {
      switch (w) {
         case WSDLClass.A: return MemberType.A;
         case WSDLClass.B: return MemberType.B;
         default: return null;
      }
    }
}

Then you would use that function as follow:

import static xyz.MemberType.fromWsdl;
...
MemberType m = ...;
WSDLClass w = ...;
if (m.equals(fromWsdl(w))) ...
Community
  • 1
  • 1
Javier
  • 12,100
  • 5
  • 46
  • 57
1

If the names are strictly the same, I suppose you can :

private MemberType convertEnum(WSDLClass type) {
    return Enum.valueOf(MemberType.class, type.name());
}

If the names are not strictly the same or could differ in the future you are going to have to do :

private MemberType convertEnum(WSDLClass type) {
    MemberType memberType;
    switch (type) {
    case A:
        memberType = MemberType.A;
        break;
    case B:
        memberType = MemberType.B;
        break;
    default:
        memberType = null;
        break;
    }
    return memberType;
}

If you want to use the first solution but the fact that it throw NullPointerException if type is null bother you can use EnumUtils from Apache Commons Lang.