Java Enums are an incredibly useful feature and often under utilized because some libraries don't treat them as first class citizens. They are also often used properly but there is a recurring issue that plagues many code bases which has inspired this post. The problem is simple, how should you get an Enum by its name or value and ignore non existant values?
The Enum
The enum we will be using in our examples. Let's pick a more complex enum to also showcase looking an enum up by another field.
The Problem
Using Enum.valueOf
is great when you know the input is valid. However if you pass in an invalid name an exception will be thrown. In some cases this is fine. Often times we would prefer to just ignore it and return null.
Poor Implementations
It's unfortunate how often the following two approaches appear in code bases. Please don't do this.
Enum.valueOf with Try Catch (Poor)
This bad practice is most commonly made by beginners. Exceptions shouldn't be used for control flow and could have some performance implications. Don't be lazy do it the right way.
Find By Iteration (Poor)
This approach is also quite common (see here), at least the authors know not to try / catch the exceptions. What is wrong with this approach? It's iterating over all enums until it finds the matching enum or returning null with a worst case of N where N is the number of enum values. Some may argue this is being nitpicky and its premature optimization. However, data structures and algorithms are CS fundamentals it's not that much effort to use a Map instead of iterating a collection. Will it drastically improve performance? No, but it is a good habbit. When interviewing a candidate for a job would you be happy with a linear complexity search algorithm? You shouldn't let this code review pass in that case.
Better Implementations
The following all work by using an index in the form of a Map. There are some minor differences as well as boilerplate concerns.
Static Map Index (Better)
What is the correct data structure to use for quick lookups of fixed size? A HashMap. Now with a little extra boiler plate we have a much more efficient lookup as long as we have a good hash function. A bit more verbose, it would be nice if there was a way to reduce the boilerplate.
Guava Enums.getIfPresent (Recommended)
This is such a common use case our friends over at Google made a very clean and boiler plate free solution for us. Under the hood it even uses WeakReferences and WeakHashMaps. Basically this code will create a global static map keyed on the Enum's class name and use it for lookups.
One Step Further Indexing by Field
This exact same approach can be used for additional fields of the enum. It's not uncommon to want to look up an enum by its display name or some other property.
Static Map Indexed by Field (Better)
Same approach as above but indexed on the display name instead of the enum name.
Static Map Indexed by Field Utility (Better)
We can't leverage Guava here since it would be difficult to create unique global keys for the static index. However that doesn't mean we can't make our own helpers!
Now we have a fairly boilerplate free generic solution.
Conclusion
There are several ways to solve the same problem. Some are better than others.
BONUS - Serializing an Enum as an Object with Jackson
If you happened to notice our json output is a full object not just the enum name the magic comes from the Jackson annotation @JsonFormat(shape = JsonFormat.Shape.OBJECT)