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 Summary
Modifier and Type Method Description boolean
equals(java.lang.Object obj)
Clarifies that functionalValues have a proper equals() method implemented.io.vavr.control.Option<CallResult.SuccessProjection<L,R>>
filter(java.util.function.Predicate<? super R> predicate)
ReturnsSome
value of type R if this is a success projection of a Success value and the predicate applies to the underlying value.<U> CallResult.SuccessProjection<L,U>
flatMap(java.util.function.Function<? super R,? extends CallResult.SuccessProjection<L,? extends U>> mapper)
FlatMaps this SuccessProjection.R
get()
Gets theSuccess
value or throws.R
getOrElse(R other)
Gets the Success value or an alternate value, if the projected CallResult is a Failure.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.<X extends java.lang.Throwable>
RgetOrElseThrow(java.util.function.Function<? super L,X> exceptionFunction)
Gets the Success value or throws, if the projected CallResult is a Failure.int
hashCode()
Clarifies that functionalValues have a proper hashCode() method implemented.boolean
isEmpty()
Checks, thisValue
is empty, i.e. if the underlying functionalValue is absent.io.vavr.collection.Iterator<R>
iterator()
Returns a richio.vavr.collection.Iterator
.<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.CallResult.SuccessProjection<L,R>
orElse(CallResult.SuccessProjection<? extends L,? extends R> other)
CallResult.SuccessProjection<L,R>
orElse(java.util.function.Supplier<? extends CallResult.SuccessProjection<? extends L,? extends R>> supplier)
void
orElseRun(java.util.function.Consumer<? super L> action)
Runs an action in the case this is a projection on a Failure value.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.CallResult<L,R>
toCallResult()
Returns the underlying callResult of this projection.<U> U
transform(java.util.function.Function<? super CallResult.SuccessProjection<L,R>,? extends U> f)
Transforms thisSuccessProjection
.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.github.badpop.jcoinbase.control.FunctionalValue
contains, exists, forAll, getOrElse, getOrElseThrow, getOrElseTry, getOrNull, isAsync, isLazy, isSingleValued, spliterator, toCallResult, toCallResult, toJavaOptional, toOption, toString
Methods inherited from interface java.lang.Iterable
forEach
-
Method Details
-
isEmpty
public boolean isEmpty()Description copied from interface:FunctionalValue
Checks, thisValue
is empty, i.e. if the underlying functionalValue is absent.- Specified by:
isEmpty
in interfaceFunctionalValue<L>
- Returns:
- false, if no underlying functionalValue is present, true otherwise.
-
get
Gets theSuccess
value or throws.- Specified by:
get
in interfaceFunctionalValue<L>
- Returns:
- the success value, if the underlying
CallResult
is aSuccess
- Throws:
java.util.NoSuchElementException
- if the underlyingCallResult
of thisSuccessProjection
is aFailure
-
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
Gets the Success value or an alternate value, if the projected CallResult is a Failure.- Specified by:
getOrElse
in interfaceFunctionalValue<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
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
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.ThrowableGets 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
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)ReturnsSome
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
- ifmapper
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 interfaceFunctionalValue<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
Applies the given action to the value if the projected callResult is a Success. Otherwise nothing happens.- Specified by:
peek
in interfaceFunctionalValue<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 thisSuccessProjection
.- Type Parameters:
U
- Type of transformation result- Parameters:
f
- A transformation- Returns:
- An instance of type
U
- Throws:
java.lang.NullPointerException
- iff
is null
-
iterator
Description copied from interface:FunctionalValue
Returns a richio.vavr.collection.Iterator
.- Specified by:
iterator
in interfaceFunctionalValue<L>
- Specified by:
iterator
in interfacejava.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.- Specified by:
equals
in interfaceFunctionalValue<L>
- Overrides:
equals
in classjava.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 interfaceFunctionalValue<L>
- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- The hashcode of this object
-