Skip to content

Commit

Permalink
Remove FlowPredicate from FlowPart
Browse files Browse the repository at this point in the history
Preparatory refactor for #2
  • Loading branch information
bertilmuth committed May 11, 2017
1 parent c92f877 commit 3a98225
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 55 deletions.
Expand Up @@ -19,13 +19,11 @@ public class FlowPart {
private Flow flow;
private UseCase useCase;
private UseCasePart useCasePart;
private FlowPredicate flowPredicate;

FlowPart(Flow flow, UseCasePart useCasePart) {
this.flow = flow;
this.useCasePart = useCasePart;
this.useCase = useCasePart.useCase();
this.flowPredicate = new FlowPredicate();
}

/**
Expand All @@ -37,19 +35,10 @@ public class FlowPart {
*/
public StepPart step(String stepName) {
Step useCaseStep =
useCasePart.useCase().newStep(stepName, flow, Optional.empty(), getFlowPredicate());
useCasePart.useCase().newStep(stepName, flow, Optional.empty());
return new StepPart(useCaseStep, this);
}

/**
* Returns the predicate for this flow (that will become the predicate of its first step).
*
* @return the flow's predicate
*/
Optional<Predicate<UseCaseModelRunner>> getFlowPredicate() {
return flowPredicate.get();
}

/**
* Starts the flow after the specified step has been run, in this flow's use case. You should use
* after to handle exceptions that occured in the specified step.
Expand All @@ -60,7 +49,7 @@ Optional<Predicate<UseCaseModelRunner>> getFlowPredicate() {
*/
public FlowPart after(String stepName) {
Step step = useCase.findStep(stepName);
flowPredicate.setFlowPosition(new After(Optional.of(step)));
flow.setFlowPosition(new After(Optional.of(step)));
return this;
}

Expand All @@ -73,7 +62,7 @@ public FlowPart after(String stepName) {
*/
public FlowPart insteadOf(String stepName) {
Step step = useCase.findStep(stepName);
flowPredicate.setFlowPosition(new InsteadOf(step));
flow.setFlowPosition(new InsteadOf(step));
return this;
}

Expand All @@ -87,7 +76,7 @@ public FlowPart insteadOf(String stepName) {
public FlowPart when(Predicate<UseCaseModelRunner> whenPredicate) {
Objects.requireNonNull(whenPredicate);

flowPredicate.setWhen(whenPredicate);
flow.setWhen(whenPredicate);
return this;
}

Expand All @@ -102,33 +91,4 @@ UseCasePart getUseCasePart() {
UseCaseModelBuilder getUseCaseModelBuilder() {
return useCasePart.useCaseModelBuilder();
}

private class FlowPredicate {
private Optional<Predicate<UseCaseModelRunner>> predicate;

private FlowPredicate() {
this.predicate = Optional.empty();
}

public void setFlowPosition(Predicate<UseCaseModelRunner> stepPredicate) {
flow.setFlowPosition(stepPredicate);
predicate = Optional.of(stepPredicate);
}

public void setWhen(Predicate<UseCaseModelRunner> whenPredicate) {
flow.setWhen(whenPredicate);
predicate = Optional.of(predicate.orElse(r -> true).and(whenPredicate));
}

public Optional<Predicate<UseCaseModelRunner>> get() {
return predicate.map(pred -> isRunnerInDifferentFlow().and(pred));
}

private Predicate<UseCaseModelRunner> isRunnerInDifferentFlow() {
Predicate<UseCaseModelRunner> isRunnerInDifferentFlow =
runner ->
runner.getLatestFlow().map(runnerFlow -> !flow.equals(runnerFlow)).orElse(true);
return isRunnerInDifferentFlow;
}
}
}
Expand Up @@ -43,7 +43,7 @@ public StepPart step(String stepName) {
Step nextUseCaseStepInFlow =
useCaseFlow
.getUseCase()
.newStep(stepName, useCaseFlow, Optional.of(step), Optional.empty());
.newStep(stepName, useCaseFlow, Optional.of(step));

return new StepPart(nextUseCaseStepInFlow, useCaseFlowPart);
}
Expand Down
Expand Up @@ -10,7 +10,6 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;

import org.requirementsascode.exception.ElementAlreadyInModel;
import org.requirementsascode.exception.NoSuchElementInModel;
Expand Down Expand Up @@ -99,17 +98,9 @@ Flow newFlow(String flowName) {
* @param stepName the name of the step
* @param flow the flow the step shall belong to
* @param previousStep the previous step in the flow, if there is one
* @param optionalPredicate the complete predicate of the step. If empty, the default predicate
* is: after previous step, unless interrupted by other flow with specified condition (e.g
* flow with "insteadOf" condition).
* @return the newly created step
*/
Step newStep(
String stepName,
Flow flow,
Optional<Step> previousStep,
Optional<Predicate<UseCaseModelRunner>> optionalPredicate) {

Step newStep(String stepName, Flow flow, Optional<Step> previousStep) {
Step step = new Step(stepName, flow, previousStep);
saveModelElement(step, nameToStepMap);

Expand Down

0 comments on commit 3a98225

Please sign in to comment.