Record Class EmptyNonNull<T>

java.lang.Object
java.lang.Record
io.foxcapades.lib.opt.impl.EmptyNonNull<T>
Type Parameters:
T - Generic type of this option.
All Implemented Interfaces:
NonNullOption<T>, Option<T>

public record EmptyNonNull<T>() extends Record implements NonNullOption<T>
Singleton type representing an empty option.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an instance of a EmptyNonNull record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    equals​(Object o)
    Indicates whether some other object is "equal to" this one.
    @NotNull NonNullOption<T>
    filter​(@NotNull Predicate<? super T> fn)
    Calls the given Predicate on the value wrapped by this Option, if this Option is not empty.
    <R> @NotNull Option<R>
    flatMap​(@NotNull Function<? super T,​? extends Option<? extends R>> fn)
    Calls the given Function if this Option is not empty.
    <R> @NotNull Option<R>
    flatMap​(@NotNull Function<? super T,​? extends Option<? extends R>> ifPresent, @NotNull Supplier<? extends Option<? extends R>> ifEmpty)
    Calls the given mapping Function ifPresent if this Option is not empty, otherwise calls the given Supplier ifEmpty.
    int
    Returns a hash code value for this object.
    @NotNull NonNullOption<T>
    ifEmpty​(@NotNull Runnable fn)
    Executes the given Runnable if and only if this Option is empty.
    @NotNull NonNullOption<T>
    ifPresent​(@NotNull Consumer<? super T> fn)
    Executes the given Consumer with the value wrapped by this Option if this Option is not empty.
    static <T> NonNullOption<T>
    Returns the singleton EmptyNonNull instance.
    boolean
    Tests whether this Option does not contain a value.
    boolean
    Tests whether this Option contains a value.
    <R> @NotNull NonNullOption<R>
    map​(@NotNull Function<? super T,​? extends R> fn)
    Calls the given Function on the value wrapped by this Option if and only if this Option is not empty.
    <R> @NotNull NonNullOption<R>
    map​(@NotNull Function<? super T,​? extends R> ifPresent, @NotNull Supplier<? extends R> ifEmpty)
    Calls the given Function ifPresent if this Option is not empty, otherwise calls the given Supplier ifEmpty.
    or​(T other)
    Returns either the value wrapped by this Option, if it is not empty, or the given value if this Option is empty.
    <E extends Throwable>
    T
    orElseThrow​(@NotNull Supplier<? extends E> fn)
    Returns the value wrapped by this Option if it is not empty, otherwise throws the Exception returned by the given Supplier.
    orGet​(@NotNull Supplier<? extends T> fn)
    Returns either the value wrapped by this Option, if it is not empty, or the value returned by the given Supplier.
    <E extends Throwable>
    T
    orThrow​(E err)
    Returns the value wrapped by this Option if it is not empty, otherwise throws the given Exception value.
    @NotNull Stream<T>
    Creates a new Stream containing either one value, if this Option is not empty, or containing zero values if this Option is empty.
    @NotNull NullableOption<T>
    Converts this NonNullOption into an instance of NullableOption.
    @NotNull NullableOption<T>
    toNullable​(boolean emptyToNull)
    Converts this NonNullOption into an instance of NullableOption.
    Returns a string representation of this record class.
    Attempts to unwrap this Option's value.
    boolean
    valueEquals​(@Nullable Object value)
    Returns whether the value wrapped by this option equals the given input value.
    @NotNull NonNullOption<T>
    with​(@NotNull Consumer<? super T> ifPresent, @NotNull Runnable ifEmpty)
    Executes the given Consumer ifPresent on the wrapped value if this Option is not empty, otherwise calls the given Runnable ifEmpty.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • EmptyNonNull

      public EmptyNonNull()
      Creates an instance of a EmptyNonNull record class.
  • Method Details

    • instance

      public static <T> NonNullOption<T> instance()
      Returns the singleton EmptyNonNull instance.
      Type Parameters:
      T - Generic type of the returned option.
      Returns:
      The singleton EmptyNonNull instance.
    • isPresent

      public boolean isPresent()
      Description copied from interface: NonNullOption
      Tests whether this Option contains a value.

      IMPORTANT: Implementations of this interface do not allow wrapping null values.

      Specified by:
      isPresent in interface NonNullOption<T>
      Specified by:
      isPresent in interface Option<T>
      Returns:
      true if this Option contains a value, otherwise false.
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: NonNullOption
      Tests whether this Option does not contain a value.

      IMPORTANT: Implementations of this interface do not allow wrapping null values.

      Specified by:
      isEmpty in interface NonNullOption<T>
      Specified by:
      isEmpty in interface Option<T>
      Returns:
      true if this Option does not contain a value, otherwise false.
    • unwrap

      @NotNull public T unwrap() throws UnwrapException
      Description copied from interface: NonNullOption
      Attempts to unwrap this Option's value. If this option is empty, this method throws an UnwrapException.

      IMPORTANT: Implementations of this interface do not allow wrapping null values.

      Specified by:
      unwrap in interface NonNullOption<T>
      Specified by:
      unwrap in interface Option<T>
      Returns:
      The value wrapped by this Option.
      Throws:
      UnwrapException - If this method is called on an empty Option.
    • or

      @Nullable public T or(@Nullable T other)
      Description copied from interface: NonNullOption
      Returns either the value wrapped by this Option, if it is not empty, or the given value if this Option is empty.

      IMPORTANT: Implementations of this interface do not allow wrapping null values.

      Specified by:
      or in interface NonNullOption<T>
      Specified by:
      or in interface Option<T>
      Parameters:
      other - Fallback value to return if this Option is empty.

      This argument may be null.

      Returns:
      Either the value wrapped by this Option, or the value of other.
    • orGet

      @Nullable public T orGet(@NotNull @NotNull Supplier<? extends T> fn)
      Description copied from interface: NonNullOption
      Returns either the value wrapped by this Option, if it is not empty, or the value returned by the given Supplier.

      The given Supplier will not be called if this Option is not empty.

      IMPORTANT: Implementations of this interface do not allow wrapping null values.

      Specified by:
      orGet in interface NonNullOption<T>
      Specified by:
      orGet in interface Option<T>
      Parameters:
      fn - Supplier for the fallback value.

      This argument must not be null.

      The value returned by the given Supplier may be null.

      Returns:
      Either the value wrapped by this Option, or the value returned by the given Supplier.
    • orThrow

      public <E extends Throwable> T orThrow(@NotNull E err) throws E
      Description copied from interface: NonNullOption
      Returns the value wrapped by this Option if it is not empty, otherwise throws the given Exception value.

      IMPORTANT: Implementations of this interface do not allow wrapping null values.

      Specified by:
      orThrow in interface NonNullOption<T>
      Specified by:
      orThrow in interface Option<T>
      Type Parameters:
      E - Type of the Exception value that will be thrown if this Option is empty.
      Parameters:
      err - Exception to throw if this Option is empty.

      This argument must not be null.

      Returns:
      The value wrapped by this Option. Individual implementations decide whether null values may be wrapped and returned.
      Throws:
      E - Thrown if this Option is empty.
    • orElseThrow

      public <E extends Throwable> T orElseThrow(@NotNull @NotNull Supplier<? extends E> fn) throws E
      Description copied from interface: NonNullOption
      Returns the value wrapped by this Option if it is not empty, otherwise throws the Exception returned by the given Supplier.

      The given Supplier will not be called if this Option is not empty.

      IMPORTANT: Implementations of this interface do not allow wrapping null values.

      Specified by:
      orElseThrow in interface NonNullOption<T>
      Specified by:
      orElseThrow in interface Option<T>
      Type Parameters:
      E - Type of the exception that will be returned by the given Supplier and thrown.
      Parameters:
      fn - Supplier for the Exception to throw if this Option is empty.

      This argument must not be null.

      This Supplier must not return null.

      Returns:
      The value wrapped by this Option.
      Throws:
      E - If this Option is empty.
    • map

      @NotNull public <R> @NotNull NonNullOption<R> map(@NotNull @NotNull Function<? super T,​? extends R> fn)
      Description copied from interface: NonNullOption
      Calls the given Function on the value wrapped by this Option if and only if this Option is not empty.

      IMPORTANT: Implementations of this interface do not allow wrapping null values.

      If the given function returns null an empty Option will be returned. If the given function does not return null a new Option will be returned wrapping the result value of the call to the given Function.

      Specified by:
      map in interface NonNullOption<T>
      Specified by:
      map in interface Option<T>
      Type Parameters:
      R - The return type of the given mapping Function.
      Parameters:
      fn - Function to call on the value wrapped by this Option.

      This argument must not be null

      Returns:
      A new Option of generic type R. This Option will be empty if the return value of fn was null, otherwise the returned Option will be wrapping the value returned by the given Function.
    • map

      @NotNull public <R> @NotNull NonNullOption<R> map(@NotNull @NotNull Function<? super T,​? extends R> ifPresent, @NotNull @NotNull Supplier<? extends R> ifEmpty)
      Description copied from interface: NonNullOption
      Calls the given Function ifPresent if this Option is not empty, otherwise calls the given Supplier ifEmpty.

      IMPORTANT: Implementations of this interface do not allow wrapping null values.

      Specified by:
      map in interface NonNullOption<T>
      Specified by:
      map in interface Option<T>
      Type Parameters:
      R - Generic type of the returned Option.
      Parameters:
      ifPresent - Mapping function to call on the value wrapped by this Option.

      This argument must not be null.

      This function will only be called if this Option is not empty.

      ifEmpty - Value supplier to call when this Option is empty.

      This argument must not be null.

      Returns:
      A new Option of generic type R.
    • flatMap

      @NotNull public <R> @NotNull Option<R> flatMap(@NotNull @NotNull Function<? super T,​? extends Option<? extends R>> fn)
      Description copied from interface: NonNullOption
      Calls the given Function if this Option is not empty.

      IMPORTANT: Implementations of this interface do not allow wrapping null values.

      Specified by:
      flatMap in interface NonNullOption<T>
      Specified by:
      flatMap in interface Option<T>
      Type Parameters:
      R - Generic type of the returned Option.
      Parameters:
      fn - Mapping function to call if this Option is not empty.

      This argument must not be null.

      This function must not return null

      This function will not be called if this Option is empty.

      Returns:
      A new Option of generic type R.
    • flatMap

      @NotNull public <R> @NotNull Option<R> flatMap(@NotNull @NotNull Function<? super T,​? extends Option<? extends R>> ifPresent, @NotNull @NotNull Supplier<? extends Option<? extends R>> ifEmpty)
      Description copied from interface: NonNullOption
      Calls the given mapping Function ifPresent if this Option is not empty, otherwise calls the given Supplier ifEmpty. IMPORTANT: Implementations of this interface do not allow wrapping null values.
      Specified by:
      flatMap in interface NonNullOption<T>
      Specified by:
      flatMap in interface Option<T>
      Type Parameters:
      R - Generic type for the returned Option.
      Parameters:
      ifPresent - Mapping function to call if this Option is not empty.

      This argument must not be null.

      This function must not return null.

      ifEmpty - Supplier function to call if this Option is empty.

      This argument must not be null.

      This value must not return null.

      Returns:
      A new Option of generic type R.
    • stream

      @NotNull public @NotNull Stream<T> stream()
      Description copied from interface: NonNullOption
      Creates a new Stream containing either one value, if this Option is not empty, or containing zero values if this Option is empty.

      IMPORTANT: Implementations of this interface do not allow wrapping null values.

      Specified by:
      stream in interface NonNullOption<T>
      Specified by:
      stream in interface Option<T>
      Returns:
      A new Stream that may contain the value wrapped by this Option, if such a value exists, otherwise contains no values.
    • ifPresent

      @NotNull public @NotNull NonNullOption<T> ifPresent(@NotNull @NotNull Consumer<? super T> fn)
      Description copied from interface: NonNullOption
      Executes the given Consumer with the value wrapped by this Option if this Option is not empty.

      The given function will not be called if this Option is empty.

      IMPORTANT: Implementations of this interface do not allow wrapping null values.

      Specified by:
      ifPresent in interface NonNullOption<T>
      Specified by:
      ifPresent in interface Option<T>
      Parameters:
      fn - Consumer to call on the wrapped value if this Option is not empty.

      This argument must not be null.

      Returns:
      This Option instance.
    • ifEmpty

      @NotNull public @NotNull NonNullOption<T> ifEmpty(@NotNull @NotNull Runnable fn)
      Description copied from interface: NonNullOption
      Executes the given Runnable if and only if this Option is empty.

      IMPORTANT: Implementations of this interface do not allow wrapping null values.

      Specified by:
      ifEmpty in interface NonNullOption<T>
      Specified by:
      ifEmpty in interface Option<T>
      Parameters:
      fn - Runnable to call if this Option is empty.

      This argument must not be null.

      Returns:
      This Option instance.
    • with

      @NotNull public @NotNull NonNullOption<T> with(@NotNull @NotNull Consumer<? super T> ifPresent, @NotNull @NotNull Runnable ifEmpty)
      Description copied from interface: NonNullOption
      Executes the given Consumer ifPresent on the wrapped value if this Option is not empty, otherwise calls the given Runnable ifEmpty.

      IMPORTANT: Implementations of this interface do not allow wrapping null values.

      Specified by:
      with in interface NonNullOption<T>
      Specified by:
      with in interface Option<T>
      Parameters:
      ifPresent - Consumer to call with the value wrapped by this Option if this Option is not empty.

      This argument must not be null.

      ifEmpty - Runnable to call if this Option is empty.

      This argument must not be null.

      Returns:
      This Option instance.
    • filter

      @NotNull public @NotNull NonNullOption<T> filter(@NotNull @NotNull Predicate<? super T> fn)
      Description copied from interface: NonNullOption
      Calls the given Predicate on the value wrapped by this Option, if this Option is not empty.

      IMPORTANT: Implementations of this interface do not allow wrapping null values.

      Specified by:
      filter in interface NonNullOption<T>
      Specified by:
      filter in interface Option<T>
      Parameters:
      fn - Predicate to apply to the value wrapped by this Option if this Option is not empty.

      This value must not be null.

      Returns:
      This Option if the given Predicate returns true, otherwise an empty Option.
    • valueEquals

      public boolean valueEquals(@Nullable @Nullable Object value)
      Description copied from interface: Option
      Returns whether the value wrapped by this option equals the given input value.

      For empty options, this method will always return false.

      Specified by:
      valueEquals in interface Option<T>
      Parameters:
      value - Value to compare against the wrapped value.
      Returns:
      Whether the given value equals the value wrapped by this option. If this option is empty, this value will always be false. If this option allows null values and is wrapping a null value, this method will only return true if the given input is also null.
    • toNullable

      @NotNull public @NotNull NullableOption<T> toNullable()
      Description copied from interface: NonNullOption
      Converts this NonNullOption into an instance of NullableOption.

      If the current Option is empty, the returned Option will also be empty.

      This method is equivalent to calling toNullable(false).

      Specified by:
      toNullable in interface NonNullOption<T>
      Returns:
      A NullableOption that is either empty or wrapping the same value as this Option.
    • toNullable

      @NotNull public @NotNull NullableOption<T> toNullable(boolean emptyToNull)
      Description copied from interface: NonNullOption
      Converts this NonNullOption into an instance of NullableOption.

      If the current option is not empty, the returned Option will wrap the same value as this Option.

      If the current option is empty, either a new NullableOption wrapping null or an empty Option will be returned depending on the value of emptyToNull.

      If emptyToNull == true && this.isEmpty(), then a NullableOption that is not-empty, wrapping the value null will be returned.

      If emptyToNull == false && this.isEmpty(), then an empty option will be returned.

      Specified by:
      toNullable in interface NonNullOption<T>
      Parameters:
      emptyToNull - Flag indicating whether an empty option instance should be converted to a null wrapping option or an empty option.
      Returns:
      A new option that will be wrapping this option's value, null, or will be empty.
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.