Skip to content

Commit d1ae65c

Browse files
authored
Merge pull request #4 from h3nrique/main
Refactoring class to avoid casts and raw use
2 parents 66e0f1d + 41b2180 commit d1ae65c

File tree

5 files changed

+162
-88
lines changed

5 files changed

+162
-88
lines changed

pom.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,13 @@
1414
<project.encoding>UTF-8</project.encoding>
1515
<caffeine.version>3.1.8</caffeine.version>
1616
<junit.version>4.13.2</junit.version>
17-
<lombok.version>1.18.26</lombok.version>
1817
<mysql-connector.version>8.0.33</mysql-connector.version>
1918
<okhttp.version>4.10.0</okhttp.version>
2019
<protobuf.version>3.22.2</protobuf.version>
2120
<testcontainers.version>1.17.6</testcontainers.version>
2221
</properties>
2322

2423
<dependencies>
25-
<dependency>
26-
<groupId>org.projectlombok</groupId>
27-
<artifactId>lombok</artifactId>
28-
<version>${lombok.version}</version>
29-
</dependency>
30-
3124
<dependency>
3225
<groupId>org.slf4j</groupId>
3326
<artifactId>slf4j-api</artifactId>
Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
11
package io.eigr.spawn.api;
22

3-
import lombok.AllArgsConstructor;
4-
import lombok.Builder;
5-
import lombok.Getter;
6-
import lombok.NoArgsConstructor;
7-
83
import java.time.Duration;
94
import java.time.LocalDateTime;
105
import java.time.temporal.ChronoUnit;
116
import java.util.Optional;
127

