Class Value2<B,C>

java.lang.Object
com.terheyden.value.Value2<B,C>

public class Value2<B,C> extends Object
A stack of two values.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <X, Y> Value2<X,Y>
    Returns an empty Value2 instance.
    boolean
    Indicates whether some other object is "equal to" this Value2.
    filter(io.vavr.CheckedPredicate<? super C> predicate)
    If a value is present, and the value matches the given predicate, returns an Value2 describing the value, otherwise returns an empty Value2.
    <D> Value2<B,D>
    flatMap(io.vavr.CheckedFunction1<? super C,? extends Optional<? extends D>> mapper)
    If a value is present, returns the result of applying the given Value2-bearing mapping function to the value, otherwise returns an empty Value2.
    <D> Value1<D>
    flatReduceAll(io.vavr.CheckedFunction2<? super B,? super C,Value1<? extends D>> reducer)
    Reduce all values to a single stack value.
    get()
    If a value is present, returns the value, otherwise throws NoSuchElementException.
    int
    Returns the hash code of the value, if present, otherwise 0 (zero) if no value is present.
    ifAllPresent(CheckedConsumer2<? super B,? super C> action)
    Perform the given action using all values, if present.
    ifEmpty(io.vavr.CheckedRunnable emptyAction)
    Performs the empty-based action if no value is present, otherwise does nothing.
    ifPresent(io.vavr.CheckedConsumer<? super C> action)
    If a value is present, performs the given action with the value, otherwise does nothing.
    ifPresentOrElse(io.vavr.CheckedConsumer<? super C> action, io.vavr.CheckedRunnable emptyAction)
    If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
    boolean
    True if any value is null.
    boolean
    True if all values are non-null.
    <U> Value2<B,U>
    map(io.vavr.CheckedFunction1<? super C,? extends U> mapper)
    If a value is present, returns an Value2 describing (as if by #ofNullable) the result of applying the given mapping function to the value, otherwise returns an empty Value2.
    or(C useIfEmpty)
    If a value is present, returns an Value2 describing the value, otherwise returns an Value2 produced by the supplying function.
    or(Value1<? extends C> useIfEmpty)
    If a value is present, returns an Value2 describing the value, otherwise returns an Value2 produced by the supplying function.
    or(io.vavr.CheckedFunction0<? extends Optional<? extends C>> supplier)
    If a value is present, returns an Value2 describing the value, otherwise returns an Value2 produced by the supplying function.
    or(io.vavr.CheckedFunction1<? super B,? extends C> valueFunction)
    If the last added value is empty, use the given value function along with all other present values to create a new value.
    or(Optional<? extends C> useIfEmpty)
    If a value is present, returns an Value2 describing the value, otherwise returns an Value2 produced by the supplying function.
    orElse(C other)
    If a value is present, returns the value, otherwise returns other.
    orElseGet(Supplier<? extends C> supplier)
    If a value is present, returns the value, otherwise returns the result produced by the supplying function.
    If a value is present, returns the value, otherwise throws NoSuchElementException.
    <X extends Throwable>
    C
    orElseThrow(io.vavr.CheckedFunction0<? extends X> exceptionSupplier)
    If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.
    <D> Value1<D>
    reduceAll(io.vavr.CheckedFunction2<? super B,? super C,? extends D> reducer)
    Reduce all values to a single stack value.
    If a value is present, returns a sequential Stream containing only that value, otherwise returns an empty Stream.
    Converts the last value to a Java Optional.
    Returns a non-empty string representation of this Value2 suitable for debugging.

    Methods inherited from class java.lang.Object

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

    • empty

      public static <X, Y> Value2<X,Y> empty()
      Returns an empty Value2 instance. No value is present for this Value2.
      Type Parameters:
      T - The type of the non-existent value
      Returns:
      an empty Value2
    • get

      public C get()
      If a value is present, returns the value, otherwise throws NoSuchElementException.
      Returns:
      the non-null value described by this Value2
      Throws:
      NoSuchElementException - if no value is present
    • isPresent

      public boolean isPresent()
      True if all values are non-null.
    • isEmpty

      public boolean isEmpty()
      True if any value is null.
    • ifPresent

      public Value2<B,C> ifPresent(io.vavr.CheckedConsumer<? super C> action)
      If a value is present, performs the given action with the value, otherwise does nothing.
      Parameters:
      action - the action to be performed, if a value is present
      Returns:
      this, for chaining
      Throws:
      NullPointerException - if value is present and the given action is null
    • ifPresentOrElse

      public Value2<B,C> ifPresentOrElse(io.vavr.CheckedConsumer<? super C> action, io.vavr.CheckedRunnable emptyAction)
      If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
      Parameters:
      action - the action to be performed, if a value is present
      emptyAction - the empty-based action to be performed, if no value is present
      Returns:
      this, for chaining
      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.
      Since:
      9
    • ifAllPresent

      public Value2<B,C> ifAllPresent(CheckedConsumer2<? super B,? super C> action)
      Perform the given action using all values, if present. If any value is empty, does nothing.
      Returns:
      this, for chaining
    • ifEmpty

      public Value2<B,C> ifEmpty(io.vavr.CheckedRunnable emptyAction)
      Performs the empty-based action if no value is present, otherwise does nothing.
      Parameters:
      action - the action to be performed, if a value is present
      emptyAction - the empty-based action to be performed, if no value is present
      Returns:
      this, for chaining
      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.
      Since:
      9
    • filter

      public Value2<B,C> filter(io.vavr.CheckedPredicate<? super C> predicate)
      If a value is present, and the value matches the given predicate, returns an Value2 describing the value, otherwise returns an empty Value2.
      Parameters:
      predicate - the predicate to apply to a value, if present
      Returns:
      an Value2 describing the value of this Value2, if a value is present and the value matches the given predicate, otherwise an empty Value2
      Throws:
      NullPointerException - if the predicate is null
    • map

      public <U> Value2<B,U> map(io.vavr.CheckedFunction1<? super C,? extends U> mapper)
      If a value is present, returns an Value2 describing (as if by #ofNullable) the result of applying the given mapping function to the value, otherwise returns an empty Value2.

      If the mapping function returns a null result then this method returns an empty Value2.

      Type Parameters:
      U - The type of the value returned from the mapping function
      Parameters:
      mapper - the mapping function to apply to a value, if present
      Returns:
      an Value2 describing the result of applying a mapping function to the value of this Value2, if a value is present, otherwise an empty Value2
      Throws:
      NullPointerException - if the mapping function is null
    • flatMap

      public <D> Value2<B,D> flatMap(io.vavr.CheckedFunction1<? super C,? extends Optional<? extends D>> mapper)
      If a value is present, returns the result of applying the given Value2-bearing mapping function to the value, otherwise returns an empty Value2.

      This method is similar to #map(Function), but the mapping function is one whose result is already an Value2, and if invoked, flatMap does not wrap it within an additional Value2.

      Type Parameters:
      D - The type of value of the Value2 returned by the mapping function
      Parameters:
      mapper - the mapping function to apply to a value, if present
      Returns:
      the result of applying an Value2-bearing mapping function to the value of this Value2, if a value is present, otherwise an empty Value2
      Throws:
      NullPointerException - if the mapping function is null or returns a null result
    • reduceAll

      public <D> Value1<D> reduceAll(io.vavr.CheckedFunction2<? super B,? super C,? extends D> reducer)
      Reduce all values to a single stack value.
    • flatReduceAll

      public <D> Value1<D> flatReduceAll(io.vavr.CheckedFunction2<? super B,? super C,Value1<? extends D>> reducer)
      Reduce all values to a single stack value.
    • or

      public Value2<B,C> or(io.vavr.CheckedFunction0<? extends Optional<? extends C>> supplier)
      If a value is present, returns an Value2 describing the value, otherwise returns an Value2 produced by the supplying function.
      Parameters:
      supplier - the supplying function that produces an Value2 to be returned
      Returns:
      returns an Value2 describing the value of this Value2, if a value is present, otherwise an Value2 produced by the supplying function.
      Throws:
      NullPointerException - if the supplying function is null or produces a null result
      Since:
      9
    • or

      public Value2<B,C> or(io.vavr.CheckedFunction1<? super B,? extends C> valueFunction)
      If the last added value is empty, use the given value function along with all other present values to create a new value.
    • or

      public Value2<B,C> or(Optional<? extends C> useIfEmpty)
      If a value is present, returns an Value2 describing the value, otherwise returns an Value2 produced by the supplying function.
      Parameters:
      supplier - the supplying function that produces an Value2 to be returned
      Returns:
      returns an Value2 describing the value of this Value2, if a value is present, otherwise an Value2 produced by the supplying function.
      Throws:
      NullPointerException - if the supplying function is null or produces a null result
      Since:
      9
    • or

      public Value2<B,C> or(Value1<? extends C> useIfEmpty)
      If a value is present, returns an Value2 describing the value, otherwise returns an Value2 produced by the supplying function.
      Parameters:
      supplier - the supplying function that produces an Value2 to be returned
      Returns:
      returns an Value2 describing the value of this Value2, if a value is present, otherwise an Value2 produced by the supplying function.
      Throws:
      NullPointerException - if the supplying function is null or produces a null result
      Since:
      9
    • or

      public Value2<B,C> or(C useIfEmpty)
      If a value is present, returns an Value2 describing the value, otherwise returns an Value2 produced by the supplying function.
      Parameters:
      supplier - the supplying function that produces an Value2 to be returned
      Returns:
      returns an Value2 describing the value of this Value2, if a value is present, otherwise an Value2 produced by the supplying function.
      Throws:
      NullPointerException - if the supplying function is null or produces a null result
      Since:
      9
    • stream

      public Stream<C> stream()
      If a value is present, returns a sequential Stream containing only that value, otherwise returns an empty Stream.
      Returns:
      the optional value as a Stream
      Since:
      9
    • orElse

      @Nullable public C orElse(@Nullable C other)
      If a value is present, returns the value, otherwise returns other.
      Parameters:
      other - the value to be returned, if no value is present. May be null.
      Returns:
      the value, if present, otherwise other
    • orElseGet

      @Nullable public C orElseGet(Supplier<? extends C> supplier)
      If a value is present, returns the value, otherwise returns the result produced by the supplying function.
      Parameters:
      supplier - the supplying function that produces a value to be returned
      Returns:
      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
    • orElseThrow

      public C orElseThrow()
      If a value is present, returns the value, otherwise throws NoSuchElementException.
      Returns:
      the non-null value described by this Value2
      Throws:
      NoSuchElementException - if no value is present
      Since:
      10
    • orElseThrow

      public <X extends Throwable> C orElseThrow(io.vavr.CheckedFunction0<? extends X> exceptionSupplier) throws X
      If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.
      Type Parameters:
      X - Type of the exception to be thrown
      Parameters:
      exceptionSupplier - the supplying function that produces an exception to be thrown
      Returns:
      the value, if present
      Throws:
      X - if no value is present
      NullPointerException - if no value is present and the exception supplying function is null
    • toOptional

      public Optional<C> toOptional()
      Converts the last value to a Java Optional.
    • equals

      public boolean equals(@Nullable Object obj)
      Indicates whether some other object is "equal to" this Value2. The other object is considered equal if:
      • it is also an Value2 and;
      • both instances have no value present or;
      • the present values are "equal to" each other via equals().
      Overrides:
      equals in class Object
      Parameters:
      obj - an object to be tested for equality
      Returns:
      true if the other object is "equal to" this object otherwise false
    • hashCode

      public int hashCode()
      Returns the hash code of the value, if present, otherwise 0 (zero) if no value is present.
      Overrides:
      hashCode in class Object
      Returns:
      hash code value of the present value or 0 if no value is present
    • toString

      public String toString()
      Returns a non-empty string representation of this Value2 suitable for debugging. The exact presentation format is unspecified and may vary between implementations and versions.
      Overrides:
      toString in class Object
      Returns:
      the string representation of this instance