0

I have an Enum

public enum status{
  YES,
  NO
}

the input from json string is "Yes" or "No", is there any method in ObjectMapper to match status.YES with "Yes", and status.NO with "No".

I don't want to change enum, because int my previous system, people use the enum all the time, I don't want cause problem for others

Neil
  • 1,924
  • 8
  • 24
  • 37

2 Answers2

0

You can always redefine it like:

public enum Status {
  YES("Yes"),
  NO("No");

  private final String status;

  private Status(final String status) {
    this.status = status;
  }

  public String value() {
    return this.status;
  }
}

And then use something like this: Status.YES.value();

x80486
  • 5,131
  • 4
  • 32
  • 71
  • Thank you for your response, but is there any way I don't need change enum, because I don't mess with the enum everyone is using – Neil Aug 13 '15 at 20:30
  • If you do that you won't break anyone else code...guaranteed! Take a look. You are not changing any structure or any value(s); the only thing you need to get the value you want is to invoke the `.value()` call on any existing enum value in the actual source code – x80486 Aug 13 '15 at 20:37
  • The constructor here must be private. – JamesB Aug 13 '15 at 21:07
0

You can use toString() method available in all Java enums:

http://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html

and on returned String call compareToIgnoreCase method to compare it with input:

http://www.tutorialspoint.com/java/java_string_comparetoignorecase.htm

Or you can call toUpperCase on input String and then comapre them:

http://www.tutorialspoint.com/java/java_string_touppercase.htm

Finally, you can use toString mentioned earlier and put all letters except the first to lower case:

String YesString = enumWithYESValue.toString().substring(0, 1) + enumWithYESValue.toString().substring(1).toLowerCase();

Based on: How to capitalize the first letter of a String in Java?

Community
  • 1
  • 1
Darth Hunterix
  • 1,404
  • 5
  • 27
  • 31