Interface FunctionalValue<T>

Type Parameters:
T - The type of the wrapped value.
All Superinterfaces:
java.lang.Iterable<T>
All Known Implementing Classes:
CallResult, CallResult.Failure, CallResult.FailureProjection, CallResult.Success, CallResult.SuccessProjection

public interface FunctionalValue<T>
extends java.lang.Iterable<T>
Call result is a simplified version of the vavr Value adapted to the JCoinbase needs. For more information, please take a look at the vavr site

Functional programming is all about values and transformation of values using functions. The FunctionalValue type reflects the values in a functional setting. It can be seen as the result of a partial function application. Hence the result may be undefined. If a value is undefined, we say it is empty.

How the empty state is interpreted depends on the context, i.e. it may be undefined, failed, no elements, etc.

Basic operations:

Iterable extensions: Side-effects: Tests:
  • Method Summary

    Modifier and Type Method Description
    default boolean contains​(T element)
    Shortcut for exists(e -> Objects.equals(e, element)), tests if the given element is contained.
    boolean equals​(java.lang.Object o)
    Clarifies that functionalValues have a proper equals() method implemented.
    default boolean exists​(java.util.function.Predicate<? super T> predicate)
    Checks, if an element exists such that the predicate holds.
    default boolean forAll​(java.util.function.Predicate<? super T> predicate)
    Checks, if the given predicate holds for all elements.
    T get()
    Gets the underlying functionalValue or throws if no functionalValue is present.
    default T getOrElse​(java.util.function.Supplier<? extends T> supplier)
    Returns the underlying functionalValue if present, otherwise other.
    default T getOrElse​(T other)
    Returns the underlying functionalValue if present, otherwise other.
    default <X extends java.lang.Throwable>
    T
    getOrElseThrow​(java.util.function.Supplier<X> supplier)
    Returns the underlying functionalValue if present, otherwise throws supplier.get().
    default T getOrElseTry​(io.vavr.CheckedFunction0<? extends T> supplier)
    Returns the underlying functionalValue if present, otherwise returns the result of Try.of(supplier).get().
    default T getOrNull()
    Returns the underlying functionalValue if present, otherwise null.
    int hashCode()
    Clarifies that functionalValues have a proper hashCode() method implemented.
    default boolean isAsync()
    Checks if this Value is asynchronously (short: async) computed.
    boolean isEmpty()
    Checks, this Value is empty, i.e. if the underlying functionalValue is absent.
    default boolean isLazy()
    Checks if this Value is lazily evaluated.
    default boolean isSingleValued()
    States whether this is a single-valued type.
    io.vavr.collection.Iterator<T> iterator()
    Returns a rich io.vavr.collection.Iterator.
    <U> FunctionalValue<U> map​(java.util.function.Function<? super T,​? extends U> mapper)
    Maps the underlying functionalValue to a different component type.
    FunctionalValue<T> peek​(java.util.function.Consumer<? super T> action)
    Performs the given action on the first element if this is an eager implementation.
    default java.util.Spliterator<T> spliterator()  
    default <L> CallResult<L,​T> toCallResult​(java.util.function.Supplier<? extends L> leftSupplier)
    Converts this to a CallResult.
    default <L> CallResult<L,​T> toCallResult​(L left)
    Converts this to a CallResult.
    default java.util.Optional<T> toJavaOptional()
    Converts this to an Optional.
    default io.vavr.control.Option<T> toOption()
    Converts this to an Option.
    java.lang.String toString()
    Clarifies that functionalValues have a proper toString() method implemented.

    Methods inherited from interface java.lang.Iterable

    forEach
  • Method Details

    • contains

      default boolean contains​(T element)
      Shortcut for exists(e -> Objects.equals(e, element)), tests if the given element is contained.
      Parameters:
      element - An Object of type A, may be null.
      Returns:
      true, if element is contained, false otherwise.
    • exists

      default boolean exists​(java.util.function.Predicate<? super T> predicate)
      Checks, if an element exists such that the predicate holds.
      Parameters:
      predicate - A Predicate
      Returns:
      true, if predicate holds for one or more elements, false otherwise
      Throws:
      java.lang.NullPointerException - if predicate is null
    • isAsync

      default boolean isAsync()
      Checks if this Value is asynchronously (short: async) computed.

      Methods of a Value instance that operate on the underlying value may block the current thread until the value is present and the computation can be performed.

      Returns:
      true if this Value is async (like Future), false otherwise.
    • isLazy

      default boolean isLazy()
      Checks if this Value is lazily evaluated.
      Returns:
      true if this Value is lazy (like Lazy and Stream), false otherwise.
    • isSingleValued

      default boolean isSingleValued()
      States whether this is a single-valued type.
      Returns:
      true if this is single-valued, false otherwise.
    • forAll

      default boolean forAll​(java.util.function.Predicate<? super T> predicate)
      Checks, if the given predicate holds for all elements.
      Parameters:
      predicate - A Predicate
      Returns:
      true, if the predicate holds for all elements, false otherwise
      Throws:
      java.lang.NullPointerException - if predicate is null
    • get

      T get()
      Gets the underlying functionalValue or throws if no functionalValue is present.

      IMPORTANT! This method will throw an undeclared Throwable if isEmpty() == true is true.

      Because the 'empty' state indicates that there is no functionalValue present that can be returned, get() has to throw in such a case. Generally, implementing classes should throw a NoSuchElementException if isEmpty() returns true.

      However, there exist use-cases, where implementations may throw other exceptions. See Try.get().

      Additional note: Dynamic proxies will wrap an undeclared exception in a UndeclaredThrowableException.

      Returns:
      the underlying functionalValue if this is not empty, otherwise get() throws a Throwable
    • getOrElse

      default T getOrElse​(T other)
      Returns the underlying functionalValue if present, otherwise other.
      Parameters:
      other - An alternative functionalValue.
      Returns:
      A functionalValue of type T
    • getOrElse

      default T getOrElse​(java.util.function.Supplier<? extends T> supplier)
      Returns the underlying functionalValue if present, otherwise other.
      Parameters:
      supplier - An alternative functionalValue supplier.
      Returns:
      A functionalValue of type T
      Throws:
      java.lang.NullPointerException - if supplier is null
    • getOrElseThrow

      default <X extends java.lang.Throwable> T getOrElseThrow​(java.util.function.Supplier<X> supplier) throws X extends java.lang.Throwable
      Returns the underlying functionalValue if present, otherwise throws supplier.get().
      Type Parameters:
      X - a Throwable type
      Parameters:
      supplier - An exception supplier.
      Returns:
      A functionalValue of type T.
      Throws:
      java.lang.NullPointerException - if supplier is null
      X - if no functionalValue is present
    • getOrElseTry

      default T getOrElseTry​(io.vavr.CheckedFunction0<? extends T> supplier)
      Returns the underlying functionalValue if present, otherwise returns the result of Try.of(supplier).get().
      Parameters:
      supplier - An alternative functionalValue supplier.
      Returns:
      A functionalValue of type T.
      Throws:
      java.lang.NullPointerException - if supplier is null
    • getOrNull

      default T getOrNull()
      Returns the underlying functionalValue if present, otherwise null.
      Returns:
      A functionalValue of type T or null.
    • isEmpty

      boolean isEmpty()
      Checks, this Value is empty, i.e. if the underlying functionalValue is absent.
      Returns:
      false, if no underlying functionalValue is present, true otherwise.
    • map

      <U> FunctionalValue<U> map​(java.util.function.Function<? super T,​? extends U> mapper)
      Maps the underlying functionalValue to a different component type.
      Type Parameters:
      U - The new component type
      Parameters:
      mapper - A mapper
      Returns:
      A new functionalValue
    • peek

      FunctionalValue<T> peek​(java.util.function.Consumer<? super T> action)
      Performs the given action on the first element if this is an eager implementation. Performs the given action on all elements (the first immediately, successive deferred), if this is a lazy implementation.
      Parameters:
      action - The action that will be performed on the element(s).
      Returns:
      this instance
    • iterator

      io.vavr.collection.Iterator<T> iterator()
      Returns a rich io.vavr.collection.Iterator.
      Specified by:
      iterator in interface java.lang.Iterable<T>
      Returns:
      A new Iterator
    • toJavaOptional

      default java.util.Optional<T> toJavaOptional()
      Converts this to an Optional.
      
       // = Optional.empty
       Future.of(() -> { throw new Error(); })
             .toJavaOptional()
      
       // = Optional[ok]
       Try.of(() -> "ok")
           .toJavaOptional()
      
       // = Optional[1]
       List.of(1, 2, 3)
           .toJavaOptional()
       
      Returns:
      A new Optional.
    • toCallResult

      default <L> CallResult<L,​T> toCallResult​(java.util.function.Supplier<? extends L> leftSupplier)
      Converts this to a CallResult.
      Type Parameters:
      L - Validation error component type
      Parameters:
      leftSupplier - A Supplier for the failure value for the CallResult
      Returns:
      A new CallResult.
    • toCallResult

      default <L> CallResult<L,​T> toCallResult​(L left)
      Converts this to a CallResult.
      Type Parameters:
      L - CallResult failure component type
      Parameters:
      left - A failure value for the CallResult
      Returns:
      A new CallResult.
    • toOption

      default io.vavr.control.Option<T> toOption()
      Converts this to an Option.
      Returns:
      A new Option.
    • spliterator

      default java.util.Spliterator<T> spliterator()
      Specified by:
      spliterator in interface java.lang.Iterable<T>
    • equals

      boolean equals​(java.lang.Object o)
      Clarifies that functionalValues have a proper equals() method implemented.

      See Object.equals(Object).

      Overrides:
      equals in class java.lang.Object
      Parameters:
      o - An object
      Returns:
      true, if this equals o, false otherwise
    • hashCode

      int hashCode()
      Clarifies that functionalValues have a proper hashCode() method implemented.

      See Object.hashCode().

      Overrides:
      hashCode in class java.lang.Object
      Returns:
      The hashcode of this object
    • toString

      java.lang.String toString()
      Clarifies that functionalValues have a proper toString() method implemented.

      See Object.toString().

      Overrides:
      toString in class java.lang.Object
      Returns:
      A String representation of this object