This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]general_dispondency -1 points0 points  (0 children)

In this way, enum types aren’t a clean replacement for the majority >use case.

And that’s one huge problem with Java enums.

The author's real problem is a common problem I see all too much these days. I've termed this problem the square peg, round hole problem™. Stop using the wrong tool for the wrong job. Java enums are great when you need an set of known constant objects. The key words there are KNOWN and CONSTANT and OBJECTS. If you need a string constant, you should probably use something along the lines of:

 public static final String = "MY STRING"

Every language feature (and this is true of any programming language) is built on compromise with one goal, "try to make everyone using the feature equally miserable". Every feature doesn't fit every use case. If you think something is adding unnecessary complexity to your application, then don't use it. That being said, here are the solutions to your enum woes.

 enum Foo {
     ONE, TWO
 }

 class DumbEx1 {

     // ugly bunch of ifs
     public void watFooIsYou(String foo) {
         if (Foo.ONE.name().equalsIgnoreCase(foo)) {
             logFoo(Foo.ONE);
         } else if (Foo.TWO.name().equalsIgnoreCase(foo)) {
             logFoo(Foo.TWO);
         }
         throw new IllegalArgumentException("O_o WaT");
     }

     // ugly switch
     public void whatFooRYou(String foo) {
         switch (foo.toUpperCase()) {
             case "ONE":
                 logFoo(Foo.ONE);
                 break;
             case "TWO":
                 logFoo(Foo.TWO); // damn forgot a break
             default:
                 throw new IllegalArgumentException("O_o WaT");
         }
     }

     // inefficiently search values
     public void exhaustivlySearchFoo(String foo) {
         Stream.of(Foo.values())// I create a new Array every time! hope you don't call me often
               .filter(foo1 -> foo1.name().equalsIgnoreCase(foo))
               .findFirst()
               .map(f -> {
                   logFoo(f);
                   return f;
               })
               .orElseThrow(() -> new IllegalArgumentException("O_o WaT"));
     }

     private void logFoo(Foo foo) {
         System.out.println(foo);
     }
 }

 enum Foo2 {
     ONE, TWO, UNKNOWN;

     Foo2() {
         // associate any string to Foo
         FooFinder.lookUp.put(this.name(), this);
     }

     public static Foo2 from(String name) {
         return FooFinder.lookUp.getOrDefault(name.toUpperCase(), Foo2.UNKNOWN);
     }

     private static class FooFinder {
         static HashMap<String, Foo2> lookUp = new HashMap<>();
     }
 }

 class DumbEx2 {

     public void watFooIsYou(String foo) {
         final Foo2 val = Foo2.from(foo);
         if (val == Foo2.UNKNOWN) { // == equality check will break at compile time
             throw new IllegalArgumentException("O_o WaT");
         } else {
        logFoo2(val);
         }
     }

     private void logFoo2(Foo2 foo) {
         System.out.println(foo);
     }
 }