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 function
V - the type of the internal result of the function
R - the type of the external result of the function
All Implemented Interfaces:
Function<T,R>
Direct Known Subclasses:
AsyncStepFunction, SyncStepFunction

public abstract class StepFunction<T,V,R> extends Object implements Function<T,R>
A StepFunction is a Function that is composed of Steps.

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 Details

    • StepFunction

      @Deprecated public StepFunction(Step<T,?> initialStep, Map<Step<?,?>,Set<Transition<?,?>>> transitions)
      Deprecated.
      use StepFunction(Step) instead - this constructor will be removed in 1.2.0
      Creates a new StepFunction with the given initial Step and Transitions.
      Parameters:
      initialStep - the initial step
      transitions - the transitions
      Since:
      1.0.0
    • StepFunction

      public StepFunction(Step<T,?> initialStep)
      Creates a new StepFunction with the given initial Step.
      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 a Transition from the first given Step to the second given Step with the given predicate and mapper.
      Type Parameters:
      A - the output type of the from step
      B - the input type of the to step
      Parameters:
      from - the step to transition from
      to - the step to transition to
      predicate - the predicate to determine if the transition is applicable
      mapper - the mapper to map the input to the next step
      Since:
      1.1.0
    • addTransition

      public <A> void addTransition(Step<?,A> from, Step<A,?> to, Function<A,Boolean> predicate)
      Adds a Transition from the first given Step to the second given Step with the given predicate.
      Type Parameters:
      A - the output type of the from step
      Parameters:
      from - the step to transition from
      to - the step to transition to
      predicate - the predicate to determine if the transition is applicable
      Since:
      1.0.0
    • apply

      protected <A, B> void apply(Step<A,B> step, Step<?,?> from, A input, CompletableFuture<V> future)
      Recursive method that applies the given Step to the given input.

      This method implements the logic for aggregating the inputs of the Steps and applying complete Steps.

      After the Step has been applied, the method will complete the CompletableFuture with the result of the Step.

      Type Parameters:
      A - the input type of the step
      B - the output type of the step
      Parameters:
      step - the step to apply
      from - the step that the input is coming from
      input - the input
      future - 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 given Step with the given input.

      If the given Step is not complete the input is stored in the Step for aggregation and the result is empty.

      Type Parameters:
      A - the input type of the step
      B - the output type of the step
      Parameters:
      step - the step to execute
      from - the step that the input is coming from
      input - the input
      Returns:
      the result of the step
      Since:
      1.0.0
    • firstStep

      protected Step<T,?> firstStep()
      Returns the initial Step of this StepFunction.
      Returns:
      the initial step
      Since:
      1.0.0
    • transitions

      protected <A, B> Set<Transition<A,B>> transitions(Step<?,A> step)
      Returns the Transitions that transition from the given Step.
      Type Parameters:
      A - the output type of the step
      B - the output type of the transitions
      Parameters:
      step - the step
      Returns:
      the transitions
      Since:
      1.0.0