package org.requirementsascode.predicate;

import java.io.Serializable;
import java.util.Objects;
import java.util.function.Predicate;

import org.requirementsascode.FlowStep;
import org.requirementsascode.UseCaseModelRunner;

public class ReactWhile implements Predicate<UseCaseModelRunner>, Serializable {
    private static final long serialVersionUID = -3190093346311188647L;

    private Predicate<UseCaseModelRunner> completeCondition;
    private Predicate<UseCaseModelRunner> reactWhileCondition;

    public ReactWhile(FlowStep step, Predicate<UseCaseModelRunner> reactWhileCondition) {
	Objects.requireNonNull(step);
	Objects.requireNonNull(reactWhileCondition);
	createReactWhileCondition(step, reactWhileCondition);
    }

    @Override
    public boolean test(UseCaseModelRunner runner) {
	return completeCondition.test(runner);
    }

    private void createReactWhileCondition(FlowStep step, Predicate<UseCaseModelRunner> reactWhileCondition) {
	completeCondition = step.getPredicate().and(reactWhileCondition);
	createLoop(step);
	this.reactWhileCondition = reactWhileCondition;
    }

    private void createLoop(FlowStep step) {
	FlowPosition flowPosition = step.getFlowPosition();
	flowPosition.orAfter(step);
    }

    public Predicate<UseCaseModelRunner> getReactWhileCondition() {
	return reactWhileCondition;
    }
}

@@ -169,16 +169,52 @@
 owStep;%0A
+import org.requirementsascode.Step;%0A
 import o
@@ -284,49 +284,39 @@
 ile 
-implements Predicate%3CUseCaseModelRunner%3E,
+extends FlowPosition implements
  Ser
@@ -616,38 +616,21 @@
 ion) %7B%0A%09
-Objects.requireNonNull
+super
 (step);%0A
@@ -732,24 +732,28 @@
 ion);%0A    %7D%0A
+    
 %0A    @Overri
@@ -760,21 +760,24 @@
 de%0A    p
-ublic
+rotected
  boolean
@@ -777,21 +777,54 @@
 boolean 
-test(
+isRunnerAtRightPositionFor(Step step, 
 UseCaseM
@@ -834,17 +834,29 @@
 lRunner 
-r
+useCaseModelR
 unner) %7B
@@ -887,17 +887,29 @@
 on.test(
-r
+useCaseModelR
 unner);%0A
@@ -1405,10 +1405,12 @@
 ;%0A    %7D%0A
+%0A%0A
 %7D%0A

Make ReactWhile a subclass of FlowPosition