mkodekar / guava-libraries

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

Missing use case: transforming Optional to Optional #1799

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have a use case where I am transforming an `Optional`, transforming it into 
the value of a member variable. The issue: The member variable can be null so 
I'd like to convert it to an Optional if it exists.

Proposal:

class Optional<T> ... {
    ...

    public Optional<F> Optional#transformToOptional(Function<T, F> transformer) {
        try {
            return this.transform(transformer);
        catch (NullPointerException e) {
            return Optional.<F>.absent()
        }
    }
}
(which reservations to typos and probably not catching NullPointerException in 
real implementation, instead checking for null)

Original issue reported on code.google.com by jens.ran...@tink.se on 9 Jul 2014 at 8:50

GoogleCodeExporter commented 9 years ago
I've reported similar issue in October 2012 and cannot wait for resolution, see 
https://code.google.com/p/guava-libraries/issues/detail?id=1171 (the cause of 
NPE can be arbitrary so I feel weird of globally catching NPE, instead I 
suggested transforming null result to Optional which eventually showed to be 
consistent with JDK8).

Original comment by tomas.za...@gmail.com on 9 Jul 2014 at 9:17

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

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

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

GoogleCodeExporter commented 9 years ago

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

GoogleCodeExporter commented 9 years ago

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