/** * Return the contained value, if present, otherwise null. * * @return the present value, if present, otherwise null */ public T orNull() { return isPresent() ? value : null; }
/** * Returns the hash code value of the present value, if any, or 0 (zero) if * no value is present. * * @return hash code value of the present value or 0 if no value is present */ @Override public int hashCode() { return isPresent() ? value.hashCode() : 0; }
/** * Returns a non-empty string representation of this Optional suitable for * debugging. The exact presentation format is unspecified and may vary * between implementations and versions. * * @return the string representation of this instance */ @Override public String toString() { return isPresent() ? String.format("Optional[%s]", value) : "Optional.ABSENT"; } }
/** * Return the value if present, otherwise return {@code other}. * * @param other the value to be returned if there is no value present, may not * be null * @return the value, if present, otherwise {@code other} * @throws IllegalArgumentException if {@code other} is null */ public T or(T other) { if (other == null) { throw new IllegalArgumentException("null may not be passed as an argument; use orNull() instead."); } return isPresent() ? value : other; }
/** * If a value is present in this {@code Optional}, returns the value, * otherwise throws {@code IllegalStateException}. * * @return the non-null value held by this {@code Optional} * @throws IllegalStateException if there is no value present */ public T get() { if (isPresent()) { return value; } throw new IllegalStateException("Value is absent."); }
/** * Return the contained value, if present, otherwise throw an exception * instance provided as the parameter. * * @param <X> Type of the exception to be thrown * @param throwable The throwable instance to be thrown * @return the present value * @throws X if there is no value present * @throws IllegalArgumentException if {@code throwable} is null */ public <X extends Throwable> T orThrow(X throwable) throws X { if (throwable == null) { throw new IllegalArgumentException("null may not be passed as an argument; use orNull() instead."); } if (isPresent()) { return value; } throw throwable; }
/** * If a value is present, invoke the specified consumer with the value, * otherwise do nothing. * * @param consumer block to be executed if a value is present */ public void ifPresent(Consumer<T> consumer) { if (isPresent()) { consumer.consume(value); } }
/** * Return the value if present, otherwise invoke {@code otherSupplier} and return * the result of that invocation. * * @param otherSupplier a {@code Supplier} whose result is returned if no value * is present * @return the value if present otherwise the result of {@code otherSupplier.get()} * @throws IllegalArgumentException if {@code otherSupplier} is null */ public T or(Supplier<T> otherSupplier) { if (otherSupplier == null) { throw new IllegalArgumentException("null may not be passed as an argument; use orNull() instead."); } return isPresent() ? value : otherSupplier.get(); }
/** * Return the contained value, if present, otherwise throw an exception * to be created by the provided supplier. * * @param <X> Type of the exception to be thrown * @param throwableSupplier a {@code Supplier} which will return the exception to * be thrown * @return the present value * @throws X if there is no value present * @throws IllegalArgumentException if {@code throwableSupplier} is null */ public <X extends Throwable> T orThrow(Supplier<? extends X> throwableSupplier) throws X { if (throwableSupplier == null) { throw new IllegalArgumentException("null may not be passed as an argument; use orNull() instead."); } if (isPresent()) { return value; } throw throwableSupplier.get(); }
/** * If a value is present, invoke the specified consumer with the value, * otherwise invoke the function passed as the second parameter. * * @param consumer block to be executed if a value is present * @param function block to be executed if a value is absent */ public void ifPresentOrElse(Consumer<T> consumer, Function function) { if (isPresent()) { consumer.consume(value); } else { function.call(); } }
@Test public void absent() throws Exception { Assert.assertFalse(Optional.absent().isPresent()); }
@Test public void of() throws Exception { Assert.assertTrue(Optional.of("42").isPresent()); try { Optional.of(null); Assert.fail("Invoking of() with null argument should raise an exception."); } catch (IllegalArgumentException expected) { // No-op. This is the expected behaviour. } }