yangxu998 / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Documentation: Objects#firstNonNull should mention Optional #685

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Suppose I have a line like the following:

Objects.firstNonNull(expensiveOperationThatIsUsuallyNonnull(), 
evenMoreExpensiveOperationThatRarelyNeedsToExecute())

What I would like to be able to do is wrap the second argument in a supplier so 
that it only executes if the first is null. That is, something like

    public static <T> T firstNonNull(@Nullable T first, @Nullable Supplier<? extends T> second) {
        T result = null;

        if (first == null) {
            result = first;
        } else if (second != null) {
            result = second.get();
        }

        return result;
    }

This would have behavior more like

Object result1 = expensiveOperationThatIsUsuallyNonnull();
if (result1 != null) {
    return result1;
} else {
    return evenMoreExpensiveOperationThatRarelyNeedsToExecute();
}

The current implementation is more like doing this:

Object result1 = expensiveOperationThatIsUsuallyNonnull();
Object result2 = evenMoreExpensiveOperationThatRarelyNeedsToExecute();
if (result1 != null) {
    return result1;
} else {
    return result2;
}

Original issue reported on code.google.com by raymond....@gmail.com on 11 Aug 2011 at 2:27

GoogleCodeExporter commented 9 years ago
How about using Optional?

Object result = Optional.fromNullable(expensiveOperationThatIsUsuallyNonnull())
    .or(evenMoreExpensiveOperationThatRarelyNeedsToExecute())

Original comment by kurt.kluever on 11 Aug 2011 at 3:01

GoogleCodeExporter commented 9 years ago
Ah, that is even better. I'm not particularly familiar with optionals yet, but 
I'm looking forward to that being released. I see that the `of` method takes a 
supplier, so that would cover my use case. This issue can be closed. Thanks, 
Kurt :)

Original comment by raymond....@gmail.com on 11 Aug 2011 at 3:14

GoogleCodeExporter commented 9 years ago
Rather, the `or` method.

Original comment by raymond....@gmail.com on 11 Aug 2011 at 3:15

GoogleCodeExporter commented 9 years ago
firstNonNull should mention Optional in its doc!

Original comment by kevin...@gmail.com on 11 Aug 2011 at 3:26

GoogleCodeExporter commented 9 years ago

Original comment by kurt.kluever on 11 Aug 2011 at 4:42

GoogleCodeExporter commented 9 years ago
> Object result = Optional
>   .fromNullable(expensiveOperationThatIsUsuallyNonnull())
>   .or(evenMoreExpensiveOperationThatRarelyNeedsToExecute())

This only makes sense if the method to calculate the alternative value is 
executed lazily, which is why I requested `Optional.or(Supplier)` in the first 
place.

Original comment by j...@nwsnet.de on 16 Aug 2011 at 7:45

GoogleCodeExporter commented 9 years ago
Well you'll be happy to hear that I added Optional.or(Supplier) a couple weeks 
ago!
http://code.google.com/p/guava-libraries/source/detail?spec=svn643&r=593

Original comment by kurt.kluever on 16 Aug 2011 at 1:20

GoogleCodeExporter commented 9 years ago
I've seen it already (and raymond, obviously, too), but thanks nevertheless :)

Original comment by j...@nwsnet.de on 16 Aug 2011 at 1:47

GoogleCodeExporter commented 9 years ago

Original comment by kurt.kluever on 16 Aug 2011 at 10:21

GoogleCodeExporter commented 9 years ago

Original comment by cpov...@google.com on 1 Sep 2011 at 9:20

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09