Class CallResult.SuccessProjection<L,​R>

java.lang.Object
com.github.badpop.jcoinbase.control.CallResult.SuccessProjection<L,​R>
Type Parameters:
L - The type of the Failure value of a CallResult.
R - The type of the Success value of a CallResult.
All Implemented Interfaces:
FunctionalValue<R>, java.lang.Iterable<R>
Enclosing class:
CallResult<L,​R>

public static final class CallResult.SuccessProjection<L,​R>
extends java.lang.Object
implements FunctionalValue<R>
A success projection of a CallResult.
  • Method Details

    • isEmpty

      public boolean isEmpty()
      Description copied from interface: FunctionalValue
      Checks, this Value is empty, i.e. if the underlying functionalValue is absent.
      Specified by:
      isEmpty in interface FunctionalValue<L>
      Returns:
      false, if no underlying functionalValue is present, true otherwise.
    • get

      public R get()
      Gets the Success value or throws.
      Specified by:
      get in interface FunctionalValue<L>
      Returns:
      the success value, if the underlying CallResult is a Success
      Throws:
      java.util.NoSuchElementException - if the underlying CallResult of this SuccessProjection is a Failure
    • orElse

      public CallResult.SuccessProjection<L,​R> orElse​(CallResult.SuccessProjection<? extends L,​? extends R> other)
    • orElse

      public CallResult.SuccessProjection<L,​R> orElse​(java.util.function.Supplier<? extends CallResult.SuccessProjection<? extends L,​? extends R>> supplier)
    • getOrElse

      public R getOrElse​(R other)
      Gets the Success value or an alternate value, if the projected CallResult is a Failure.
      Specified by:
      getOrElse in interface FunctionalValue<L>
      Parameters:
      other - an alternative value
      Returns:
      the success value, if the underlying CallResult is a Success or else other
      Throws:
      java.util.NoSuchElementException - if the underlying callResult of this SuccessProjection is a Failure
    • getOrElseGet

      public R getOrElseGet​(java.util.function.Function<? super L,​? extends R> other)
      Gets the Success value or an alternate value, if the projected CallResult is a Failure.
      
       // prints 42
       System.out.println(CallResult.success(42).getOrElseGet(l -> 2));
       // prints 0
       System.out.println(CallResult.failure(42).getOrElseGet(l -> 0));
       
      Parameters:
      other - a function which converts a Failure value to an alternative Success value
      Returns:
      the success value, if the underlying CallResult is a Success or else the alternative Success value provided by other by applying the Failure value.
    • orElseRun

      public void orElseRun​(java.util.function.Consumer<? super L> action)
      Runs an action in the case this is a projection on a Failure value.
      
       // nothing is printed
       CallResult.success(42).orElseRun(System.out::println);
      
       // prints "error message"
       CallResult.failure("error message").orElseRun(System.out::println);
       
      Parameters:
      action - an action which consumes a Failure value
    • getOrElseThrow

      public <X extends java.lang.Throwable> R getOrElseThrow​(java.util.function.Function<? super L,​X> exceptionFunction) throws X extends java.lang.Throwable
      Gets the Success value or throws, if the projected CallResult is a Failure.
      
       // prints "42"
       System.out.println(CallResult.<String, Integer> success(42).getOrElseThrow(s -> new RuntimeException(s)));
      
       // throws RuntimeException("error message")
       CallResult.failure("error message").getOrElseThrow(s -> new RuntimeException(s));
       
      Type Parameters:
      X - a throwable type
      Parameters:
      exceptionFunction - a function which creates an exception based on a Failure value
      Returns:
      the success value, if the underlying CallResult is a Success or else throws the exception provided by exceptionFunction by applying the Failure value.
      Throws:
      X - if the projected CallResult is a Failure
    • toCallResult

      public CallResult<L,​R> toCallResult()
      Returns the underlying callResult of this projection.
      Returns:
      the underlying callResult
    • filter

      public io.vavr.control.Option<CallResult.SuccessProjection<L,​R>> filter​(java.util.function.Predicate<? super R> predicate)
      Returns Some value of type R if this is a success projection of a Success value and the predicate applies to the underlying value.
      Parameters:
      predicate - A predicate
      Returns:
      A new Option
    • flatMap

      public <U> CallResult.SuccessProjection<L,​U> flatMap​(java.util.function.Function<? super R,​? extends CallResult.SuccessProjection<L,​? extends U>> mapper)
      FlatMaps this SuccessProjection.
      Type Parameters:
      U - Component type of the mapped success value
      Parameters:
      mapper - A mapper
      Returns:
      this as SuccessProjection<L, U> if a Failure is underlying, otherwise a the mapping result of the success value.
      Throws:
      java.lang.NullPointerException - if mapper is null
    • map

      public <U> CallResult.SuccessProjection<L,​U> map​(java.util.function.Function<? super R,​? extends U> mapper)
      Maps the success value if the projected CallResult is a Success.
      Specified by:
      map in interface FunctionalValue<L>
      Type Parameters:
      U - The new type of a Success value
      Parameters:
      mapper - A mapper which takes a success value and returns a value of type U
      Returns:
      A new SuccessProjection
    • peek

      public CallResult.SuccessProjection<L,​R> peek​(java.util.function.Consumer<? super R> action)
      Applies the given action to the value if the projected callResult is a Success. Otherwise nothing happens.
      Specified by:
      peek in interface FunctionalValue<L>
      Parameters:
      action - An action which takes a success value
      Returns:
      this CallResult instance
    • transform

      public <U> U transform​(java.util.function.Function<? super CallResult.SuccessProjection<L,​R>,​? extends U> f)
      Transforms this SuccessProjection.
      Type Parameters:
      U - Type of transformation result
      Parameters:
      f - A transformation
      Returns:
      An instance of type U
      Throws:
      java.lang.NullPointerException - if f is null
    • iterator

      public io.vavr.collection.Iterator<R> iterator()
      Description copied from interface: FunctionalValue
      Returns a rich io.vavr.collection.Iterator.
      Specified by:
      iterator in interface FunctionalValue<L>
      Specified by:
      iterator in interface java.lang.Iterable<L>
      Returns:
      A new Iterator
    • equals

      public boolean equals​(java.lang.Object obj)
      Description copied from interface: FunctionalValue
      Clarifies that functionalValues have a proper equals() method implemented.

      See Object.equals(Object).

      Specified by:
      equals in interface FunctionalValue<L>
      Overrides:
      equals in class java.lang.Object
      Parameters:
      obj - An object
      Returns:
      true, if this equals o, false otherwise
    • hashCode

      public int hashCode()
      Description copied from interface: FunctionalValue
      Clarifies that functionalValues have a proper hashCode() method implemented.

      See Object.hashCode().

      Specified by:
      hashCode in interface FunctionalValue<L>
      Overrides:
      hashCode in class java.lang.Object
      Returns:
      The hashcode of this object