/** * Returns an {@code Optional} describing the specified value, if non-null, * otherwise returns an absent {@code Optional}. * * @param <T> the class of the value * @param value the possibly-null value to describe * @return an {@code Optional} with a present value if the specified value * is non-null, otherwise an absent {@code Optional} */ public static <T> Optional<T> ofNullable(T value) { return value == null ? (Optional<T>) absent() : of(value); }
@Test public void orThrow() throws Exception { try { Optional.ofNullable(null).orThrow(new Exception("An exception occurred.")); Optional.absent().orThrow(new Exception("An exception occurred.")); Assert.fail("Invoking orThrow() on an absent optional should throw an exception."); } catch (Exception anException) { // No-op. This is the expected behaviour. } }
@Test public void absent() throws Exception { Assert.assertFalse(Optional.absent().isPresent()); }
@Test public void orThrowWithSupplier() throws Exception { final Supplier<Exception> anExceptionSupplier = new Supplier<Exception>() { @Override public Exception get() { return new Exception("An exception occurred."); } }; try { Optional.ofNullable(null).orThrow(anExceptionSupplier); Optional.absent().orThrow(anExceptionSupplier); Assert.fail("Invoking orThrow() on an absent optional should throw an exception."); } catch (Exception anException) { // No-op. This is the expected behaviour. } }
@Test public void orNull() throws Exception { Assert.assertNull(Optional.ofNullable(null).orNull()); Assert.assertNull(Optional.absent().orNull()); }
@Test public void orWithSupplier() throws Exception { final Supplier<String> aStringSupplier = new Supplier<String>() { @Override public String get() { return "42"; } }; Assert.assertEquals("42", Optional.<String>ofNullable(null).or(aStringSupplier)); Assert.assertEquals("42", Optional.<String>absent().or(aStringSupplier)); }
@Test public void or() throws Exception { Assert.assertEquals("42", Optional.ofNullable(null).or("42")); Assert.assertEquals("42", Optional.absent().or("42")); }
@Test public void ifPresent() throws Exception { Optional.ofNullable("42").ifPresent(new Consumer<String>() { @Override public void consume(String value) { Assert.assertEquals("42", value); } }); Optional.ofNullable(null).ifPresent(new Consumer<Object>() { @Override public void consume(Object value) { Assert.fail("ifPresent() on an absent optional should never call its consumer"); } }); Optional.absent().ifPresent(new Consumer<Object>() { @Override public void consume(Object value) { Assert.fail("ifPresent() on an absent optional should never call its consumer"); } }); }
Optional.absent().ifPresentOrElse(new Consumer<Object>() { @Override public void consume(Object value) {
@Test public void get() throws Exception { Assert.assertEquals("42", Optional.of("42").get()); Assert.assertEquals("42", Optional.ofNullable("42").get()); try { Optional.ofNullable(null).get(); Optional.absent().get(); Assert.fail("Invoking get() on an absent optional should raise an exception."); } catch (IllegalStateException expected) { // No-op. This is the expected behaviour. } }
@Test public void isPresent() throws Exception { Assert.assertTrue(Optional.of("42").isPresent()); Assert.assertTrue(Optional.ofNullable("42").isPresent()); Assert.assertFalse(Optional.ofNullable(null).isPresent()); Assert.assertFalse(Optional.absent().isPresent()); }