Optional.isEmpty() Coming to Java?
Optional.isEmpty() Coming to Java?
Is it really needed? Let's take a look at the justification provided and see if this method is coming to Java any time soon.
Join the DZone community and get the full member experience.Join For Free
Learn how to build stream processing applications in Java-includes reference application. Brought to you in partnership with Hazelcast.
JDK-8184693 requests that the method
isEmpty() be added to the Optional class introduced with JDK 8. Invoking
Optional.isEmpty() would be the equivalent of invoking . There is no JDK release currently associated with JDK-8184693, but it is being actively worked as demonstrated in a recent core-libs-dev mailing list post titled " RFR: 8184693: (opt) add Optional.isEmpty".
Written by Stuart Marks in July 2017, JDK-8184693 provides some interesting justification for the addition of
Optional.isEmpty(). Marks points out that "generally we avoid adding methods that are simple inverses of each other" and cites as examples presence of String.isEmpty() and Collection.isEmpty() without any accompanying
Collection.nonEmpty() counterparts. Marks writes this approach works well in these cases because "emptiness/non-emptiness isn't fundamental" for them: "For these objects, it's perfectly reasonable to operate on an empty String (e.g., searching or appending it) or collection (e.g., iterating over it)."
In JDK-8184693, Marks writes of examples that do have methods to explicitly express both emptiness and non-emptiness. He writes, "However, with references, null/non-null is pretty fundamental, we have Objects.isNull and Objects.nonNull." Because these examples' usages are more like
Optional's usages, Marks argues that
Optional should have an
isEmpty() method alongside its current
isPresent() method: "Similarly with
Optional, the empty/present dichotomy is quite fundamental, so there should be
isEmpty alongside of
Most of the justification text in JDK-8184693 was added this month (April 2018) and includes a link to the April 2017 core-lib-devs mailing list post "Optional.isEmpty()" by Peter Levart. The bug write-up summarizes some of the discussion started by this post. Messages in that thread include those that provide humor, reference bikeshedding, list "plenty of one-liners that don't use boolean negation," recommend name isNotPresent() or isAbsent(), provide enthusiastic support of the idea of
Optional.isEmpty(), and remind that "the bar for adding methods to Optional is set very high."
The previously mentioned mailing list message "RFR: 8184693: (opt) add Optional.isEmpty" references code available for review. The "Sdiff" of
Optional.java for this proposed change shows that this method has been implemented. However, a similar change still needs to be made for OptionalDouble, OptionalLong, and OptionalInt.
As I've used
Optional in my Java code, I've come to appreciate times when I don't need to use
Optional.isPresent(). However, there are times when there's no good way around it and I look forward to the addition of
Optional.isEmpty() to replace the use of
!Optional.isPresent(). The addition of
Optional.isEmpty() is a minor thing, but I believe it will make my code more readable and more fluent. I look forward to it coming soon to a JDK near me.
Published at DZone with permission of Dustin Marx , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.