Class Condition<I,O>
- java.lang.Object
-
- org.obrel.core.RelatedObject
-
- de.esoco.coroutine.CoroutineStep<I,O>
-
- de.esoco.coroutine.step.Condition<I,O>
-
- All Implemented Interfaces:
de.esoco.lib.property.Fluent<CoroutineStep<I,O>>,org.obrel.core.FluentRelatable<CoroutineStep<I,O>>,org.obrel.core.Relatable
public class Condition<I,O> extends CoroutineStep<I,O>
ACoroutinestep that test a logical expression in the form of aPredicateorBiPredicateand executes certain steps (which may be a subroutines) based on the boolean result.
-
-
Constructor Summary
Constructors Constructor Description Condition(java.util.function.BiPredicate<? super I,Continuation<?>> pCondition, CoroutineStep<I,O> rRunIfTrue, CoroutineStep<I,O> rRunIfFalse)Creates a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <I,O>
Condition<I,O>doIf(java.util.function.BiPredicate<? super I,Continuation<?>> fCondition, CoroutineStep<I,O> rRunIfTrue)Tests a logical condition and executes a certain step if the condition is TRUE.static <I,O>
Condition<I,O>doIf(java.util.function.Predicate<? super I> fCondition, CoroutineStep<I,O> rRunIfTrue)Tests a logical condition and executes a certain step if the condition is TRUE.static <I,O>
Condition<I,O>doIfElse(java.util.function.BiPredicate<? super I,Continuation<?>> fCondition, CoroutineStep<I,O> rRunIfTrue, CoroutineStep<I,O> rRunIfFalse)Tests a logical condition and executes certain steps if the condition is either TRUE or FALSE.static <I,O>
Condition<I,O>doIfElse(java.util.function.Predicate<? super I> fCondition, CoroutineStep<I,O> rRunIfTrue, CoroutineStep<I,O> rRunIfFalse)Tests a logical condition and executes certain steps if the condition is either TRUE or FALSE.protected Oexecute(I input, Continuation<?> continuation)This method must be implemented by subclasses to provide the actual functionality of this step.Condition<I,O>orElse(CoroutineStep<I,O> rRunIfFalse)Returns a new instance with the condition and TRUE step of this and a certain step to execute if the condition is FALSE.voidrunAsync(java.util.concurrent.CompletableFuture<I> previousExecution, CoroutineStep<O,?> nextStep, Continuation<?> continuation)Runs this execution step asynchronously as a continuation of a previous code execution in aCompletableFutureand proceeds to the next step afterwards.-
Methods inherited from class de.esoco.coroutine.CoroutineStep
runBlocking, terminateCoroutine, toString
-
Methods inherited from class org.obrel.core.RelatedObject
deleteRelation, get, getRelation, getRelations, notifyRelationListeners, readRelations, relationsEqual, relationsHashCode, relationsString, set, set, transform, writeRelations
-
-
-
-
Constructor Detail
-
Condition
public Condition(java.util.function.BiPredicate<? super I,Continuation<?>> pCondition, CoroutineStep<I,O> rRunIfTrue, CoroutineStep<I,O> rRunIfFalse)
Creates a new instance.- Parameters:
pCondition- The condition to testrRunIfTrue- The step to run if the condition is TRUErRunIfFalse- The step to run if the condition is FALSE
-
-
Method Detail
-
doIf
public static <I,O> Condition<I,O> doIf(java.util.function.Predicate<? super I> fCondition, CoroutineStep<I,O> rRunIfTrue)
Tests a logical condition and executes a certain step if the condition is TRUE. To create a condition that also runs a step if the condition is FALSE either a subsequent call toorElse(CoroutineStep)or the alternative methoddoIfElse(Predicate, CoroutineStep, CoroutineStep)can be used. If no 'else' step is set the coroutine will terminate if the condition is not met.This variant expects a unary predicate that only receives the input value. If the
Continuationneeds to be tested too the methoddoIf(BiPredicate, CoroutineStep)can be used instead.- Parameters:
fCondition- The condition to testrRunIfTrue- The step to run if the condition is TRUE- Returns:
- The new conditional step
-
doIf
public static <I,O> Condition<I,O> doIf(java.util.function.BiPredicate<? super I,Continuation<?>> fCondition, CoroutineStep<I,O> rRunIfTrue)
Tests a logical condition and executes a certain step if the condition is TRUE. To create a condition that also runs a step if the condition is FALSE either a subsequent call toorElse(CoroutineStep)or the alternative methoddoIfElse(Predicate, CoroutineStep, CoroutineStep)can be used. If no 'else' step is set the coroutine will terminate if the condition is not met.This variant expects a binary predicate that also receives the current
Continuation. If a test of the input value is sufficient the methoddoIf(Predicate, CoroutineStep)can be used instead.- Parameters:
fCondition- The condition to testrRunIfTrue- The step to run if the condition is TRUE- Returns:
- The new conditional step
-
doIfElse
public static <I,O> Condition<I,O> doIfElse(java.util.function.BiPredicate<? super I,Continuation<?>> fCondition, CoroutineStep<I,O> rRunIfTrue, CoroutineStep<I,O> rRunIfFalse)
Tests a logical condition and executes certain steps if the condition is either TRUE or FALSE. A semantic alternative is the factory methoddoIf(BiPredicate, CoroutineStep)in conjunction with the instance methodorElse(CoroutineStep).This variant expects a binary predicate that also receives the current
Continuation. If a test of the input value is sufficient the methoddoIfElse(Predicate, CoroutineStep, CoroutineStep)can be used instead.- Parameters:
fCondition- The condition to testrRunIfTrue- The step to run if the condition is TRUErRunIfFalse- The step to run if the condition is FALSE- Returns:
- The new conditional step
-
doIfElse
public static <I,O> Condition<I,O> doIfElse(java.util.function.Predicate<? super I> fCondition, CoroutineStep<I,O> rRunIfTrue, CoroutineStep<I,O> rRunIfFalse)
Tests a logical condition and executes certain steps if the condition is either TRUE or FALSE. A semantic alternative is the factory methoddoIf(BiPredicate, CoroutineStep)in conjunction with the instance methodorElse(CoroutineStep).This variant expects a unary predicate that only receives the input value. If the
Continuationneeds to be tested too the methoddoIf(BiPredicate, CoroutineStep)can be used instead.- Parameters:
fCondition- The condition to testrRunIfTrue- The step to run if the condition is TRUErRunIfFalse- The step to run if the condition is FALSE- Returns:
- The new conditional step
-
orElse
public Condition<I,O> orElse(CoroutineStep<I,O> rRunIfFalse)
Returns a new instance with the condition and TRUE step of this and a certain step to execute if the condition is FALSE. This is just a semantic alternative todoIfElse(BiPredicate, CoroutineStep, CoroutineStep).- Parameters:
rRunIfFalse- The step to run if the condition is FALSE- Returns:
- A new conditional step
-
runAsync
public void runAsync(java.util.concurrent.CompletableFuture<I> previousExecution, CoroutineStep<O,?> nextStep, Continuation<?> continuation)
Runs this execution step asynchronously as a continuation of a previous code execution in aCompletableFutureand proceeds to the next step afterwards.Subclasses that need to suspend the invocation of the next step until some condition is met (e.g. sending or receiving data has finished) need to override this method and create a
Suspensionby invokingContinuation.suspend(CoroutineStep, CoroutineStep)on the next step. If the condition that caused the suspension resolves the coroutine execution can be resumed by callingSuspension.resume(Object).Subclasses that override this method also need to handle errors by terminating any further execution (i.e. not resuming a suspension if such exists) and forwarding the causing exception to
Continuation.fail(Throwable).- Overrides:
runAsyncin classCoroutineStep<I,O>- Parameters:
previousExecution- The future of the previous code executionnextStep- The next step to execute or NULL for nonecontinuation- The continuation of the execution
-
execute
protected O execute(I input, Continuation<?> continuation)
This method must be implemented by subclasses to provide the actual functionality of this step.- Specified by:
executein classCoroutineStep<I,O>- Parameters:
input- The input valuecontinuation- The continuation of the execution- Returns:
- The result of the execution
-
-