/** * Indicates whether some other object is "equal to" this Optional. The * other object is considered equal if: * <ul> * <li>it is also an {@code OptionalShort} and; * <li>both instances have no value present or; * <li>the present values are "equal to" each other via the "{@code ==}" * operator. * </ul> * * @param object an object to be tested for equality * @return {code true} if the other object is "equal to" this object * otherwise {@code false} */ @Override public boolean equals(Object object) { if (this == object) { return true; } if (!(object instanceof OptionalShort)) { return false; } final OptionalShort other = (OptionalShort) object; return (isPresent() && other.isPresent()) ? get() == other.get() : isPresent() == other.isPresent(); }
/** * Returns an {@code OptionalShort} with the specified present value. * * @param value the value to be present * @return an {@code OptionalShort} with the value present */ public static OptionalShort of(short value) { return new OptionalShort(value); }
/** * Return the value if present, otherwise return {@code other}. * * @param other the value to be returned if there is no value present * @return the value, if present, otherwise {@code other} */ public short or(short other) { return isPresent() ? value : other; }
@Test public void get() throws Exception { Assert.assertEquals(((short) 42), OptionalShort.of(((short) 42)).get()); try { OptionalShort.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 ifPresent() throws Exception { OptionalShort.of(((short) 42)).ifPresent(new ShortConsumer() { @Override public void consume(short value) { Assert.assertEquals(((short) 42), value); } }); OptionalShort.absent().ifPresent(new ShortConsumer() { @Override public void consume(short value) { Assert.fail("ifPresent() on an absent optional should never call its consumer"); } }); }
@Test public void ifPresentOrElse() throws Exception { OptionalShort.of(((short) 42)).ifPresentOrElse(new ShortConsumer() { @Override public void consume(short value) { Assert.assertEquals(((short) 42), value); } }, new Function() { @Override public void call() { Assert.fail("ifPresent() on an non-absent optional should call its consumer"); } }); OptionalShort.absent().ifPresentOrElse(new ShortConsumer() { @Override public void consume(short 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. } }); } }
/** * Returns a non-empty string representation of this OptionalShort 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("OptionalShort[%s]", value) : "OptionalShort.ABSENT"; } }
@Test public void orThrowWithSupplier() throws Exception { final Supplier<Exception> anExceptionSupplier = new Supplier<Exception>() { @Override public Exception get() { return new Exception("An exception occurred."); } }; try { OptionalShort.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. } }
/** * If a value is present in this {@code OptionalShort}, returns the value, * otherwise throws {@code IllegalStateException}. * * @return the value held by this {@code OptionalShort} * @throws IllegalStateException if there is no value present */ public short 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> short orThrow(X throwable) throws X { if (throwable == null) { throw new IllegalArgumentException("null may not be passed as an argument."); } 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(ShortConsumer consumer) { if (isPresent()) { consumer.consume(value); } }
/** * 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() ? new Short(value).hashCode() : 0; }
/** * 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 short or(ShortSupplier otherSupplier) { if (otherSupplier == null) { throw new IllegalArgumentException("null may not be passed as an argument."); } 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> short orThrow(Supplier<? extends X> throwableSupplier) throws X { if (throwableSupplier == null) { throw new IllegalArgumentException("null may not be passed as an argument."); } if (isPresent()) { return value; } throw throwableSupplier.get(); }