Package com.koralix.stepfn
Class StepFunction<T,V,R>
java.lang.Object
com.koralix.stepfn.StepFunction<T,V,R>
- Type Parameters:
T
- the type of the input to the functionV
- the type of the internal result of the functionR
- the type of the external result of the function
- All Implemented Interfaces:
Function<T,
R>
- Direct Known Subclasses:
AsyncStepFunction
,SyncStepFunction
A
StepFunction
is a Function
that is composed of Step
s.
Example:
Step<String, Integer> step1 = new Step<>() {
@Override
public Integer apply(String input) {
return input.length();
}
@Override
public boolean isComplete() {
return true;
}
};
Step<Integer, Boolean> step2 = new Step<>() {
@Override
public Integer apply(Integer input) {
return input > 5;
}
@Override
public boolean isComplete() {
return true;
}
};
StepFunction<String, ?, Boolean> lengthCheck = new SyncStepFunction<>(step1);
lengthCheck.addTransition(
step1,
step2,
input -> true
);
lengthCheck.apply("Hello World"); // true
lengthCheck.apply("Hello"); // false
- Since:
- 1.0.0
- See Also:
-
Constructor Summary
ConstructorDescriptionStepFunction
(Step<T, ?> initialStep) Creates a newStepFunction
with the given initialStep
.StepFunction
(Step<T, ?> initialStep, Map<Step<?, ?>, Set<Transition<?, ?>>> transitions) Deprecated. -
Method Summary
Modifier and TypeMethodDescription<A> void
addTransition
(Step<?, A> from, Step<A, ?> to, Function<A, Boolean> predicate) <A,
B> void Adds aTransition
from the first givenStep
to the second givenStep
with the given predicate and mapper.protected <A,
B> void apply
(Step<A, B> step, Step<?, ?> from, A input, CompletableFuture<V> future) Recursive method that applies the givenStep
to the given input.Returns the initialStep
of thisStepFunction
.protected abstract <A,
B> Optional<CompletableFuture<B>> Computes the result of the givenStep
with the given input.protected <A,
B> Set<Transition<A, B>> transitions
(Step<?, A> step) Returns theTransition
s that transition from the givenStep
.
-
Constructor Details
-
StepFunction
@Deprecated public StepFunction(Step<T, ?> initialStep, Map<Step<?, ?>, Set<Transition<?, ?>>> transitions) Deprecated.useStepFunction(Step)
instead - this constructor will be removed in 1.2.0- Parameters:
initialStep
- the initial steptransitions
- the transitions- Since:
- 1.0.0
-
StepFunction
Creates a newStepFunction
with the given initialStep
.- Parameters:
initialStep
- the initial step- Since:
- 1.0.0
-
-
Method Details
-
addTransition
public <A,B> void addTransition(Step<?, A> from, Step<B, ?> to, Function<A, Boolean> predicate, Function<A, B> mapper) Adds aTransition
from the first givenStep
to the second givenStep
with the given predicate and mapper.- Type Parameters:
A
- the output type of the from stepB
- the input type of the to step- Parameters:
from
- the step to transition fromto
- the step to transition topredicate
- the predicate to determine if the transition is applicablemapper
- the mapper to map the input to the next step- Since:
- 1.1.0
-
addTransition
- Type Parameters:
A
- the output type of the from step- Parameters:
from
- the step to transition fromto
- the step to transition topredicate
- the predicate to determine if the transition is applicable- Since:
- 1.0.0
-
apply
Recursive method that applies the givenStep
to the given input.This method implements the logic for aggregating the inputs of the
Step
s and applying completeStep
s.After the
Step
has been applied, the method will complete theCompletableFuture
with the result of theStep
.- Type Parameters:
A
- the input type of the stepB
- the output type of the step- Parameters:
step
- the step to applyfrom
- the step that the input is coming frominput
- the inputfuture
- the future to complete- Since:
- 1.0.0
-
step
protected abstract <A,B> Optional<CompletableFuture<B>> step(Step<A, B> step, Step<?, ?> from, A input) Computes the result of the givenStep
with the given input.If the given
Step
is not complete the input is stored in theStep
for aggregation and the result is empty.- Type Parameters:
A
- the input type of the stepB
- the output type of the step- Parameters:
step
- the step to executefrom
- the step that the input is coming frominput
- the input- Returns:
- the result of the step
- Since:
- 1.0.0
-
firstStep
Returns the initialStep
of thisStepFunction
.- Returns:
- the initial step
- Since:
- 1.0.0
-
transitions
Returns theTransition
s that transition from the givenStep
.- Type Parameters:
A
- the output type of the stepB
- the output type of the transitions- Parameters:
step
- the step- Returns:
- the transitions
- Since:
- 1.0.0
-
StepFunction(Step)
instead - this constructor will be removed in 1.2.0