dnrajugade / guava-libraries

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

Optional.transformToNullable #1171

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have an instance which is to be passed through chain of Optional.transform 
methods. But functions may return null and hence cannot be used in 
Optional.transform:

Optional.of(obj).transform(function1).transform(function2).orNull();

As Optional provides method fromNullable for construction from null, it could 
similarly wrap null-returning legacy functions:

  class Present
  ...
  @Override public <V> Optional<V> transformToNullable(Function<? super T, V> function) {
    V functionResult = function.apply(reference);
    return functionResult == null ? Absent.INSTANCE : new Present<V>(functionResult);
  }

Until now, following 2 workarounds can be used but first one scales source text 
badly for long chains and second one is awful abuse of iterables.

V result = null;
V1 result1 = function1.apply(obj);
if (result1 != null) {
    result = function2.apply(result1);
}

V result = from(singleton(obj))
    .transform(function1).filter(notNull())
    .transform(function2).filter(notNull())
    .first().orNull();

Thanks!

Original issue reported on code.google.com by tomas.za...@gmail.com on 16 Oct 2012 at 2:03

GoogleCodeExporter commented 9 years ago
More workarounds potentially worth mentioning:

Optional.fromNullable(function.apply(value.orNull());

Alternately, users could just write a simple transformNullable(Optional, 
Function) method themselves.

Should we maybe try to search within Google for potential users?

Original comment by lowas...@google.com on 16 Oct 2012 at 3:51

GoogleCodeExporter commented 9 years ago

Original comment by kak@google.com on 22 Aug 2013 at 10:50

GoogleCodeExporter commented 9 years ago
Just note: in JDK8's Optional.map the transformation of null result into 
empty() is automatic. 
See 
http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/43386cc9a017/src/share/classes/jav
a/util/Optional.java line 215
IMHO accepting issue would make Guava usage easier for those who cannot move to 
JDK8 now yet.

Original comment by tomas.za...@intelis.cz on 13 Feb 2014 at 9:02

GoogleCodeExporter commented 9 years ago
In reply to lowas/#1: The issue with your proposal is that the function then 
needs to take into account getting a null value, which is a little annoying.

Original comment by jens.ran...@tink.se on 9 Jul 2014 at 12:36

GoogleCodeExporter commented 9 years ago
In what way?  The above implementation (properly) just throws an NPE.

Original comment by lowas...@google.com on 9 Jul 2014 at 4:19

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:13

GoogleCodeExporter commented 9 years ago

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

GoogleCodeExporter commented 9 years ago

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