aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkevinb <kevinb@google.com>2016-12-02 16:07:11 -0800
committerChris Povirk <cpovirk@google.com>2016-12-05 14:16:01 -0800
commit15b547abb7015069bb464833a72f433d82b83829 (patch)
tree914fdae5226dade01ae8cb2a1564ffa1f61e1bf4
parent397d2a62c4cdbf37e22b303acc7800b14a3c4534 (diff)
downloadguava-15b547abb7015069bb464833a72f433d82b83829.tar.gz
Add toJavaUtil instance method, which is handier and also doesn't have to be
marked @Nullable. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=140905873
-rw-r--r--guava-gwt/test/com/google/common/base/OptionalTest_gwt.java9
-rw-r--r--guava-tests/test/com/google/common/base/OptionalTest.java7
-rw-r--r--guava/src/com/google/common/base/Optional.java28
3 files changed, 38 insertions, 6 deletions
diff --git a/guava-gwt/test/com/google/common/base/OptionalTest_gwt.java b/guava-gwt/test/com/google/common/base/OptionalTest_gwt.java
index 1bebbb9cc..e8bdb1a13 100644
--- a/guava-gwt/test/com/google/common/base/OptionalTest_gwt.java
+++ b/guava-gwt/test/com/google/common/base/OptionalTest_gwt.java
@@ -193,9 +193,14 @@ public void testSampleCodeFine2() throws Exception {
testCase.testSampleCodeFine2();
}
-public void testToJavaUtil() throws Exception {
+public void testToJavaUtil_instance() throws Exception {
com.google.common.base.OptionalTest testCase = new com.google.common.base.OptionalTest();
- testCase.testToJavaUtil();
+ testCase.testToJavaUtil_instance();
+}
+
+public void testToJavaUtil_static() throws Exception {
+ com.google.common.base.OptionalTest testCase = new com.google.common.base.OptionalTest();
+ testCase.testToJavaUtil_static();
}
public void testToString_absent() throws Exception {
diff --git a/guava-tests/test/com/google/common/base/OptionalTest.java b/guava-tests/test/com/google/common/base/OptionalTest.java
index 007841fbb..8f7de2110 100644
--- a/guava-tests/test/com/google/common/base/OptionalTest.java
+++ b/guava-tests/test/com/google/common/base/OptionalTest.java
@@ -36,12 +36,17 @@ import junit.framework.TestCase;
*/
@GwtCompatible(emulated = true)
public final class OptionalTest extends TestCase {
- public void testToJavaUtil() {
+ public void testToJavaUtil_static() {
assertNull(Optional.toJavaUtil(null));
assertEquals(java.util.Optional.empty(), Optional.toJavaUtil(Optional.absent()));
assertEquals(java.util.Optional.of("abc"), Optional.toJavaUtil(Optional.of("abc")));
}
+ public void testToJavaUtil_instance() {
+ assertEquals(java.util.Optional.empty(), Optional.absent().toJavaUtil());
+ assertEquals(java.util.Optional.of("abc"), Optional.of("abc").toJavaUtil());
+ }
+
public void testFromJavaUtil() {
assertNull(Optional.fromJavaUtil(null));
assertEquals(Optional.absent(), Optional.fromJavaUtil(java.util.Optional.empty()));
diff --git a/guava/src/com/google/common/base/Optional.java b/guava/src/com/google/common/base/Optional.java
index 6b18e959d..96d82baa9 100644
--- a/guava/src/com/google/common/base/Optional.java
+++ b/guava/src/com/google/common/base/Optional.java
@@ -121,6 +121,8 @@ public abstract class Optional<T> implements Serializable {
/**
* Returns the equivalent {@code com.google.common.base.Optional} value to the given {@code
* java.util.Optional}, or {@code null} if the argument is null.
+ *
+ * @since 21.0
*/
@Nullable
public static <T> Optional<T> fromJavaUtil(@Nullable java.util.Optional<T> javaUtilOptional) {
@@ -130,12 +132,19 @@ public abstract class Optional<T> implements Serializable {
/**
* Returns the equivalent {@code java.util.Optional} value to the given {@code
* com.google.common.base.Optional}, or {@code null} if the argument is null.
+ *
+ * <p>If {@code googleOptional} is known to be non-null, use {@code googleOptional.toJavaUtil()}
+ * instead.
+ *
+ * <p>Unfortunately, the method reference {@code Optional::toJavaUtil} will not work, because it
+ * could refer to either the static or instance version of this method. Write out the lambda
+ * expression {@code o -> Optional.toJavaUtil(o)} instead.
+ *
+ * @since 21.0
*/
@Nullable
public static <T> java.util.Optional<T> toJavaUtil(@Nullable Optional<T> googleOptional) {
- return (googleOptional == null)
- ? null
- : java.util.Optional.ofNullable(googleOptional.orNull());
+ return googleOptional == null ? null : googleOptional.toJavaUtil();
}
Optional() {}
@@ -263,6 +272,19 @@ public abstract class Optional<T> implements Serializable {
public abstract <V> Optional<V> transform(Function<? super T, V> function);
/**
+ * Returns the equivalent {@code java.util.Optional} value to this optional.
+ *
+ * <p>Unfortunately, the method reference {@code Optional::toJavaUtil} will not work, because it
+ * could refer to either the static or instance version of this method. Write out the lambda
+ * expression {@code o -> o.toJavaUtil()} instead.
+ *
+ * @since 21.0
+ */
+ public java.util.Optional<T> toJavaUtil() {
+ return java.util.Optional.ofNullable(orNull());
+ }
+
+ /**
* Returns {@code true} if {@code object} is an {@code Optional} instance, and either the
* contained references are {@linkplain Object#equals equal} to each other or both are absent.
* Note that {@code Optional} instances of differing parameterized types can be equal.