package org.requirementsascode;

import java.util.Optional;
import java.util.function.Predicate;

import org.requirementsascode.predicate.Anytime;

public class ConditionalStep extends Step {
    private static final long serialVersionUID = 7204738737376844201L;

    ConditionalStep(String stepName, UseCase useCase, Flow useCaseFlow) {
	super(stepName, useCase, useCaseFlow);
    }

    public Predicate<UseCaseModelRunner> getPredicate() {
	Predicate<UseCaseModelRunner> predicate;
	Predicate<UseCaseModelRunner> reactWhile = getReactWhile();

	if (reactWhile != null) {
	    predicate = reactWhile;
	} else {
	    predicate = getFlowPredicate().get();
	}

	return predicate;
    }

    public Optional<Predicate<UseCaseModelRunner>> getFlowPredicate() {
	Optional<Predicate<UseCaseModelRunner>> flowPredicate = Optional.empty();
	Optional<Predicate<UseCaseModelRunner>> flowPosition = getFlowPosition();
	Optional<Predicate<UseCaseModelRunner>> when = getWhen();

	Anytime anytime = new Anytime();
	Predicate<UseCaseModelRunner> flowPositionOrElseAnytime = flowPosition.orElse(anytime);
	Predicate<UseCaseModelRunner> whenOrElseAnytime = when.orElse(anytime);
	flowPredicate = Optional.of(isRunnerInDifferentFlow().and(flowPositionOrElseAnytime).and(whenOrElseAnytime));
	return flowPredicate;
    }

    private Predicate<UseCaseModelRunner> isRunnerInDifferentFlow() {
	Predicate<UseCaseModelRunner> isRunnerInDifferentFlow = runner -> runner.getLatestFlow()
		.map(runnerFlow -> !runnerFlow.equals(getFlow())).orElse(true);
	return isRunnerInDifferentFlow;
    }
}

@@ -756,92 +756,43 @@
  %7B%0A%09
-Optional%3CPredicate%3CUseCaseModelRunner%3E%3E flowPredicate = Optional.empty();%0A%09Optional%3C
+Anytime anytime = new Anytime();%0A%0A%09
 Pred
@@ -816,17 +816,16 @@
 lRunner%3E
-%3E
  flowPos
@@ -853,175 +853,73 @@
 on()
-;%0A%09Optional%3CPredicate%3CUseCaseModelRunner%3E%3E when = getWhen();%0A%0A%09Anytime anytime = new Anytime();%0A%09Predicate%3CUseCaseModelRunner%3E flowPositionOrElseAnytime = flowPosition
+.orElse(anytime);%0A%09Predicate%3CUseCaseModelRunner%3E when = getWhen()
 .orE
@@ -924,33 +924,43 @@
 rElse(anytime);%0A
-%09
+%0A%09Optional%3C
 Predicate%3CUseCas
@@ -976,52 +976,10 @@
 ner%3E
+%3E
  
-whenOrElseAnytime = when.orElse(anytime);%0A%09
 flow
@@ -998,16 +998,19 @@
 Optional
+%0A%09%09
 .of(isRu
@@ -1051,44 +1051,18 @@
 tion
-OrElseAnytime).and(whenOrElseAnytime
+).and(when
 ));%0A

Clean up getFlowPredicate method