Consider using switch
instead of if
/else
for enums. That is, prefer this:
switch (foo.getBar()) {
case BAZ:
doSomething();
break;
default:
doSomethingElse();
}
instead of this:
if (foo.getBar().equals(Bar.BAZ)) {
doSomething();
} else {
doSomethingElse();
}
Switches on enums
have a few small advantages worth considering:
equals
vs. ==
debate.BAZ
by its simple name without a static import. That’s
good because you might not want to see it appear unqualified in other parts
of the file where the context would not make its meaning so clear.Suppress false positives by adding the suppression annotation @SuppressWarnings("UseEnumSwitch")
to the enclosing element.