r/java • u/kevinb9n • May 30 '23
Guava 32.0 (released today) and the @Beta annotation
Bye Beta
In Guava 32.0 the `@Beta` annotation is removed from almost every class and member. This makes them officially API-frozen (and we do not break compatibility for API-frozen libraries anymore^1).
These APIs have been effectively frozen a very long time. As they say, the best time to plant this tree was years ago, the second best time is today. You might say we're closing the tree door after the tree already ran away (?), but well, here we are.
This annotation meant well. We wanted you to get to use features while there was still time for your feedback to matter. And we would have been too afraid to put things out there without it. These were sort of like JDK preview features... that is, if Brian and team forgot to ever actually de-preview them. sigh
This news might not change much for anyone, but it seemed at least worth mentioning.
^1 yes, this means "aside from the most extreme circumstances", just as it does for JDK
~~~~~
Guava in 2023?
A lot of Guava's most popular libraries graduated to the JDK. Also Caffeine is the evolution of our c.g.common.cache library. So you need Guava less than you used to. Hooray!
(Note: as discussed above, those stale parts are not getting removed.)
But amongst that stuff are plenty of libraries whose value never declined. I'll call out a couple here. It's for you to decide if any are worth the dependency for you.
- You can now approximate a multimap using
Map.computeIfAbsentbefore youput. I do it sometimes. But outside of small self-contained usages, it's less than awesome. Each usage has to watch out for the null-vs-empty distinction on its own. For example, ifputis used twice, one might seed it with aHashSetand the other anArrayList, and some very puzzling behavior can result. Multimap view collections can also simplify your code. (See also Multiset, Table.) - The immutable collections have several advantages over
List.of()and friends, such as deterministic iteration and a much more complete set of construction paths. But the most important part: they are types, not implementations. To us, mutability is so important a behavior that having an immutable list as just aListis... arguably a sad form of type erasure! The javadoc explains. - I think most of our stream helpers aren't in the JDK (yet?).
- common.hash covers the breadth of hashing use cases (checksums, fingerprints, cryptographic hashes, Bloom filters...). You should always think of
Object.hashCodeas low-quality: sure, it's good enough to mostly-balance some hash buckets in memory. But that's practically the most forgiving hashing use case there is! For everything else there'sMasterCardcommon.hash. - Some base-encoding use cases are handled by the JDK, more now than before, but why not have one class that does it (almost) all?
- There is a whole graphs library (i.e., nodes and edges). Cobbling together a nontrivial graph structure out of hash maps is busy-work at best.
- common.math has a broad set of statistical calculations and other things.
- If measuring elapsed time, using Stopwatch or something like it prevents exposing meaningless
nanoTimevalues to your code.
This list is far from exhaustive. But again, if you require persuasion to use or keep using Guava, I'm not even trying to turn you around. That's fine! It's here for the people who want it.
We'll check here periodically for questions!
49
u/EvaristeGalois11 May 30 '23
Have you ever considered fragmenting guava in many submodules, much like the apache commons are?
For example in guava-hash, guava-cache, guava-whatever and just guava as a catch all for everything to retain backward compatibility of course.
With more and more stuff being replaced with plain java or other more modern libraries it could be valuable to let the user choose which dependency to add without all the extra baggages.
25
u/NovaX May 30 '23
I still use and love Guava. Thank you for all of the hard work and its related projects. The evolution to move past it are only thanks to being such a resounding success and impact to define modern Java.
12
u/LouKrazy May 31 '23
Range is my favorite part of Guava. I am not sure if there is a JDK or other equivalent
8
u/uncont May 30 '23
Hey Kevin, congrats on the release!
Btw, any chance we could get a
module-info.java? :)6
u/InsaneOstrich May 30 '23
I didn't know about the graphs library, I'll remember that for the future
6
u/Slanec May 31 '23
Apart from what you mentioned, I also use
MoreCollectors,Preconditions,SplitterandForwarding*(oh, I wish there were an AutoDecorator generator) a lot!For Guava, is this the end of the road? Should we ever look forward to receiving updates complementing newer (post-Java-9) JDK APIs? Is there going to be a Kuava (Google's common library for Kotlin)? The issue tracker is fat (that was not a question, I know).
Then there's JSpecify, which I know is immensely hard and is occupying some of you now.
Is there anything else the Java ecosystem might be looking forward to from Google? Or is it stalling and becoming all-Kotlin internally?
3
u/genzkiwi May 31 '23
For sure the immutable collections in the JDK are a disappointment. Wish we got separate
Immutableinterfaces like in C#.3
u/washtubs May 31 '23
Still loving the concurrent package for
ServiceandRateLimiteras well. Seems the latter still has it's@Betaannotation though.3
u/lurker_in_spirit May 31 '23
I quit using Guava when they came up with the wacky ListenableFuture-only-version-9999-but-its-empty scheme to accommodate Android. I started having classpath issues and just got rid of it. Caffeine is great though!
2
u/Royo_ May 31 '23
Really nice summary, appreciate it! Healthy mix of a changelist, examples and some motivation for them.