Java enum and if statement, is there a better way to do this?

I have a java Enum class and at runtime I will read the value from the command line and I want to match this value with the value in the Enum class. Shipper is my Enum class. Is there a better way to do this instead of if and else if below? It looks ugly.

 private List<Shipper> shipperName = new ArrayList<Shipper>(); ... public void init(String s){ if(s.equals("a")){ shipperName.add(Shipper.A); }else if(s.equals("b")){ shipperName.add(Shipper.B); }else if(s.equals("c")){ shipperName.add(Shipper.C); }else if(s.equals("d")){ shipperName.add(Shipper.D); }else if(s.equals("e")){ shipperName.add(Shipper.E); }else{ System.out.println("Error"); } } 

Here is my Shipper.class

 public enum Shipper { A("a"), B("b"), C("c"), D("e"), F("f") ; private String directoryName; private Shipper(String directoryName) { this.directoryName = directoryName; } public String getDirectoryName() { return directoryName; } } 
+4
source share
3 answers

Yes, add a string to the enumeration constructor (specify the constructor in the enumeration that accepts the string) and create the enumerations with the text value a, b, c. Etc. Then we implement the static factory method in the enumeration, which takes a string and returns an instance of enum. I call this method textValueOf. The existing valueOf method in the enumeration cannot be used for this. Something like that:

 public enum EnumWithValueOf { VALUE_1("A"), VALUE_2("B"), VALUE_3("C"); private String textValue; EnumWithValueOf(String textValue) { this.textValue = textValue; } public static EnumWithValueOf textValueOf(String textValue){ for(EnumWithValueOf value : values()) { if(value.textValue.equals(textValue)) { return value; } } throw new IllegalArgumentException("No EnumWithValueOf for value: " + textValue); } } 

This case is case insensitive, and the text value may differ from the enumeration name β€” ideally if the database codes are esoteric or non-descriptive, but you need better names in Java code. Then execute the client code:

 EnumWithValueOf enumRepresentation = EnumWithValueOf.textValueOf("a"); 
+6
source
 shipperName.add(Shipper.valueOf(s.toUpperCase())); 

If the name doesn’t always match the enumeration, you can do something like this

 public static Shipper getShipper(String directoryName) { for(Shipper shipper : Shipper.values()) { if(shipper.getDirectoryName().equals(directoryName)) { return shipper; } } return null; //Or thrown exception } 
+5
source

You can get Enum from the valueOf () method:

 public void init(String s) { try { Shipper shipper = Shipper.valueOf(s.toUpperCase()); shipperName.add(shipper); } catch(IllegalArgumentException e) { e.printStackTrace(); } } 

EDIT:

If this is not just a simple case a β†’ A, the idea may arise to create HashMap pairs with the key => value:

 private HashMap<String, Shipper> map; public void init(String s) { if(map == null) { map = new HashMap<String, Shipper>(); map.put("a", Shipper.A); ... //b => B code etc } if(map.containsKey(s)) { shipperName.add(map.get(s)); } else { System.out.println("Error"); } } 

Hope this helps!

+3
source

All Articles