Class FullOption<T,​I extends Option<T>>

java.lang.Object
io.foxcapades.lib.opt.impl.FullOption<T,​I>
Type Parameters:
T - Type of the wrapped value.
I - Type of the extending class.
All Implemented Interfaces:
Option<T>
Direct Known Subclasses:
FullNonNullOption, FullNullableOption

public abstract class FullOption<T,​I extends Option<T>> extends Object implements Option<T>
Base implementation of a non-empty option.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected T
    Value wrapped by this option.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    FullOption​(T value)
    Constructs a new option instance wrapping the given value.
  • Method Summary

    Modifier and Type
    Method
    Description
    <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>> ignored)
    Calls the given mapping Function ifPresent if this Option is not empty, otherwise calls the given Supplier ifEmpty.
    ifEmpty​(@NotNull Runnable ignored)
    Executes the given Runnable if and only if this Option is empty.
    ifPresent​(@NotNull Consumer<? super T> fn)
    Executes the given Consumer with the value wrapped by this Option if this Option is not empty.
    boolean
    Tests whether this Option does not contain a value.
    boolean
    Tests whether this Option contains a value.
    or​(T ignored)
    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> ignored)
    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> ignored)
    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 ignored)
    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.
    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.
    with​(@NotNull Consumer<? super T> ifPresent, @NotNull Runnable ignored)
    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, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface io.foxcapades.lib.opt.Option

    filter, map, map
  • Field Details

    • value

      protected final T value
      Value wrapped by this option.
  • Constructor Details

    • FullOption

      protected FullOption(T value)
      Constructs a new option instance wrapping the given value.

      This method does not check if the given input value is null, this check should be performed by implementation classes.

      Parameters:
      value - Value to wrap.
  • Method Details

    • isPresent

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

      IMPORTANT: Individual implementations of Option decide whether a null value may be wrapped. For those implementations, a return value of true does not mean the value contained is not null. It simply means that a value is wrapped.

      Implementers should detail their rules regarding null values.

      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: Option
      Tests whether this Option does not contain a value.

      IMPORTANT: Individual implementations of Option decide whether a null value may be wrapped. For those implementations, a return value of false does not indicate whether the value wrapped is null, instead it simply means that no value whatsoever was wrapped.

      Implementers should detail their rules regarding null values.

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

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

      IMPORTANT: Individual implementations of Option decide whether a null value may be wrapped. For those implementations, this method will not throw an exception if the value returned is null. Instead it will only throw an exception if no value was set whatsoever.

      Implementers should detail their rules regarding null values.

      Specified by:
      unwrap in interface Option<T>
      Returns:
      The value wrapped by this Option. This value may be null depending on whether this implementation of Option allows null values.
      Throws:
      UnwrapException - If this method is called on an empty Option.
    • or

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

      IMPORTANT: Individual implementations of Option decide whether a null value may be wrapped. For those implementations, the other value will not be returned if the Option is wrapping a null value as, for those implementations, null is a legal and expected possible value.

      Implementers should detail their rules regarding null values.

      Specified by:
      or in interface Option<T>
      Parameters:
      ignored - 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> ignored)
      Description copied from interface: Option
      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: Individual implementations of Option decide whether a null value may be wrapped. For those implementations, the Supplier will not be called if the Option is wrapping a null value as, for those implementations, null is a legal and expected possible value.

      Implementers should detail their rules regarding null values.

      Specified by:
      orGet in interface Option<T>
      Parameters:
      ignored - 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 ignored)
      Description copied from interface: Option
      Returns the value wrapped by this Option if it is not empty, otherwise throws the given Exception value.

      IMPORTANT: Individual implementations of Option decide whether a null value may be wrapped. For those implementations, the given Exception will not be thrown if the wrapped value is null. Instead, the exception will only be thrown if this Option is wrapping no value whatsoever.

      Implementers should detail their rules regarding null values.

      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:
      ignored - 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.
    • orElseThrow

      public <E extends Throwable> T orElseThrow(@NotNull @NotNull Supplier<? extends E> ignored)
      Description copied from interface: Option
      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: Individual implementations of Option decide whether a null value may be wrapped. For those implementations, the Supplier will not be called, and no Exception will be thrown if the wrapped value is null. Instead, the Supplier will be called and Exception thrown only if this Option is wrapping no value whatsoever.

      Implementers should detail their rules regarding null values.

      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:
      ignored - 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. Individual implementations decide whether null values may be wrapped.
    • flatMap

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

      IMPORTANT: Individual implementations of Option decide whether a null value may be wrapped. For those implementations, if the wrapped value is null, the given Function will be called, and passed the wrapped null value. If the current implementation allows nulls, this Function should be prepared to handle null inputs.

      Implementers should detail their rules regarding null values.

      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>> ignored)
      Description copied from interface: Option
      Calls the given mapping Function ifPresent if this Option is not empty, otherwise calls the given Supplier ifEmpty. IMPORTANT: Individual implementations of Option decide whether a null value may be wrapped. For those implementations, if the Option is wrapping a null value, it will call the Function ifPresent, passing in the wrapped null value. This means, for those Option implementations, the Function passed as ifPresent should be prepared to handle null values.

      Implementers should detail their rules regarding null values.

      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.

      ignored - 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: Option
      Creates a new Stream containing either one value, if this Option is not empty, or containing zero values if this Option is empty.

      IMPORTANT: Individual implementations of Option decide whether a null value may be wrapped. This means, for those implementations, the single value in the returned Stream for non-empty Options may be null.

      Implementers should detail their rules regarding null values.

      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 I ifPresent(@NotNull @NotNull Consumer<? super T> fn)
      Description copied from interface: Option
      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: Individual Option implementations decide whether a null value may be wrapped. For those implementations, the given Consumer will be called if the Option is wrapping a null value. This means, for those implementations, the passed Consumer should be prepared to handle a null input.

      Implementers should detail their rules regarding null values.

      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 I ifEmpty(@NotNull @NotNull Runnable ignored)
      Description copied from interface: Option
      Executes the given Runnable if and only if this Option is empty.

      IMPORTANT: Individual Option implementations decide whether a null value may be wrapped. For those implementations, if this Option is wrapping a null value, the given function will not be called.

      Implementers should detail their rules regarding null values.

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

      This argument must not be null.

      Returns:
      This Option instance.
    • with

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

      IMPORTANT: Individual Option implementations decide whether a null value may be wrapped. For those implementations, if the wrapped value is null, the given function ifPresent will be called and passed the wrapped null value, null values are considered legal and possible valid values. This means, for those Option implementations, the given ifPresent function should be prepared to handle a null input.

      Implementers should detail their rules regarding null values.

      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.

      ignored - Runnable to call if this Option is empty.

      This argument must not be null.

      Returns:
      This Option instance.
    • 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.