.0.9.1.source-code.OptionalShort Maven / Gradle / Ivy
Show all versions of util Show documentation
/* Copyright (c) 2020 LibJ
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* You should have received a copy of The MIT License (MIT) along with this
* program. If not, see .
*/
package org.libj.util;
import java.util.NoSuchElementException;
import java.util.function.Supplier;
import javax.annotation.Generated;
import org.libj.util.function.ShortConsumer;
import org.libj.util.function.ShortSupplier;
/**
* A container object which may or may not contain a {@code short} value. If a value is present, {@code isPresent()} returns
* {@code true}. If no value is present, the object is considered empty and {@code isPresent()} returns {@code false}.
*
* Additional methods that depend on the presence or absence of a contained value are provided, such as {@link #orElse(short)
* orElse()} (returns a default value if no value is present) and {@link #ifPresent(ShortConsumer) ifPresent()} (performs an action if
* a value is present).
*
* This is a value-based class; use of identity-sensitive operations (including reference equality ({@code ==}), identity hash code,
* or synchronization) on instances of {@code OptionalShort} may have unpredictable results and should be avoided.
*
* @implNote {@code OptionalShort} is primarily intended for use as a method return type where there is a clear need to represent "no
* result." A variable whose type is {@code OptionalShort} should never itself be {@code null}; it should always point to an
* {@code OptionalShort} instance.
*/
@Generated(value="org.openjax.codegen.template.Templates", date="2024-02-27T13:50:20.763")
public final class OptionalShort {
/** Common instance for {@code empty()}. */
private static final OptionalShort EMPTY = new OptionalShort();
/** If true then the value is present, otherwise indicates no value is present. */
private final boolean isPresent;
private final short value;
/**
* Construct an empty instance.
*
* @implNote Generally only one empty instance, {@link OptionalShort#EMPTY}, should exist per VM.
*/
private OptionalShort() {
this.isPresent = false;
this.value = 0;
}
/**
* Returns an empty {@link OptionalShort} instance. No value is present for this {@code OptionalShort}.
*
* @implNote Though it may be tempting to do so, avoid testing if an object is empty by comparing with {@code ==} against
* instances returned by {@code OptionalShort.empty()}. There is no guarantee that it is a singleton. Instead, use
* {@link #isPresent()}.
* @return An empty {@code OptionalShort}.
*/
public static OptionalShort empty() {
return EMPTY;
}
/**
* Construct an instance with the described value.
*
* @param value The short value to describe.
*/
private OptionalShort(final short value) {
this.isPresent = true;
this.value = value;
}
/**
* Returns an {@link OptionalShort} describing the given value.
*
* @param value The value to describe.
* @return An {@link OptionalShort} with the value present.
*/
public static OptionalShort of(final short value) {
return new OptionalShort(value);
}
/**
* If a value is present, returns the value, otherwise throws {@link NoSuchElementException}.
*
* @implNote The preferred alternative to this method is {@link #orElseThrow()}.
* @return The value described by this {@link OptionalShort}.
* @throws NoSuchElementException If no value is present.
*/
public short getAsShort() {
if (isPresent)
return value;
throw new NoSuchElementException("No value present");
}
/**
* If a value is present, returns {@code true}, otherwise {@code false}.
*
* @return {@code true} if a value is present, otherwise {@code false}.
*/
public boolean isPresent() {
return isPresent;
}
/**
* If a value is not present, returns {@code true}, otherwise {@code false}.
*
* @return {@code true} if a value is not present, otherwise {@code false}.
*/
public boolean isEmpty() {
return !isPresent;
}
/**
* If a value is present, performs the given action with the value, otherwise does nothing.
*
* @param action The action to be performed, if a value is present.
* @throws NullPointerException If value is present and the given action is null.
*/
public void ifPresent(final ShortConsumer action) {
if (isPresent)
action.accept(value);
}
/**
* If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
*
* @param action The action to be performed, if a value is present.
* @param emptyAction The empty-based action to be performed, if no value is present.
* @throws NullPointerException If a value is present and the given action is null, or no value is present and the given
* empty-based action is null.
*/
public void ifPresentOrElse(final ShortConsumer action, final Runnable emptyAction) {
if (isPresent)
action.accept(value);
else
emptyAction.run();
}
// /**
// * If a value is present, returns a sequential {@link ShortStream} containing only that value, otherwise returns an empty
// * {@code ShortStream}.
// *
// * Note: This method can be used to transform a {@code Stream} of optional shorts to an {@code ShortStream} of present
// * shorts:
// *
// *
// * {@code
// * Stream os = ..
// * ShortStream s = os.flatMapToShort(OptionalShort::stream)
// * }
// *
// *
// * @return The optional value as an {@code ShortStream}.
// */
// public ShortStream stream() {
// return isPresent ? ShortStream.of(value) : ShortStream.empty();
// }
/**
* If a value is present, returns the value, otherwise returns {@code other}.
*
* @param other The value to be returned, if no value is present.
* @return The value, if present, otherwise {@code other}.
*/
public short orElse(final short other) {
return isPresent ? value : other;
}
/**
* If a value is present, returns the value, otherwise returns the result produced by the supplying function.
*
* @param supplier The supplying function that produces a value to be returned.
* @return The value, if present, otherwise the result produced by the supplying function.
* @throws NullPointerException If no value is present and the supplying function is null.
*/
public short orElseGet(final ShortSupplier supplier) {
return isPresent ? value : supplier.getAsShort();
}
/**
* If a value is present, returns the value, otherwise throws {@link NoSuchElementException}.
*
* @return The value described by this {@code OptionalShort}.
* @throws NoSuchElementException If no value is present.
*/
public short orElseThrow() {
if (isPresent)
return value;
throw new NoSuchElementException("No value present");
}
/**
* If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.
*
* @implNote A method reference to the exception constructor with an empty argument list can be used as the supplier. For example,
* {@link IllegalStateException#IllegalStateException() IllegalStateException::new}.
* @param Type of the exception to be thrown
* @param exceptionSupplier The supplying function that produces an exception to be thrown.
* @return The value, if present
* @throws T If no value is present.
* @throws NullPointerException If no value is present and the exception supplying function is null.
*/
public short orElseThrow(final Supplier extends T> exceptionSupplier) throws T {
if (isPresent)
return value;
throw exceptionSupplier.get();
}
/**
* Indicates whether some other object is "equal to" this {@link OptionalShort}. The other object is considered equal if:
*
* - it is also an {@code OptionalShort} and;
*
- both instances have no value present or;
*
- the present values are "equal to" each other via {@code ==}.
*
*
* @param obj 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(final Object obj) {
if (this == obj)
return true;
if (!(obj instanceof OptionalShort))
return false;
final OptionalShort that = (OptionalShort)obj;
return isPresent && that.isPresent ? value == that.value : isPresent == that.isPresent;
}
/**
* Returns the hash code of the value, if present, otherwise {@code 0} (zero) if no value is present.
*
* @return Hash code value of the present value or {@code 0} if no value is present.
*/
@Override
public int hashCode() {
return isPresent ? Short.hashCode(value) : 0;
}
/**
* Returns a non-empty string representation of this {@code OptionalShort} suitable for debugging. The exact presentation format is
* unspecified and may vary between implementations and versions.
*
* @implNote If a value is present the result must include its string representation in the result. Empty and present
* {@code OptionalShort}s must be unambiguously differentiable.
* @return The string representation of this instance.
*/
@Override
public String toString() {
return isPresent ? String.format("OptionalShort[%s]", value) : "OptionalShort.empty";
}
}