Railway

The Railway type represents a value of one of two possible types (a disjoint union) of Success or Failure , where chaining follows the Railway Pattern , that is, once failed, stays failed.

Factory methods Railway#success(Object) and Railway#failure(Object) correspond to the two possible values.

API

Railway.java
interface Railway<F, S> {
  Success<F, S> success(S success)
  Failure<F, S> failure(F failure)
  boolean isSuccess()
  boolean isFailure()
  Optional<S> getSuccess()     (1)
  S getSuccessElseFail()
  S getSuccessElseFail(Function<F, ? extends Throwable> toThrowable)
  Optional<F> getFailure()     (2)
  F getFailureElseFail()
  Railway<F, S> ifSuccess(ThrowingConsumer<S> successConsumer)     (3)
  Railway<F, S> ifFailure(ThrowingConsumer<F> failureConsumer)     (4)
  Railway<F, R> mapSuccess(ThrowingFunction<S, R> successMapper)     (5)
  Railway<R, S> mapFailure(ThrowingFunction<F, R> failureMapper)     (6)
  R fold(ThrowingFunction<F, R> failureMapper, ThrowingFunction<S, R> successMapper)     (7)
  Railway<F, S> chain(ThrowingFunction<S, Railway<F, S>> chainingFunction)     (8)
}
1 getSuccess()

Optionally returns the contained value based on presence, that is, if this is a Success .

2 getFailure()

Optionally returns the contained failure based on presence, that is, if this is a Failure .

3 ifSuccess(ThrowingConsumer)

If this is a Success , peeks into the contained success .

4 ifFailure(ThrowingConsumer)

If this is a Failure , peeks into the contained failure .

5 mapSuccess(ThrowingFunction)

If this is a Success , maps this Railway to another (success). Otherwise if this is a Failure acts as identity operator.

6 mapFailure(ThrowingFunction)

Maps this Railway to another if this is a Failure . Otherwise if this is a Success acts as identity operator.

7 fold(ThrowingFunction, ThrowingFunction)

Maps the contained failure or success to a new value of type R using according mapping function failureMapper or successMapper .

8 chain(ThrowingFunction)
*Railway Pattern* If this is a _Success_ , returns a new xref:refguide:commons:index/functional/Railway.adoc[Railway] as produced by the chainingFunction, that receives the current success value as input. Otherwise if this is a _Failure_ acts as identity operator and the chainingFunction is not executed.

Members

getSuccess()

Optionally returns the contained value based on presence, that is, if this is a Success .

getFailure()

Optionally returns the contained failure based on presence, that is, if this is a Failure .

ifSuccess(ThrowingConsumer)

If this is a Success , peeks into the contained success .

ifFailure(ThrowingConsumer)

If this is a Failure , peeks into the contained failure .

mapSuccess(ThrowingFunction)

If this is a Success , maps this Railway to another (success). Otherwise if this is a Failure acts as identity operator.

mapFailure(ThrowingFunction)

Maps this Railway to another if this is a Failure . Otherwise if this is a Success acts as identity operator.

fold(ThrowingFunction, ThrowingFunction)

Maps the contained failure or success to a new value of type R using according mapping function failureMapper or successMapper .

chain(ThrowingFunction)

*Railway Pattern* If this is a _Success_ , returns a new xref:refguide:commons:index/functional/Railway.adoc[Railway] as produced by the chainingFunction, that receives the current success value as input. Otherwise if this is a _Failure_ acts as identity operator and the chainingFunction is not executed.

In other words: if once failed stays failed