migrator / guava-libraries-2

Guava: Google Core Libraries for Java 1.6+
0 stars 0 forks source link

Missing use case: transforming Optional to Optional #31

Open migrator opened 10 years ago

migrator commented 10 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 ... { ...

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)

relevance: 1

migrator commented 10 years ago

summary: Not Defined

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).

status Not Defined creator: tomas.za...@gmail.com created at: Jul 9, 2014