Skip to content

Commit dacdc70

Browse files
committed
Adding java lambda support
Signed-off-by: fjtirado <ftirados@redhat.com>
1 parent 14d25e9 commit dacdc70

File tree

14 files changed

+610
-26
lines changed

14 files changed

+610
-26
lines changed

experimental/lambda/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<parent>
4+
<groupId>io.serverlessworkflow</groupId>
5+
<artifactId>serverlessworkflow-experimental</artifactId>
6+
<version>8.0.0-SNAPSHOT</version>
7+
</parent>
8+
<artifactId>serverlessworkflow-experimental-lambda</artifactId>
9+
<name>ServelessWorkflow:: Experimental:: lambda</name>
10+
<dependencies>
11+
<dependency>
12+
<groupId>io.serverlessworkflow</groupId>
13+
<artifactId>serverlessworkflow-experimental-types</artifactId>
14+
</dependency>
15+
<dependency>
16+
<groupId>io.serverlessworkflow</groupId>
17+
<artifactId>serverlessworkflow-impl-core</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.junit.jupiter</groupId>
21+
<artifactId>junit-jupiter-api</artifactId>
22+
<scope>test</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.junit.jupiter</groupId>
26+
<artifactId>junit-jupiter-engine</artifactId>
27+
<scope>test</scope>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.junit.jupiter</groupId>
31+
<artifactId>junit-jupiter-params</artifactId>
32+
<scope>test</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.assertj</groupId>
36+
<artifactId>assertj-core</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>ch.qos.logback</groupId>
41+
<artifactId>logback-classic</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
</dependencies>
45+
</project>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.impl.expressions;
17+
18+
import io.serverlessworkflow.impl.TaskContext;
19+
import io.serverlessworkflow.impl.WorkflowContext;
20+
import io.serverlessworkflow.impl.WorkflowFilter;
21+
import io.serverlessworkflow.impl.WorkflowModel;
22+
import io.serverlessworkflow.impl.WorkflowModelFactory;
23+
import java.util.function.BiFunction;
24+
import java.util.function.BiPredicate;
25+
import java.util.function.Function;
26+
27+
public class JavaExpressionFactory implements ExpressionFactory {
28+
29+
private final WorkflowModelFactory modelFactory = new JavaModelFactory();
30+
private final Expression dummyExpression =
31+
new Expression() {
32+
@Override
33+
public WorkflowModel eval(
34+
WorkflowContext workflowContext, TaskContext context, WorkflowModel model) {
35+
return model;
36+
}
37+
};
38+
39+
@Override
40+
public Expression buildExpression(String expression) {
41+
return dummyExpression;
42+
}
43+
44+
@Override
45+
public WorkflowFilter buildFilter(String expr, Object value) {
46+
if (value instanceof Function func) {
47+
return (w, t, n) -> modelFactory.fromAny(func.apply(n.asJavaObject()));
48+
} else if (value instanceof BiPredicate pred) {
49+
return (w, t, n) -> modelFactory.from(pred.test(w, t));
50+
} else if (value instanceof BiFunction func) {
51+
return (w, t, n) -> modelFactory.fromAny(func.apply(w, t));
52+
} else if (value instanceof WorkflowFilter filter) {
53+
return filter;
54+
} else {
55+
return (w, t, n) -> modelFactory.fromAny(value);
56+
}
57+
}
58+
59+
@Override
60+
public WorkflowModelFactory modelFactory() {
61+
return modelFactory;
62+
}
63+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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.impl.expressions;
17+
18+
import io.cloudevents.CloudEventData;
19+
import io.serverlessworkflow.impl.WorkflowModel;
20+
import java.time.OffsetDateTime;
21+
import java.util.Collection;
22+
import java.util.Collections;
23+
import java.util.Map;
24+
import java.util.Optional;
25+
import java.util.function.BiConsumer;
26+
27+
public class JavaModel implements WorkflowModel {
28+
29+
private Object object;
30+
31+
static final JavaModel TrueModel = new JavaModel(Boolean.TRUE);
32+
static final JavaModel FalseModel = new JavaModel(Boolean.FALSE);
33+
static final JavaModel NullModel = new JavaModel(null);
34+
35+
JavaModel(Object object) {
36+
this.object = object;
37+
}
38+
39+
@Override
40+
public void forEach(BiConsumer<String, WorkflowModel> consumer) {
41+
asMap()
42+
.ifPresent(
43+
m ->
44+
m.forEach(
45+
(k, v) ->
46+
consumer.accept(
47+
k, v instanceof WorkflowModel model ? model : new JavaModel(v))));
48+
}
49+
50+
@Override
51+
public Optional<Boolean> asBoolean() {
52+
return object instanceof Boolean value ? Optional.of(value) : Optional.empty();
53+
}
54+
55+
@Override
56+
public Collection<WorkflowModel> asCollection() {
57+
return object instanceof Collection value
58+
? new JavaModelCollection(value)
59+
: Collections.emptyList();
60+
}
61+
62+
@Override
63+
public Optional<String> asText() {
64+
return object instanceof String value ? Optional.of(value) : Optional.empty();
65+
}
66+
67+
@Override
68+
public Optional<OffsetDateTime> asDate() {
69+
return object instanceof OffsetDateTime value ? Optional.of(value) : Optional.empty();
70+
}
71+
72+
@Override
73+
public Optional<Number> asNumber() {
74+
return object instanceof Number value ? Optional.of(value) : Optional.empty();
75+
}
76+
77+
@Override
78+
public Optional<CloudEventData> asCloudEventData() {
79+
return object instanceof CloudEventData value ? Optional.of(value) : Optional.empty();
80+
}
81+
82+
@Override
83+
public Optional<Map<String, Object>> asMap() {
84+
return object instanceof Map ? Optional.of((Map<String, Object>) object) : Optional.empty();
85+
}
86+
87+
@Override
88+
public Object asJavaObject() {
89+
return object;
90+
}
91+
92+
@Override
93+
public Object asIs() {
94+
return object;
95+
}
96+
97+
@Override
98+
public Class<?> objectClass() {
99+
return object != null ? object.getClass() : Object.class;
100+
}
101+
102+
@Override
103+
public <T> Optional<T> as(Class<T> clazz) {
104+
return object != null && object.getClass().isAssignableFrom(clazz)
105+
? Optional.of(clazz.cast(object))
106+
: Optional.empty();
107+
}
108+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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.impl.expressions;
17+
18+
import io.serverlessworkflow.impl.WorkflowModel;
19+
import io.serverlessworkflow.impl.WorkflowModelCollection;
20+
import java.util.ArrayList;
21+
import java.util.Collection;
22+
import java.util.Iterator;
23+
import java.util.Optional;
24+
25+
public class JavaModelCollection implements Collection<WorkflowModel>, WorkflowModelCollection {
26+
27+
private final Collection object;
28+
29+
JavaModelCollection() {
30+
this.object = new ArrayList<>();
31+
}
32+
33+
JavaModelCollection(Collection<?> object) {
34+
this.object = object;
35+
}
36+
37+
@Override
38+
public int size() {
39+
return object.size();
40+
}
41+
42+
@Override
43+
public boolean isEmpty() {
44+
return object.isEmpty();
45+
}
46+
47+
@Override
48+
public boolean contains(Object o) {
49+
throw new UnsupportedOperationException();
50+
}
51+
52+
private class ModelIterator implements Iterator<WorkflowModel> {
53+
54+
private Iterator<?> wrapped;
55+
56+
public ModelIterator(Iterator<?> wrapped) {
57+
this.wrapped = wrapped;
58+
}
59+
60+
@Override
61+
public boolean hasNext() {
62+
return wrapped.hasNext();
63+
}
64+
65+
@Override
66+
public WorkflowModel next() {
67+
Object obj = wrapped.next();
68+
return obj instanceof WorkflowModel value ? value : new JavaModel(obj);
69+
}
70+
}
71+
72+
@Override
73+
public Iterator<WorkflowModel> iterator() {
74+
return new ModelIterator(object.iterator());
75+
}
76+
77+
@Override
78+
public Object[] toArray() {
79+
throw new UnsupportedOperationException();
80+
}
81+
82+
@Override
83+
public <T> T[] toArray(T[] a) {
84+
throw new UnsupportedOperationException();
85+
}
86+
87+
@Override
88+
public boolean add(WorkflowModel e) {
89+
return object.add(e.asIs());
90+
}
91+
92+
@Override
93+
public boolean remove(Object o) {
94+
return object.remove(((WorkflowModel) o).asIs());
95+
}
96+
97+
@Override
98+
public boolean containsAll(Collection<?> c) {
99+
throw new UnsupportedOperationException();
100+
}
101+
102+
@Override
103+
public boolean addAll(Collection<? extends WorkflowModel> c) {
104+
int size = size();
105+
c.forEach(this::add);
106+
return size() > size;
107+
}
108+
109+
@Override
110+
public boolean removeAll(Collection<?> c) {
111+
int size = size();
112+
c.forEach(this::remove);
113+
return size() < size;
114+
}
115+
116+
@Override
117+
public boolean retainAll(Collection<?> c) {
118+
throw new UnsupportedOperationException();
119+
}
120+
121+
@Override
122+
public void clear() {
123+
object.clear();
124+
}
125+
126+
@Override
127+
public Object asJavaObject() {
128+
return object;
129+
}
130+
131+
@Override
132+
public Object asIs() {
133+
return object;
134+
}
135+
136+
@Override
137+
public Class<?> objectClass() {
138+
return object.getClass();
139+
}
140+
141+
@Override
142+
public <T> Optional<T> as(Class<T> clazz) {
143+
return object.getClass().isAssignableFrom(clazz)
144+
? Optional.of(clazz.cast(object))
145+
: Optional.empty();
146+
}
147+
}

0 commit comments

Comments
 (0)