@Test public void of() throws Exception { Assert.assertTrue(OptionalFloat.of(42f).isPresent()); }
@Test public void ifPresent() throws Exception { OptionalFloat.of(42f).ifPresent(new FloatConsumer() { @Override public void consume(float value) { Assert.assertEquals(42f, value, 0); } }); OptionalFloat.absent().ifPresent(new FloatConsumer() { @Override public void consume(float value) { Assert.fail("ifPresent() on an absent optional should never call its consumer"); } }); }
@Test public void isPresent() throws Exception { Assert.assertTrue(OptionalFloat.of(42f).isPresent()); Assert.assertFalse(OptionalFloat.absent().isPresent()); }
@Test public void ifPresentOrElse() throws Exception { OptionalFloat.of(42f).ifPresentOrElse(new FloatConsumer() { @Override public void consume(float value) { Assert.assertEquals(42f, value, 0); } }, new Function() { @Override public void call() { Assert.fail("ifPresent() on an non-absent optional should call its consumer"); } }); OptionalFloat.absent().ifPresentOrElse(new FloatConsumer() { @Override public void consume(float 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 get() throws Exception { Assert.assertEquals(42f, OptionalFloat.of(42f).get(), 0); try { OptionalFloat.absent().get(); Assert.fail("Invoking get() on an absent optional should raise an exception."); } catch (IllegalStateException expected) { // No-op. This is the expected behaviour. } }