@Test public void of() throws Exception { Assert.assertTrue(OptionalDouble.of(42d).isPresent()); }
@Test public void ifPresent() throws Exception { OptionalDouble.of(42d).ifPresent(new DoubleConsumer() { @Override public void consume(double value) { Assert.assertEquals(42d, value, 0); } }); OptionalDouble.absent().ifPresent(new DoubleConsumer() { @Override public void consume(double value) { Assert.fail("ifPresent() on an absent optional should never call its consumer"); } }); }
@Test public void ifPresentOrElse() throws Exception { OptionalDouble.of(42d).ifPresentOrElse(new DoubleConsumer() { @Override public void consume(double value) { Assert.assertEquals(42d, value, 0); } }, new Function() { @Override public void call() { Assert.fail("ifPresent() on an non-absent optional should call its consumer"); } }); OptionalDouble.absent().ifPresentOrElse(new DoubleConsumer() { @Override public void consume(double value) { Assert.fail("ifPresent() on an absent optional should never call its consumer"); } }, new Function() { @Override public void call() { Assert.assertTrue(true); // No-op. This is the expected behaviour. } }); } }
@Test public void isPresent() throws Exception { Assert.assertTrue(OptionalDouble.of(42d).isPresent()); Assert.assertFalse(OptionalDouble.absent().isPresent()); }
@Test public void get() throws Exception { Assert.assertEquals(42d, OptionalDouble.of(42d).get(), 0); try { OptionalDouble.absent().get(); Assert.fail("Invoking get() on an absent optional should raise an exception."); } catch (IllegalStateException expected) { // No-op. This is the expected behaviour. } }