Skip to content

Commit a1db3cb

Browse files
committed
Introduce Java Fluent DSL
Signed-off-by: Ricardo Zanini <[email protected]>
1 parent f08c840 commit a1db3cb

File tree

24 files changed

+1187
-204
lines changed

24 files changed

+1187
-204
lines changed

experimental/types/src/main/java/io/serverlessworkflow/api/types/SwitchCaseFunction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public <T> SwitchCaseFunction withPredicate(Predicate<T> predicate) {
2727
return this;
2828
}
2929

30+
public <T> void setPredicate(Predicate<T> predicate) {
31+
this.predicate = predicate;
32+
}
33+
3034
public Predicate<?> predicate() {
3135
return predicate;
3236
}

fluent/java/pom.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>io.serverlessworkflow</groupId>
8+
<artifactId>serverlessworkflow-fluent</artifactId>
9+
<version>8.0.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<name>Serverless Workflow :: Fluent :: Java</name>
13+
<artifactId>serverlessworkflow-fluent-java</artifactId>
14+
15+
<properties>
16+
<maven.compiler.source>17</maven.compiler.source>
17+
<maven.compiler.target>17</maven.compiler.target>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>io.serverlessworkflow</groupId>
24+
<artifactId>serverlessworkflow-experimental-types</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>io.serverlessworkflow</groupId>
28+
<artifactId>serverlessworkflow-types</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>io.serverlessworkflow</groupId>
32+
<artifactId>serverlessworkflow-fluent-standard</artifactId>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.junit.jupiter</groupId>
37+
<artifactId>junit-jupiter-api</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
</dependencies>
41+
42+
</project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.java;
17+
18+
import io.serverlessworkflow.api.types.CallJava;
19+
import io.serverlessworkflow.api.types.CallTaskJava;
20+
import io.serverlessworkflow.fluent.standard.TaskBaseBuilder;
21+
import java.util.function.Function;
22+
23+
public class CallTaskJavaBuilder extends TaskBaseBuilder<CallTaskJavaBuilder>
24+
implements JavaTransformationHandlers<CallTaskJavaBuilder> {
25+
26+
private CallTaskJava callTaskJava;
27+
28+
CallTaskJavaBuilder() {
29+
callTaskJava = new CallTaskJava(new CallJava() {});
30+
super.setTask(callTaskJava.getCallJava());
31+
}
32+
33+
@Override
34+
protected CallTaskJavaBuilder self() {
35+
return this;
36+
}
37+
38+
public <T, V> CallTaskJavaBuilder function(Function<T, V> function) {
39+
this.callTaskJava = new CallTaskJava(CallJava.function(function));
40+
super.setTask(this.callTaskJava.getCallJava());
41+
return this;
42+
}
43+
44+
public CallTaskJava build() {
45+
return this.callTaskJava;
46+
}
47+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.java;
17+
18+
import io.serverlessworkflow.api.types.Task;
19+
import io.serverlessworkflow.api.types.TaskItem;
20+
import io.serverlessworkflow.fluent.standard.BaseDoTaskBuilder;
21+
import java.util.UUID;
22+
import java.util.function.Consumer;
23+
24+
public class DoTaskJavaBuilder extends BaseDoTaskBuilder<DoTaskJavaBuilder>
25+
implements JavaTransformationHandlers<DoTaskJavaBuilder> {
26+
27+
DoTaskJavaBuilder() {
28+
super();
29+
}
30+
31+
@Override
32+
protected DoTaskJavaBuilder self() {
33+
return this;
34+
}
35+
36+
@Override
37+
protected DoTaskJavaBuilder newDo() {
38+
return new DoTaskJavaBuilder();
39+
}
40+
41+
public DoTaskJavaBuilder callFn(String name, Consumer<CallTaskJavaBuilder> consumer) {
42+
final CallTaskJavaBuilder callTaskJavaBuilder = new CallTaskJavaBuilder();
43+
consumer.accept(callTaskJavaBuilder);
44+
this.addTaskItem(new TaskItem(name, new Task().withCallTask(callTaskJavaBuilder.build())));
45+
return this;
46+
}
47+
48+
public DoTaskJavaBuilder callFn(Consumer<CallTaskJavaBuilder> consumer) {
49+
return this.callFn(UUID.randomUUID().toString(), consumer);
50+
}
51+
52+
public DoTaskJavaBuilder forEachFn(String name, Consumer<ForTaskJavaBuilder> consumer) {
53+
final ForTaskJavaBuilder forTaskJavaBuilder = new ForTaskJavaBuilder();
54+
consumer.accept(forTaskJavaBuilder);
55+
this.addTaskItem(new TaskItem(name, new Task().withForTask(forTaskJavaBuilder.build())));
56+
return this;
57+
}
58+
59+
public DoTaskJavaBuilder forEachFn(Consumer<ForTaskJavaBuilder> consumer) {
60+
return this.forEachFn(UUID.randomUUID().toString(), consumer);
61+
}
62+
63+
public DoTaskJavaBuilder switchCaseFn(String name, Consumer<SwitchTaskJavaBuilder> consumer) {
64+
final SwitchTaskJavaBuilder switchTaskJavaBuilder = new SwitchTaskJavaBuilder();
65+
consumer.accept(switchTaskJavaBuilder);
66+
this.addTaskItem(new TaskItem(name, new Task().withSwitchTask(switchTaskJavaBuilder.build())));
67+
return this;
68+
}
69+
70+
public DoTaskJavaBuilder switchCaseFn(Consumer<SwitchTaskJavaBuilder> consumer) {
71+
return this.switchCaseFn(UUID.randomUUID().toString(), consumer);
72+
}
73+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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.java;
17+
18+
import io.serverlessworkflow.api.types.CallJava;
19+
import io.serverlessworkflow.api.types.CallTaskJava;
20+
import io.serverlessworkflow.api.types.ForTaskConfiguration;
21+
import io.serverlessworkflow.api.types.ForTaskFunction;
22+
import io.serverlessworkflow.api.types.Task;
23+
import io.serverlessworkflow.api.types.TaskItem;
24+
import io.serverlessworkflow.fluent.standard.TaskBaseBuilder;
25+
import io.serverlessworkflow.impl.expressions.LoopFunction;
26+
import io.serverlessworkflow.impl.expressions.LoopPredicate;
27+
import io.serverlessworkflow.impl.expressions.LoopPredicateIndex;
28+
import java.util.ArrayList;
29+
import java.util.Collection;
30+
import java.util.List;
31+
import java.util.UUID;
32+
import java.util.function.Consumer;
33+
import java.util.function.Function;
34+
35+
public class ForTaskJavaBuilder extends TaskBaseBuilder<ForTaskJavaBuilder>
36+
implements JavaTransformationHandlers<ForTaskJavaBuilder> {
37+
38+
private final ForTaskFunction forTaskFunction;
39+
private final List<TaskItem> items;
40+
41+
ForTaskJavaBuilder() {
42+
this.forTaskFunction = new ForTaskFunction();
43+
this.forTaskFunction.withFor(new ForTaskConfiguration());
44+
this.items = new ArrayList<>();
45+
super.setTask(forTaskFunction);
46+
}
47+
48+
@Override
49+
protected ForTaskJavaBuilder self() {
50+
return this;
51+
}
52+
53+
public <T, V> ForTaskJavaBuilder whileCondition(LoopPredicate<T, V> predicate) {
54+
this.forTaskFunction.withWhile(predicate);
55+
return this;
56+
}
57+
58+
public <T, V> ForTaskJavaBuilder whileCondition(LoopPredicateIndex<T, V> predicate) {
59+
this.forTaskFunction.withWhile(predicate);
60+
return this;
61+
}
62+
63+
public <T> ForTaskJavaBuilder collection(Function<T, Collection<?>> collectionF) {
64+
this.forTaskFunction.withCollection(collectionF);
65+
return this;
66+
}
67+
68+
public <T, V, R> ForTaskJavaBuilder doTasks(String name, LoopFunction<T, V, R> function) {
69+
this.items.add(
70+
new TaskItem(
71+
name,
72+
new Task()
73+
.withCallTask(
74+
new CallTaskJava(
75+
CallJava.loopFunction(
76+
function, this.forTaskFunction.getFor().getEach())))));
77+
return this;
78+
}
79+
80+
public <T, V, R> ForTaskJavaBuilder doTasks(LoopFunction<T, V, R> function) {
81+
return this.doTasks(UUID.randomUUID().toString(), function);
82+
}
83+
84+
public ForTaskJavaBuilder doTasks(Consumer<DoTaskJavaBuilder> consumer) {
85+
final DoTaskJavaBuilder builder = new DoTaskJavaBuilder();
86+
consumer.accept(builder);
87+
this.items.addAll(builder.build().getDo());
88+
return this;
89+
}
90+
91+
public ForTaskFunction build() {
92+
this.forTaskFunction.setDo(this.items);
93+
return this.forTaskFunction;
94+
}
95+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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.java;
17+
18+
@FunctionalInterface
19+
public interface JavaForEach {
20+
void configure(ForTaskJavaBuilder builder);
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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.java;
17+
18+
@FunctionalInterface
19+
public interface JavaSwitchCase {
20+
void configure(SwitchTaskJavaBuilder consumer);
21+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.java;
17+
18+
import io.serverlessworkflow.api.types.Export;
19+
import io.serverlessworkflow.api.types.ExportAsFunction;
20+
import io.serverlessworkflow.api.types.Input;
21+
import io.serverlessworkflow.api.types.InputFromFunction;
22+
import io.serverlessworkflow.api.types.Output;
23+
import io.serverlessworkflow.api.types.OutputAsFunction;
24+
import io.serverlessworkflow.fluent.standard.TransformationHandlers;
25+
import java.util.function.Function;
26+
27+
public interface JavaTransformationHandlers<B extends JavaTransformationHandlers<B>>
28+
extends TransformationHandlers {
29+
30+
default <T, V> B exportAsFn(Function<T, V> function) {
31+
setExport(new Export().withAs(new ExportAsFunction().withFunction(function)));
32+
return (B) this;
33+
}
34+
35+
default <T, V> B inputFrom(Function<T, V> function) {
36+
setInput(new Input().withFrom(new InputFromFunction().withFunction(function)));
37+
return (B) this;
38+
}
39+
40+
default <T, V> B outputAs(Function<T, V> function) {
41+
setOutput(new Output().withAs(new OutputAsFunction().withFunction(function)));
42+
return (B) this;
43+
}
44+
}

0 commit comments

Comments
 (0)