< prev index next >

test/jdk/java/util/Optional/BasicDouble.java

Print this page

        

@@ -30,10 +30,12 @@
  */
 
 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;
 
 public class BasicDouble {

@@ -49,10 +51,11 @@
         assertFalse(empty.equals(OptionalDouble.of(UNEXPECTED)));
         assertFalse(OptionalDouble.of(UNEXPECTED).equals(empty));
         assertFalse(empty.equals("unexpected"));
 
         assertFalse(empty.isPresent());
+        assertTrue(empty.isEmpty());
         assertEquals(empty.hashCode(), 0);
         assertEquals(empty.orElse(UNEXPECTED), UNEXPECTED);
         assertEquals(empty.orElseGet(() -> UNEXPECTED), UNEXPECTED);
 
         assertThrows(NoSuchElementException.class, () -> empty.getAsDouble());

@@ -124,6 +127,22 @@
 
     @Test(groups = "unit")
     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());
+    }
+
 }
< prev index next >