/** * 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 byte or(byte other) { return isPresent() ? value : other; }
/** * Returns a non-empty string representation of this OptionalByte 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("OptionalByte[%s]", value) : "OptionalByte.ABSENT"; } }
/** * If a value is present in this {@code OptionalByte}, returns the value, * otherwise throws {@code IllegalStateException}. * * @return the value held by this {@code OptionalByte} * @throws IllegalStateException if there is no value present */ public byte 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> byte 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(ByteConsumer 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 Byte(value).hashCode() : 0; }
/** * Indicates whether some other object is "equal to" this Optional. The * other object is considered equal if: * <ul> * <li>it is also an {@code OptionalByte} 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 OptionalByte)) { return false; } final OptionalByte other = (OptionalByte) object; return (isPresent() && other.isPresent()) ? get() == other.get() : isPresent() == other.isPresent(); }
/** * 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 byte or(ByteSupplier 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> byte 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(); }
/** * 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(ByteConsumer consumer, Function function) { if (isPresent()) { consumer.consume(value); } else { function.call(); } }
@Test public void of() throws Exception { Assert.assertTrue(OptionalByte.of(((byte) 42)).isPresent()); }
@Test public void absent() throws Exception { Assert.assertFalse(OptionalByte.absent().isPresent()); }