package org.requirementsascode;

import java.io.Serializable;
import java.util.Optional;

import org.requirementsascode.predicate.FlowPosition;

/**
 * @author b_muth
 */
public abstract class FlowStep extends Step implements Serializable {
    private static final long serialVersionUID = -2926490717985964131L;

    private Flow flow;
    private FlowPosition flowPosition;

    private FlowStep previousStepInFlow;

    FlowStep(String stepName, UseCase useCase, Flow useCaseFlow) {
	super(stepName, useCase);
	this.flow = useCaseFlow;
    }

    public Flow getFlow() {
	return flow;
    }

    public Optional<FlowStep> getPreviousStepInFlow() {
	return Optional.ofNullable(previousStepInFlow);
    }

    protected void setPreviousStepInFlow(FlowStep previousStepInFlow) {
	this.previousStepInFlow = previousStepInFlow;
    }

    void setFlowPosition(FlowPosition flowPosition) {
	this.flowPosition = flowPosition;
    }

    public FlowPosition getFlowPosition() {
	return flowPosition;
    }

    public void includeUseCase(UseCase includedUseCase) {
	for (Flow includedFlow : includedUseCase.getFlows()) {
	    includeFlow(includedFlow);
	}
    }

    private void includeFlow(Flow includedFlow) {
	Optional<FlowStep> optionalFirstStepOfIncludedFlow = includedFlow.getFirstStep();
	optionalFirstStepOfIncludedFlow.ifPresent(this::afterThisStepComesIncludedFlow);
    }

    private void afterThisStepComesIncludedFlow(FlowStep firstStepOfIncludedFlow) {
	FlowPosition includedFlowPosition = firstStepOfIncludedFlow.getFlowPosition();
	firstStepOfIncludedFlow.setFlowPosition(includedFlowPosition.orAfter(this));
    }
}

@@ -55,16 +55,42 @@
 izable;%0A
+import java.util.Objects;%0A
 import j
@@ -734,18 +734,8 @@
     
-protected 
 void
@@ -836,24 +836,101 @@
 nFlow;%0A    %7D
+%0A    %0A    public FlowPosition getFlowPosition() %7B%0A%09return flowPosition;%0A    %7D
 %0A%0A    void s
@@ -969,24 +969,65 @@
 Position) %7B%0A
+%09Objects.requireNonNull(flowPosition);%0A%09%0A
 %09this.flowPo
@@ -1060,81 +1060,8 @@
  %7D%0A%0A
-    public FlowPosition getFlowPosition() %7B%0A%09return flowPosition;%0A    %7D%0A%0A
     
@@ -1114,16 +1114,57 @@
 Case) %7B%0A
+%09Objects.requireNonNull(flowPosition);%0A%09%0A
 %09for (Fl

Clean up FlowStep