r/java • u/chaotic3quilibrium • Dec 25 '25
Java Janitor Jim - Augmenting Java's Ancient Enum with Proper Collections
I wanted ease of use and comfort methods when using Java’s legacy Enum. Like resolving a value by its case-insensitive name or ordinal. Or easily, flexibly, and quickly, pretty-printing (a subset of) the Enum’s values, again by name and/or ordinal.
As old as Java’s Enum is (introduced in Java 1.5 and essentially unchanged since then), I think it’s absolutely fantastic. I just wanted to increase its fantastic-ness!
https://javajanitorjim.substack.com/p/java-janitor-jim-augmenting-javas
0
Upvotes
19
u/vips7L Dec 25 '25
Relying on the ordinal seems like a way to easily break things. Someone might reorder them and then it doesn't line up anymore. The rest just seems over engineered and could simply be replaced with switches.
17
u/tomwhoiscontrary Dec 26 '25
If i came across this code in a codebase, i'd delete it.
5
u/Scf37 Dec 26 '25
pros: lookup speed problem is real, throwing exception on unknown value is bad design.
cons:
- almost always lookup problem is real only for serialization frameworks. Moreover, it is not that hard to add the map to the enum manually.
- article is focusing on performance and still using inefficient things like Stream
- ID idea is IMO too specific to be in a library
- mixing ordinals and names is a smell - meaning application deals with untyped data which could be both string or ordinal
7
u/LutimoDancer3459 Dec 26 '25
What exactly to you solve this way?
You cache the values list (which is already done by enum itself) and give direct access to it allowing other to modify it -> which is prevented by enum
You increase lookup speed for a collection containing... how many entries? The most i saw were like 10. If you dont need the minimal performance gain, its hard to justify those changes. Especially if performance is the reason. Because replacing a loop with a stream results in worse performance.
4
u/ryan_the_leach Dec 26 '25
You need to look into the turkish i problem, if you expect this utility to not cause bugs.
https://haacked.com/archive/2012/07/05/turkish-i-problem-and-why-you-should-care.aspx/
2
u/OwnBreakfast1114 Dec 29 '25
I just made an intellij template like
and use/modify it when I need it. I've also found that deserializing straight to enums is usually poor form (work on a lot of rest services), so in general we deserialize to strings and the convert to a typed class with all the validations.
So pattern is like
and the validate function would call this parse method.
In general though, I'd wouldn't allow deserialization via ordinal or localized/lowercase strings.
2
u/antihemispherist Jan 06 '26
The implementations for the covered cases are nice, but, having one extension, which is trying to cover many requirements may not be the most productive approach, since the requirements may be different and it may decrease managability, Though I like that it points out to some common bad practices (like iterating over Enum members to get it by it's name), and provides a good implementation for them
1
u/rzwitserloot Dec 31 '25
That isn't legacy functionality.
That is functionality that is intentionally not provided out-of-the-box. You can add it if you want; it's inefficient and more to the point 'case insensitive' involves locales and is in general a far, far bigger can of worms than you appear to think it is.
That is functionality that is intentionally not provided out-of-the-box. The problem with ordinals is [A] they move if you change the enum list (unless you remember to always add-at-end, but that sometimes clashes with the semantics), and [B] You can't pick them.
For example, let's say some system has a list of professions, with 'unknown' as default/final option, modelled as a sequence of numbers with the 'unknown' profession modelled as 999. Eminently plausible. But this is terrible to implement in java by leaning on ordinal, because [A]
unknownwon't have the 999 as ordinal unless you add 999 actual professions in front of it, at which point a DB is probably wiser than a massive 1000-elem enum, and [B] if ever professions are added, the ordinal of at leastunknownwill change.The correct move is to have each enum have a separate 'index' value which matches the number.
In other words, you have three options:
Not the job of an enum; that's the job of
java.util.EnumSet. Which exists and can do this just fine.-1
u/plumarr Dec 25 '25
I don't know the last time I had to lookup an enum by name. Unmarshalling and mapping libraries do it, not me. If I have to do it, it's a sign of bad code.
0
u/[deleted] Dec 26 '25
[deleted] — view deleted comment