Skip to content

Commit 435130d

Browse files
authored
[NO-ISSUE] Introduce AbstractForkTaskBuilder (#1302)
* Introduce AbstractForkTaskBuilder Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com> * Apply @fjtirado suggestions Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com> * Apply @fjtirado suggestions Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com> --------- Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com>
1 parent c6e2c03 commit 435130d

3 files changed

Lines changed: 119 additions & 124 deletions

File tree

experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/FuncForkTaskBuilder.java

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,36 @@
1515
*/
1616
package io.serverlessworkflow.fluent.func;
1717

18-
import io.serverlessworkflow.api.types.DoTask;
19-
import io.serverlessworkflow.api.types.ForkTask;
20-
import io.serverlessworkflow.api.types.ForkTaskConfiguration;
2118
import io.serverlessworkflow.api.types.Task;
2219
import io.serverlessworkflow.api.types.TaskItem;
2320
import io.serverlessworkflow.api.types.func.CallJava;
2421
import io.serverlessworkflow.api.types.func.CallTaskJava;
2522
import io.serverlessworkflow.fluent.func.spi.ConditionalTaskBuilder;
2623
import io.serverlessworkflow.fluent.func.spi.FuncTaskTransformations;
27-
import io.serverlessworkflow.fluent.spec.TaskBaseBuilder;
24+
import io.serverlessworkflow.fluent.spec.AbstractForkTaskBuilder;
2825
import io.serverlessworkflow.fluent.spec.spi.ForkTaskFluent;
29-
import java.util.ArrayList;
30-
import java.util.List;
31-
import java.util.function.Consumer;
3226
import java.util.function.Function;
3327

34-
public class FuncForkTaskBuilder extends TaskBaseBuilder<FuncForkTaskBuilder>
28+
public class FuncForkTaskBuilder
29+
extends AbstractForkTaskBuilder<FuncForkTaskBuilder, FuncTaskItemListBuilder>
3530
implements FuncTaskTransformations<FuncForkTaskBuilder>,
3631
ConditionalTaskBuilder<FuncForkTaskBuilder>,
3732
ForkTaskFluent<FuncForkTaskBuilder, FuncTaskItemListBuilder> {
3833

39-
private final ForkTask forkTask;
40-
private final List<TaskItem> items;
41-
4234
FuncForkTaskBuilder() {
43-
this.forkTask = new ForkTask();
44-
this.forkTask.setFork(new ForkTaskConfiguration());
45-
this.items = new ArrayList<>();
46-
this.setTask(forkTask);
35+
super();
4736
}
4837

4938
@Override
5039
protected FuncForkTaskBuilder self() {
5140
return this;
5241
}
5342

43+
@Override
44+
protected FuncTaskItemListBuilder newTaskItemListBuilder(int listOffsetSize) {
45+
return new FuncTaskItemListBuilder(listOffsetSize);
46+
}
47+
5448
public <T, V> FuncForkTaskBuilder branch(String name, Function<T, V> function) {
5549
return branch(name, function, null);
5650
}
@@ -60,25 +54,11 @@ public <T, V> FuncForkTaskBuilder branch(
6054
return branch(name, function, argParam, null);
6155
}
6256

63-
@Override
64-
public FuncForkTaskBuilder branch(String name, Consumer<FuncTaskItemListBuilder> branchConsumer) {
65-
if (name == null || name.isBlank()) {
66-
name = "branch-" + this.items.size();
67-
}
68-
final FuncTaskItemListBuilder branchItems = new FuncTaskItemListBuilder(this.items);
69-
this.items.add(
70-
new TaskItem(name, new Task().withDoTask(new DoTask().withDo(branchItems.build()))));
71-
return this;
72-
}
73-
7457
public <T, V> FuncForkTaskBuilder branch(
7558
String name, Function<T, V> function, Class<T> argParam, Class<V> returnClass) {
76-
if (name == null || name.isBlank()) {
77-
name = "branch-" + this.items.size();
78-
}
79-
this.items.add(
59+
this.appendBranch(
8060
new TaskItem(
81-
name,
61+
this.defaultBranchName(name, this.currentOffset()),
8262
new Task()
8363
.withCallTask(
8464
new CallTaskJava(CallJava.function(function, argParam, returnClass)))));
@@ -88,24 +68,4 @@ public <T, V> FuncForkTaskBuilder branch(
8868
public <T, V> FuncForkTaskBuilder branch(Function<T, V> function) {
8969
return this.branch(null, function);
9070
}
91-
92-
@Override
93-
public FuncForkTaskBuilder branches(Consumer<FuncTaskItemListBuilder> consumer) {
94-
final FuncTaskItemListBuilder builder = new FuncTaskItemListBuilder(this.items.size());
95-
consumer.accept(builder);
96-
this.items.addAll(builder.build());
97-
return this;
98-
}
99-
100-
@Override
101-
public FuncForkTaskBuilder compete(boolean compete) {
102-
this.forkTask.getFork().setCompete(compete);
103-
return this;
104-
}
105-
106-
@Override
107-
public ForkTask build() {
108-
this.forkTask.getFork().setBranches(this.items);
109-
return forkTask;
110-
}
11171
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.fluent.spec;
17+
18+
import io.serverlessworkflow.api.types.DoTask;
19+
import io.serverlessworkflow.api.types.ForkTask;
20+
import io.serverlessworkflow.api.types.ForkTaskConfiguration;
21+
import io.serverlessworkflow.api.types.Task;
22+
import io.serverlessworkflow.api.types.TaskItem;
23+
import io.serverlessworkflow.fluent.spec.spi.ForkTaskFluent;
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
import java.util.Objects;
27+
import java.util.function.Consumer;
28+
29+
public abstract class AbstractForkTaskBuilder<
30+
SELF extends AbstractForkTaskBuilder<SELF, L>, L extends BaseTaskItemListBuilder<L>>
31+
extends TaskBaseBuilder<SELF> implements ForkTaskFluent<SELF, L> {
32+
33+
protected final ForkTask forkTask;
34+
protected final ForkTaskConfiguration forkTaskConfiguration;
35+
36+
protected AbstractForkTaskBuilder() {
37+
this.forkTask = new ForkTask();
38+
this.forkTaskConfiguration = new ForkTaskConfiguration();
39+
super.setTask(this.forkTask);
40+
}
41+
42+
protected abstract L newTaskItemListBuilder(int listOffsetSize);
43+
44+
protected String defaultBranchName(String name, int currentOffset) {
45+
return name == null || name.isBlank() ? "branch-" + currentOffset : name;
46+
}
47+
48+
protected TaskItem createDoBranchTaskItem(String name, List<TaskItem> items) {
49+
return new TaskItem(name, new Task().withDoTask(new DoTask().withDo(items)));
50+
}
51+
52+
protected int currentOffset() {
53+
List<TaskItem> existingBranches = this.forkTaskConfiguration.getBranches();
54+
return existingBranches == null ? 0 : existingBranches.size();
55+
}
56+
57+
protected void appendBranches(List<TaskItem> newBranches) {
58+
List<TaskItem> existingBranches = this.forkTaskConfiguration.getBranches();
59+
if (existingBranches == null || existingBranches.isEmpty()) {
60+
this.forkTaskConfiguration.setBranches(newBranches);
61+
} else {
62+
List<TaskItem> merged = new ArrayList<>(existingBranches);
63+
merged.addAll(newBranches);
64+
this.forkTaskConfiguration.setBranches(merged);
65+
}
66+
}
67+
68+
protected void appendBranch(TaskItem branch) {
69+
appendBranches(List.of(branch));
70+
}
71+
72+
@Override
73+
public SELF compete(final boolean compete) {
74+
this.forkTaskConfiguration.setCompete(compete);
75+
return self();
76+
}
77+
78+
@Override
79+
public SELF branches(Consumer<L> branchesConsumer) {
80+
Objects.requireNonNull(branchesConsumer, "branches consumer must not be null");
81+
82+
final L builder = newTaskItemListBuilder(currentOffset());
83+
branchesConsumer.accept(builder);
84+
appendBranches(builder.build());
85+
86+
return self();
87+
}
88+
89+
@Override
90+
public SELF branch(String name, Consumer<L> branchConsumer) {
91+
Objects.requireNonNull(branchConsumer, "branch consumer must not be null");
92+
93+
String branchName = defaultBranchName(name, currentOffset());
94+
L branchItems = newTaskItemListBuilder(0);
95+
branchConsumer.accept(branchItems);
96+
97+
appendBranch(createDoBranchTaskItem(branchName, branchItems.build()));
98+
return self();
99+
}
100+
101+
@Override
102+
public ForkTask build() {
103+
return this.forkTask.withFork(this.forkTaskConfiguration);
104+
}
105+
}

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/ForkTaskBuilder.java

Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -15,88 +15,18 @@
1515
*/
1616
package io.serverlessworkflow.fluent.spec;
1717

18-
import io.serverlessworkflow.api.types.DoTask;
19-
import io.serverlessworkflow.api.types.ForkTask;
20-
import io.serverlessworkflow.api.types.ForkTaskConfiguration;
21-
import io.serverlessworkflow.api.types.Task;
22-
import io.serverlessworkflow.api.types.TaskItem;
2318
import io.serverlessworkflow.fluent.spec.spi.ForkTaskFluent;
24-
import java.util.ArrayList;
25-
import java.util.List;
26-
import java.util.Objects;
27-
import java.util.function.Consumer;
2819

29-
public class ForkTaskBuilder extends TaskBaseBuilder<ForkTaskBuilder>
20+
public class ForkTaskBuilder extends AbstractForkTaskBuilder<ForkTaskBuilder, TaskItemListBuilder>
3021
implements ForkTaskFluent<ForkTaskBuilder, TaskItemListBuilder> {
3122

32-
private final ForkTask forkTask;
33-
private final ForkTaskConfiguration forkTaskConfiguration;
34-
35-
ForkTaskBuilder() {
36-
this.forkTask = new ForkTask();
37-
this.forkTaskConfiguration = new ForkTaskConfiguration();
38-
super.setTask(this.forkTask);
39-
}
40-
4123
@Override
4224
protected ForkTaskBuilder self() {
4325
return this;
4426
}
4527

4628
@Override
47-
public ForkTaskBuilder compete(final boolean compete) {
48-
this.forkTaskConfiguration.setCompete(compete);
49-
return this;
50-
}
51-
52-
@Override
53-
public ForkTaskBuilder branches(Consumer<TaskItemListBuilder> branchesConsumer) {
54-
List<TaskItem> existingBranches = this.forkTaskConfiguration.getBranches();
55-
56-
int currentOffset = (existingBranches == null) ? 0 : existingBranches.size();
57-
58-
final TaskItemListBuilder doTaskBuilder = new TaskItemListBuilder(currentOffset);
59-
branchesConsumer.accept(doTaskBuilder);
60-
61-
List<TaskItem> newBranches = doTaskBuilder.build();
62-
if (existingBranches == null || existingBranches.isEmpty()) {
63-
this.forkTaskConfiguration.setBranches(newBranches);
64-
} else {
65-
List<TaskItem> merged = new ArrayList<>(existingBranches);
66-
merged.addAll(newBranches);
67-
this.forkTaskConfiguration.setBranches(merged);
68-
}
69-
70-
return this;
71-
}
72-
73-
@Override
74-
public ForkTaskBuilder branch(String name, Consumer<TaskItemListBuilder> branchConsumer) {
75-
Objects.requireNonNull(branchConsumer, "Branch consumer must not be null");
76-
77-
List<TaskItem> existingBranches = this.forkTaskConfiguration.getBranches();
78-
int currentOffset = (existingBranches == null) ? 0 : existingBranches.size();
79-
String branchName = (name == null || name.isBlank()) ? "branch-" + currentOffset : name;
80-
81-
TaskItemListBuilder branchItems = new TaskItemListBuilder(0);
82-
branchConsumer.accept(branchItems);
83-
84-
TaskItem branchItem =
85-
new TaskItem(branchName, new Task().withDoTask(new DoTask().withDo(branchItems.build())));
86-
87-
if (existingBranches == null || existingBranches.isEmpty()) {
88-
this.forkTaskConfiguration.setBranches(List.of(branchItem));
89-
} else {
90-
List<TaskItem> merged = new ArrayList<>(existingBranches);
91-
merged.add(branchItem);
92-
this.forkTaskConfiguration.setBranches(merged);
93-
}
94-
95-
return this;
96-
}
97-
98-
@Override
99-
public ForkTask build() {
100-
return this.forkTask.withFork(this.forkTaskConfiguration);
29+
protected TaskItemListBuilder newTaskItemListBuilder(int listOffsetSize) {
30+
return new TaskItemListBuilder(listOffsetSize);
10131
}
10232
}

0 commit comments

Comments
 (0)