RFR 8071670: java.util.Optional: please add a way to specify if-else behavior

Louis Wasserman lowasser at google.com
Thu Feb 12 18:00:31 UTC 2015


How often does the case when you "have a lambda handy already" come up in
practice?  If this leads to people using this method instead of ifPresent,
that seems wasteful.

Guava's Optional succeeded as much for what it left out as what it had in
-- I confess this makes me nervous that j.u.Optional is becoming a kitchen
sink.

On Thu Feb 12 2015 at 9:51:23 AM Paul Sandoz <paul.sandoz at oracle.com> wrote:

>
> On Feb 12, 2015, at 3:50 PM, Daniel Fuchs <daniel.fuchs at oracle.com> wrote:
>
> > Hi Paul,
> >
> > This looks good - I have noticed one copy/paste error in the javadoc
> > though:
> >
> > OptionalInt.java:
> >
> > looks like the throws clause of ifPresent and ifPresentOrElse have been
> > interverted:
> >
> > 138      * @throws NullPointerException if a value is present and {@code
> action} is
> > 139      * null, or a value is not present and {@code emptyAction} is
> null.
> > 140      */
> > 141     public void ifPresent(IntConsumer action) {
> > 142         if (isPresent) {
> > 143             action.accept(value);
> > 144         }
> > 145     }
> > 146
> > 147     /**
> > 148      * If a value is present, perform the given action with the
> value,
> > 149      * otherwise perform the given empty-based action.
> > 150      *
> > 151      * @param action the action to be performed if a value is present
> > 152      * @param emptyAction the empty-based action to be performed if
> a value is
> > 153      * not present
> > 154      * @throws NullPointerException if value is present and {@code
> action} is
> > 155      * null
> > 156      * @since 1.9
> > 157      */
> > 158     public void ifPresentOrElse(IntConsumer action, Runnable
> emptyAction) {
> >
>
> Oops. Fixed.
>
>
> >
> > Concerning the test, should there be a test that verifies that NPE
> > is thrown when null is passed - as specified in the javadoc?
> >
>
> Done.
>
> http://cr.openjdk.java.net/~psandoz/jdk9/JDK-8071670-
> Optional-ifPresentOrElse/webrev/
>
> Thanks,
> Paul.
>



More information about the core-libs-dev mailing list