13-
@Builder
14-
@Getter
15-
@AllArgsConstructor
16-
@NoArgsConstructor
178
public class InvocationOpts {
189

19-
@Builder.Default
20-
private boolean async = false;
10+
private final boolean async;
11+
private final Duration timeoutSeconds;
12+
private final Optional<Long> delaySeconds;
13+
private final Optional<LocalDateTime> scheduledTo;
14+
15+
private InvocationOpts(InvocationOptsBuilder invocationOptsBuilder) {
16+
this.async = invocationOptsBuilder.async;
17+
this.timeoutSeconds = invocationOptsBuilder.timeoutSeconds;
18+
this.delaySeconds = invocationOptsBuilder.delaySeconds;
19+
this.scheduledTo = invocationOptsBuilder.scheduledTo;
20+
}
21+
22+
public static InvocationOptsBuilder builder() {
23+
return new InvocationOptsBuilder();
24+
}
25+
26+
public boolean isAsync() {
27+
return async;
28+
}
2129

22-
@Builder.Default
23-
private Duration timeoutSeconds = Duration.ofSeconds(10);
30+
public Duration getTimeoutSeconds() {
31+
return timeoutSeconds;
32+
}
2433

25-
@Builder.Default
26-
private Optional<Long> delaySeconds = Optional.empty();
34+
public Optional<Long> getDelaySeconds() {
35+
return delaySeconds;
36+
}
2737

28-
@Builder.Default
29-
private Optional<LocalDateTime> scheduledTo = Optional.empty();
38+
public Optional<LocalDateTime> getScheduledTo() {
39+
return scheduledTo;
40+
}
3041

3142
public long getScheduleTimeInLong() {
3243
if (scheduledTo.isPresent()) {
@@ -40,4 +51,36 @@ public long getScheduleTimeInLong() {
4051
public long getTimeout() {
4152
return this.timeoutSeconds.toMillis();
4253
}
54+
55+
public static final class InvocationOptsBuilder {
56+
57+
private boolean async = false;
58+
private Duration timeoutSeconds = Duration.ofSeconds(10);
59+
private Optional<Long> delaySeconds = Optional.empty();
60+
private Optional<LocalDateTime> scheduledTo = Optional.empty();
61+
62+
public InvocationOpts build() {
63+
return new InvocationOpts(this);
64+
}
65+
66+
public InvocationOptsBuilder async(boolean async) {
67+
this.async = async;
68+
return this;
69+
}
70+
71+
public InvocationOptsBuilder timeoutSeconds(Duration timeoutSeconds) {
72+
this.timeoutSeconds = timeoutSeconds;
73+
return this;
74+
}
75+
76+
public InvocationOptsBuilder delaySeconds(Optional<Long> delaySeconds) {
77+
this.delaySeconds = delaySeconds;
78+
return this;
79+
}
80+
81+
public InvocationOptsBuilder scheduledTo(Optional<LocalDateTime> scheduledTo) {
82+
this.scheduledTo = scheduledTo;
83+
return this;
84+
}
85+
}
4386
}
Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,83 @@
11
package io.eigr.spawn.api;
22

3-
import lombok.*;
4-
53
import java.util.concurrent.Executor;
64
import java.util.concurrent.Executors;
75

8-
@Getter
9-
@Builder
10-
@NoArgsConstructor
11-
@AllArgsConstructor
12-
@ToString
136
public class TransportOpts {
147

15-
@Builder.Default
16-
private String host = "127.0.0.1";
17-
@Builder.Default
18-
private int port = 8091;
19-
@Builder.Default
20-
private String proxyHost = "127.0.0.1";
21-
@Builder.Default
22-
private int proxyPort = 9001;
23-
@Builder.Default
24-
private Executor executor = Executors.newCachedThreadPool();
8+
private String host;
9+
private int port;
10+
private String proxyHost;
11+
private int proxyPort;
12+
private Executor executor;
13+
14+
private TransportOpts(TransportOptsBuilder builder) {
15+
this.host = builder.host;
16+
this.port = builder.port;
17+
this.proxyHost = builder.proxyHost;
18+
this.proxyPort = builder.proxyPort;
19+
this.executor = builder.executor;
20+
}
21+
22+
public static TransportOptsBuilder builder() {
23+
return new TransportOptsBuilder();
24+
}
25+
26+
public String getHost() {
27+
return host;
28+
}
29+
30+
public int getPort() {
31+
return port;
32+
}
33+
34+
public String getProxyHost() {
35+
return proxyHost;
36+
}
37+
38+
public int getProxyPort() {
39+
return proxyPort;
40+
}
41+
42+
public Executor getExecutor() {
43+
return executor;
44+
}
45+
46+
public static final class TransportOptsBuilder {
47+
48+
private String host = "127.0.0.1";
49+
private int port = 8091;
50+
private String proxyHost = "127.0.0.1";
51+
private int proxyPort = 9001;
52+
private Executor executor = Executors.newCachedThreadPool();
53+
54+
public TransportOpts build() {
55+
return new TransportOpts(this);
56+
}
57+
58+
public TransportOptsBuilder host(String host) {
59+
this.host = host;
60+
return this;
61+
}
62+
63+
public TransportOptsBuilder port(int port) {
64+
this.port = port;
65+
return this;
66+
}
67+
68+
public TransportOptsBuilder proxyHost(String proxyHost) {
69+
this.proxyHost = proxyHost;
70+
return this;
71+
}
72+
73+
public TransportOptsBuilder proxyPort(int proxyPort) {
74+
this.proxyPort = proxyPort;
75+
return this;
76+
}
77+
78+
public TransportOptsBuilder executor(Executor executor) {
79+
this.executor = executor;
80+
return this;
81+
}
82+
}
2583
}

src/main/java/io/eigr/spawn/api/actors/Value.java

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,16 @@
1111
import java.util.Objects;
1212
import java.util.Optional;
1313

14-
public final class Value<S extends GeneratedMessageV3, R extends GeneratedMessageV3> {
15-
16-
private S state;
17-
private R response;
14+
public final class Value {
1815

16+
private Object state;
17+
private Object response;
1918
private boolean checkpoint;
2019
private Optional<Broadcast<?>> broadcast;
2120
private Optional<Forward> forward;
2221
private Optional<Pipe> pipe;
23-
private Optional<List<SideEffect>> effects;
24-
25-
private ResponseType type;
22+
private Optional<List<SideEffect<?>>> effects;
23+
private final ResponseType type;
2624

2725
private Value() {
2826
this.state = null;
@@ -36,13 +34,13 @@ private Value() {
3634
}
3735

3836
private Value(
39-
R response,
40-
S state,
37+
Object response,
38+
Object state,
4139
boolean checkpoint,
4240
Optional<Broadcast<?>> broadcast,
4341
Optional<Forward> forward,
4442
Optional<Pipe> pipe,
45-
Optional<List<SideEffect>> effects,
43+
Optional<List<SideEffect<?>>> effects,
4644
ResponseType type) {
4745
this.response = response;
4846
this.state = state;
@@ -54,16 +52,16 @@ private Value(
5452
this.type = type;
5553
}
5654

57-
public static <S, V> Value at() {
55+
public static Value at() {
5856
return new Value();
5957
}
6058

61-
public R getResponse() {
62-
return response;
59+
public <R extends GeneratedMessageV3> R getResponse() {
60+
return (R) response;
6361
}
6462

65-
public S getState() {
66-
return state;
63+
public <S extends GeneratedMessageV3> S getState() {
64+
return (S) state;
6765
}
6866

6967
public boolean getCheckpoint() {
@@ -82,31 +80,31 @@ public Optional<Pipe> getPipe() {
8280
return pipe;
8381
}
8482

85-
public Optional<List<SideEffect>> getEffects() {
83+
public Optional<List<SideEffect<?>>> getEffects() {
8684
return effects;
8785
}
8886

8987
public ResponseType getType() {
9088
return type;
9189
}
9290

93-
public Value response(R value) {
91+
public <R extends GeneratedMessageV3> Value response(R value) {
9492
this.response = value;
9593
return this;
9694
}
9795

98-
public Value state(S state) {
96+
public <S extends GeneratedMessageV3> Value state(S state) {
9997
this.state = state;
10098
return this;
10199
}
102100

103-
public Value state(S state, boolean checkpoint) {
101+
public <S extends GeneratedMessageV3> Value state(S state, boolean checkpoint) {
104102
this.state = state;
105103
this.checkpoint = checkpoint;
106104
return this;
107105
}
108106

109-
public Value flow(Broadcast broadcast) {
107+
public Value flow(Broadcast<?> broadcast) {
110108
this.broadcast = Optional.of(broadcast);
111109
return this;
112110
}
@@ -121,8 +119,8 @@ public Value flow(Pipe pipe) {
121119
return this;
122120
}
123121

124-
public Value flow(SideEffect effect) {
125-
List<SideEffect> ef;
122+
public Value flow(SideEffect<?> effect) {
123+
List<SideEffect<?>> ef;
126124
if (this.effects.isPresent()) {
127125
ef = this.effects.get();
128126
ef.add(effect);
@@ -135,7 +133,7 @@ public Value flow(SideEffect effect) {
135133
return this;
136134
}
137135

138-
public Value flow(List<SideEffect> effects) {
136+
public Value flow(List<SideEffect<?>> effects) {
139137
this.effects = Optional.of(effects);
140138
return this;
141139
}
@@ -171,7 +169,7 @@ public String toString() {
171169
public boolean equals(Object o) {
172170
if (this == o) return true;
173171
if (o == null || getClass() != o.getClass()) return false;
174-
Value<?, ?> value = (Value<?, ?>) o;
172+
Value value = (Value) o;
175173
return Objects.equals(state, value.state) &&
176174
Objects.equals(response, value.response) &&
177175
Objects.equals(checkpoint, value.checkpoint) &&

src/main/java/io/eigr/spawn/internal/transport/server/ActorServiceHandler.java

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
import io.eigr.spawn.api.actors.Value;
1313
import io.eigr.spawn.api.actors.ActorContext;
1414
import io.eigr.spawn.api.actors.ActorFactory;
15-
import io.eigr.spawn.api.actors.workflows.Broadcast;
16-
import io.eigr.spawn.api.actors.workflows.Forward;
17-
import io.eigr.spawn.api.actors.workflows.Pipe;
1815
import io.eigr.spawn.api.actors.workflows.SideEffect;
1916
import io.eigr.spawn.api.exceptions.ActorInvocationException;
2017
import io.eigr.spawn.internal.Entity;
@@ -209,30 +206,15 @@ private Optional<Entity> getEntityByActor(String actor, String parent) {
209206
private Protocol.Workflow buildWorkflow(Value valueResponse) {
210207
Protocol.Workflow.Builder workflowBuilder = Protocol.Workflow.newBuilder();
211208

212-
if (valueResponse.getBroadcast().isPresent()) {
213-
Protocol.Broadcast b = ((Broadcast) valueResponse.getBroadcast().get()).build();
214-
workflowBuilder.setBroadcast(b);
215-
}
216-
217-
if (valueResponse.getForward().isPresent()) {
218-
Protocol.Forward f = ((Forward) valueResponse.getForward().get()).build();
219-
workflowBuilder.setForward(f);
220-
}
221-
222-
if (valueResponse.getPipe().isPresent()) {
223-
Protocol.Pipe p = ((Pipe) valueResponse.getPipe().get()).build();
224-
workflowBuilder.setPipe(p);
225-
}
226-
227-
if (valueResponse.getEffects().isPresent()) {
228-
List<SideEffect> efs = ((List<SideEffect>) valueResponse.getEffects().get());
229-
workflowBuilder.addAllEffects(getProtocolEffects(efs));
230-
}
209+
valueResponse.getBroadcast().ifPresent(b -> workflowBuilder.setBroadcast(b.build()));
210+
valueResponse.getForward().ifPresent(f -> workflowBuilder.setForward(f.build()));
211+
valueResponse.getPipe().ifPresent(p -> workflowBuilder.setPipe(p.build()));
212+
valueResponse.getEffects().ifPresent(e -> workflowBuilder.addAllEffects(getProtocolEffects(e)));
231213

232214
return workflowBuilder.build();
233215
}
234216

235-
private List<Protocol.SideEffect> getProtocolEffects(List<SideEffect> effects) {
217+
private List<Protocol.SideEffect> getProtocolEffects(List<SideEffect<?>> effects) {
236218
return effects.stream()
237219
.map(SideEffect::build)
238220
.collect(Collectors.toList());

0 commit comments

Comments
 (0)