Skip to content

(WIP) Introduce Java Fluent DSL #646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public <T> SwitchCaseFunction withPredicate(Predicate<T> predicate) {
return this;
}

public <T> void setPredicate(Predicate<T> predicate) {
this.predicate = predicate;
}

public Predicate<?> predicate() {
return predicate;
}
Expand Down
42 changes: 42 additions & 0 deletions fluent/java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-fluent</artifactId>
<version>8.0.0-SNAPSHOT</version>
</parent>

<name>Serverless Workflow :: Fluent :: Java</name>
<artifactId>serverlessworkflow-fluent-java</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-experimental-types</artifactId>
</dependency>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-types</artifactId>
</dependency>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-fluent-standard</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.fluent.java;

import io.serverlessworkflow.api.types.CallJava;
import io.serverlessworkflow.api.types.CallTaskJava;
import io.serverlessworkflow.fluent.standard.TaskBaseBuilder;
import java.util.function.Function;

public class CallTaskJavaBuilder extends TaskBaseBuilder<CallTaskJavaBuilder>
implements JavaTransformationHandlers<CallTaskJavaBuilder> {

private CallTaskJava callTaskJava;

CallTaskJavaBuilder() {
callTaskJava = new CallTaskJava(new CallJava() {});
super.setTask(callTaskJava.getCallJava());
}

@Override
protected CallTaskJavaBuilder self() {
return this;
}

public <T, V> CallTaskJavaBuilder function(Function<T, V> function) {
this.callTaskJava = new CallTaskJava(CallJava.function(function));
super.setTask(this.callTaskJava.getCallJava());
return this;
}

public CallTaskJava build() {
return this.callTaskJava;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.fluent.java;

import io.serverlessworkflow.api.types.Task;
import io.serverlessworkflow.api.types.TaskItem;
import io.serverlessworkflow.fluent.standard.BaseDoTaskBuilder;
import java.util.UUID;
import java.util.function.Consumer;

public class DoTaskJavaBuilder extends BaseDoTaskBuilder<DoTaskJavaBuilder>
implements JavaTransformationHandlers<DoTaskJavaBuilder> {

DoTaskJavaBuilder() {
super();
}

@Override
protected DoTaskJavaBuilder self() {
return this;
}

@Override
protected DoTaskJavaBuilder newDo() {
return new DoTaskJavaBuilder();
}

public DoTaskJavaBuilder callFn(String name, Consumer<CallTaskJavaBuilder> consumer) {
final CallTaskJavaBuilder callTaskJavaBuilder = new CallTaskJavaBuilder();
consumer.accept(callTaskJavaBuilder);
this.addTaskItem(new TaskItem(name, new Task().withCallTask(callTaskJavaBuilder.build())));
return this;
}

public DoTaskJavaBuilder callFn(Consumer<CallTaskJavaBuilder> consumer) {
return this.callFn(UUID.randomUUID().toString(), consumer);
}

public DoTaskJavaBuilder forEachFn(String name, Consumer<ForTaskJavaBuilder> consumer) {
final ForTaskJavaBuilder forTaskJavaBuilder = new ForTaskJavaBuilder();
consumer.accept(forTaskJavaBuilder);
this.addTaskItem(new TaskItem(name, new Task().withForTask(forTaskJavaBuilder.build())));
return this;
}

public DoTaskJavaBuilder forEachFn(Consumer<ForTaskJavaBuilder> consumer) {
return this.forEachFn(UUID.randomUUID().toString(), consumer);
}

public DoTaskJavaBuilder switchCaseFn(String name, Consumer<SwitchTaskJavaBuilder> consumer) {
final SwitchTaskJavaBuilder switchTaskJavaBuilder = new SwitchTaskJavaBuilder();
consumer.accept(switchTaskJavaBuilder);
this.addTaskItem(new TaskItem(name, new Task().withSwitchTask(switchTaskJavaBuilder.build())));
return this;
}

public DoTaskJavaBuilder switchCaseFn(Consumer<SwitchTaskJavaBuilder> consumer) {
return this.switchCaseFn(UUID.randomUUID().toString(), consumer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.fluent.java;

import io.serverlessworkflow.api.types.CallJava;
import io.serverlessworkflow.api.types.CallTaskJava;
import io.serverlessworkflow.api.types.ForTaskConfiguration;
import io.serverlessworkflow.api.types.ForTaskFunction;
import io.serverlessworkflow.api.types.Task;
import io.serverlessworkflow.api.types.TaskItem;
import io.serverlessworkflow.fluent.standard.TaskBaseBuilder;
import io.serverlessworkflow.impl.expressions.LoopFunction;
import io.serverlessworkflow.impl.expressions.LoopPredicate;
import io.serverlessworkflow.impl.expressions.LoopPredicateIndex;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Function;

public class ForTaskJavaBuilder extends TaskBaseBuilder<ForTaskJavaBuilder>
implements JavaTransformationHandlers<ForTaskJavaBuilder> {

private final ForTaskFunction forTaskFunction;
private final List<TaskItem> items;

ForTaskJavaBuilder() {
this.forTaskFunction = new ForTaskFunction();
this.forTaskFunction.withFor(new ForTaskConfiguration());
this.items = new ArrayList<>();
super.setTask(forTaskFunction);
}

@Override
protected ForTaskJavaBuilder self() {
return this;
}

public <T, V> ForTaskJavaBuilder whileCondition(LoopPredicate<T, V> predicate) {
this.forTaskFunction.withWhile(predicate);
return this;
}

public <T, V> ForTaskJavaBuilder whileCondition(LoopPredicateIndex<T, V> predicate) {
this.forTaskFunction.withWhile(predicate);
return this;
}

public <T> ForTaskJavaBuilder collection(Function<T, Collection<?>> collectionF) {
this.forTaskFunction.withCollection(collectionF);
return this;
}

public <T, V, R> ForTaskJavaBuilder doTasks(String name, LoopFunction<T, V, R> function) {
this.items.add(
new TaskItem(
name,
new Task()
.withCallTask(
new CallTaskJava(
CallJava.loopFunction(
function, this.forTaskFunction.getFor().getEach())))));
return this;
}

public <T, V, R> ForTaskJavaBuilder doTasks(LoopFunction<T, V, R> function) {
return this.doTasks(UUID.randomUUID().toString(), function);
}

public ForTaskJavaBuilder doTasks(Consumer<DoTaskJavaBuilder> consumer) {
final DoTaskJavaBuilder builder = new DoTaskJavaBuilder();
consumer.accept(builder);
this.items.addAll(builder.build().getDo());
return this;
}

public ForTaskFunction build() {
this.forTaskFunction.setDo(this.items);
return this.forTaskFunction;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.fluent.java;

@FunctionalInterface
public interface JavaForEach {
void configure(ForTaskJavaBuilder builder);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.fluent.java;

@FunctionalInterface
public interface JavaSwitchCase {
void configure(SwitchTaskJavaBuilder consumer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.fluent.java;

import io.serverlessworkflow.api.types.Export;
import io.serverlessworkflow.api.types.ExportAsFunction;
import io.serverlessworkflow.api.types.Input;
import io.serverlessworkflow.api.types.InputFromFunction;
import io.serverlessworkflow.api.types.Output;
import io.serverlessworkflow.api.types.OutputAsFunction;
import io.serverlessworkflow.fluent.standard.TransformationHandlers;
import java.util.function.Function;

public interface JavaTransformationHandlers<B extends JavaTransformationHandlers<B>>
extends TransformationHandlers {

default <T, V> B exportAsFn(Function<T, V> function) {
setExport(new Export().withAs(new ExportAsFunction().withFunction(function)));
return (B) this;
}

default <T, V> B inputFrom(Function<T, V> function) {
setInput(new Input().withFrom(new InputFromFunction().withFunction(function)));
return (B) this;
}

default <T, V> B outputAs(Function<T, V> function) {
setOutput(new Output().withAs(new OutputAsFunction().withFunction(function)));
return (B) this;
}
}
Loading
Loading