--- old/src/java.base/share/classes/java/util/Optional.java 2018-04-16 10:24:15.222792015 -0700 +++ new/src/java.base/share/classes/java/util/Optional.java 2018-04-16 10:24:14.944766419 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -160,6 +160,16 @@ } /** + * If a value is not present, returns {@code true}, otherwise + * {@code false}. + * + * @return {@code true} if a value is not present, otherwise {@code false} + */ + public boolean isEmpty() { + return value == null; + } + + /** * If a value is present, performs the given action with the value, * otherwise does nothing. * --- old/src/java.base/share/classes/java/util/OptionalDouble.java 2018-04-16 10:24:15.867851402 -0700 +++ new/src/java.base/share/classes/java/util/OptionalDouble.java 2018-04-16 10:24:15.550822215 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -140,6 +140,17 @@ } /** + * If a value is not present, returns {@code true}, otherwise + * {@code false}. + * + * @return {@code true} if a value is not present, otherwise {@code false} + */ + public boolean isEmpty() { + return !isPresent; + } + + + /** * If a value is present, performs the given action with the value, * otherwise does nothing. * --- old/src/java.base/share/classes/java/util/OptionalInt.java 2018-04-16 10:24:16.452905265 -0700 +++ new/src/java.base/share/classes/java/util/OptionalInt.java 2018-04-16 10:24:16.160878380 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -140,6 +140,16 @@ } /** + * If a value is not present, returns {@code true}, otherwise + * {@code false}. + * + * @return {@code true} if a value is not present, otherwise {@code false} + */ + public boolean isEmpty() { + return !isPresent; + } + + /** * If a value is present, performs the given action with the value, * otherwise does nothing. * --- old/src/java.base/share/classes/java/util/OptionalLong.java 2018-04-16 10:24:17.098964745 -0700 +++ new/src/java.base/share/classes/java/util/OptionalLong.java 2018-04-16 10:24:16.790936386 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -140,6 +140,16 @@ } /** + * If a value is not present, returns {@code true}, otherwise + * {@code false}. + * + * @return {@code true} if a value is not present, otherwise {@code false} + */ + public boolean isEmpty() { + return !isPresent; + } + + /** * If a value is present, performs the given action with the value, * otherwise does nothing. * --- old/test/jdk/java/util/Optional/Basic.java 2018-04-16 10:24:17.741023856 -0700 +++ new/test/jdk/java/util/Optional/Basic.java 2018-04-16 10:24:17.431995405 -0700 @@ -33,6 +33,7 @@ import java.util.NoSuchElementException; import java.util.Optional; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Predicate; import static java.util.stream.Collectors.toList; @@ -52,6 +53,7 @@ assertFalse(empty.equals("unexpected")); assertFalse(empty.isPresent()); + assertTrue(empty.isEmpty()); assertEquals(empty.hashCode(), 0); assertEquals(empty.orElse("x"), "x"); assertEquals(empty.orElseGet(() -> "y"), "y"); @@ -200,4 +202,15 @@ public void testStreamPresent() { assertEquals(Optional.of("xyzzy").stream().collect(toList()), List.of("xyzzy")); } + + @Test(groups = "unit") + public void testIsEmpty() { + var integerList = List.of(1, 2, 3, 4, 5); + Predicate isPositiveNumber = x -> x > 0; + Predicate isNegativeNumber = x -> x < 0; + Optional positiveNumber = integerList.stream().filter(isPositiveNumber).findAny(); + Optional negativeNumber = integerList.stream().filter(isNegativeNumber).findAny(); + assertFalse(positiveNumber.isEmpty()); + assertTrue(negativeNumber.isEmpty()); + } } --- old/test/jdk/java/util/Optional/BasicDouble.java 2018-04-16 10:24:18.316076798 -0700 +++ new/test/jdk/java/util/Optional/BasicDouble.java 2018-04-16 10:24:18.032050649 -0700 @@ -32,6 +32,8 @@ import java.util.NoSuchElementException; import java.util.OptionalDouble; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.DoublePredicate; +import java.util.List; import static org.testng.Assert.*; import org.testng.annotations.Test; @@ -51,6 +53,7 @@ assertFalse(empty.equals("unexpected")); assertFalse(empty.isPresent()); + assertTrue(empty.isEmpty()); assertEquals(empty.hashCode(), 0); assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED); assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED); @@ -126,4 +129,20 @@ public void testStreamPresent() { assertEquals(OptionalDouble.of(DOUBLEVAL).stream().toArray(), new double[] { DOUBLEVAL }); } + + @Test(groups = "unit") + public void testIsEmpty() { + OptionalDouble empty = OptionalDouble.empty(); + assertTrue(empty.isEmpty()); + OptionalDouble present = OptionalDouble.of(DOUBLEVAL); + assertFalse(present.isEmpty()); + var doubleList = List.of(1.0, 2.0, 3.0, 4.0, 5.0); + DoublePredicate isPositiveNumber = x -> x > 0; + DoublePredicate isNegativeNumber = x -> x < 0; + OptionalDouble positiveNumber = doubleList.stream().mapToDouble(Double::doubleValue).filter(isPositiveNumber).findAny(); + OptionalDouble negativeNumber = doubleList.stream().mapToDouble(Double::doubleValue).filter(isNegativeNumber).findAny(); + assertFalse(positiveNumber.isEmpty()); + assertTrue(negativeNumber.isEmpty()); + } + } --- old/test/jdk/java/util/Optional/BasicInt.java 2018-04-16 10:24:18.916132042 -0700 +++ new/test/jdk/java/util/Optional/BasicInt.java 2018-04-16 10:24:18.638106446 -0700 @@ -32,6 +32,8 @@ import java.util.NoSuchElementException; import java.util.OptionalInt; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.IntPredicate; +import java.util.List; import static org.testng.Assert.*; import org.testng.annotations.Test; @@ -52,6 +54,8 @@ assertFalse(empty.equals("unexpected")); assertFalse(empty.isPresent()); + assertTrue(empty.isEmpty()); + assertEquals(empty.hashCode(), 0); assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED); assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED); @@ -127,4 +131,20 @@ public void testStreamPresent() { assertEquals(OptionalInt.of(INTVAL).stream().toArray(), new int[] { INTVAL }); } + + @Test(groups = "unit") + public void testIsEmpty() { + OptionalInt empty = OptionalInt.empty(); + assertTrue(empty.isEmpty()); + OptionalInt present = OptionalInt.of(1); + assertFalse(present.isEmpty()); + var integerList = List.of(1, 2, 3, 4, 5); + IntPredicate isPositiveNumber = x -> x > 0; + IntPredicate isNegativeNumber = x -> x < 0; + OptionalInt positiveNumber = integerList.stream().mapToInt(Integer::intValue).filter(isPositiveNumber).findAny(); + OptionalInt negativeNumber = integerList.stream().mapToInt(Integer::intValue).filter(isNegativeNumber).findAny(); + assertFalse(positiveNumber.isEmpty()); + assertTrue(negativeNumber.isEmpty()); + } + } --- old/test/jdk/java/util/Optional/BasicLong.java 2018-04-16 10:24:19.547190140 -0700 +++ new/test/jdk/java/util/Optional/BasicLong.java 2018-04-16 10:24:19.222160216 -0700 @@ -31,7 +31,9 @@ import java.util.NoSuchElementException; import java.util.OptionalLong; +import java.util.function.LongPredicate; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.List; import static org.testng.Assert.*; import org.testng.annotations.Test; @@ -51,6 +53,7 @@ assertFalse(empty.equals("unexpected")); assertFalse(empty.isPresent()); + assertTrue(empty.isEmpty()); assertEquals(empty.hashCode(), 0); assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED); assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED); @@ -126,4 +129,20 @@ public void testStreamPresent() { assertEquals(OptionalLong.of(LONGVAL).stream().toArray(), new long[] { LONGVAL }); } + + @Test(groups = "unit") + public void testIsEmpty() { + OptionalLong empty = OptionalLong.empty(); + assertTrue(empty.isEmpty()); + OptionalLong present = OptionalLong.of(LONGVAL); + assertFalse(present.isEmpty()); + var longList = List.of(1L, 2L, 3L, 4L, 5L); + LongPredicate isPositiveNumber = x -> x > 0; + LongPredicate isNegativeNumber = x -> x < 0; + OptionalLong positiveNumber = longList.stream().mapToLong(Long::longValue).filter(isPositiveNumber).findAny(); + OptionalLong negativeNumber = longList.stream().mapToLong(Long::longValue).filter(isNegativeNumber).findAny(); + assertFalse(positiveNumber.isEmpty()); + assertTrue(negativeNumber.isEmpty()); + } + }