< prev index next >

test/jdk/java/util/Optional/BasicInt.java

Print this page

        

*** 30,39 **** --- 30,41 ---- */ 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; public class BasicInt {
*** 50,59 **** --- 52,63 ---- assertFalse(empty.equals(OptionalInt.of(UNEXPECTED))); assertFalse(OptionalInt.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.getAsInt());
*** 125,130 **** --- 129,150 ---- @Test(groups = "unit") 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()); + } + }
< prev index next >