Skip to content

Commit d3a4ced

Browse files
author
Oleksii Tymchenko
authored
Merge pull request #246 from SpineEventEngine/error-prone-fixes
Error Prone and date/time fixes
2 parents 7ec66cb + c33a7dd commit d3a4ced

File tree

17 files changed

+477
-320
lines changed

17 files changed

+477
-320
lines changed

client/src/main/java/org/spine3/base/FailureThrowable.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public abstract class FailureThrowable extends Throwable {
4040
private final Timestamp timestamp;
4141

4242
protected FailureThrowable(GeneratedMessageV3 failure) {
43+
super();
4344
this.failure = failure;
4445
this.timestamp = getCurrentTime();
4546
}

client/src/main/java/org/spine3/validate/Validate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ public static void checkParameter(boolean expression, String parameterName, Stri
189189
public static String checkNotEmptyOrBlank(String stringToCheck, String fieldName) {
190190
checkNotNull(stringToCheck, fieldName + " must not be null.");
191191
checkParameter(!stringToCheck.isEmpty(), fieldName, "%s must not be an empty string.");
192-
checkParameter(stringToCheck.trim()
193-
.length() > 0, fieldName, "%s must not be a blank string.");
192+
final String trimmed = stringToCheck.trim();
193+
checkParameter(trimmed.length() > 0, fieldName, "%s must not be a blank string.");
194194
return stringToCheck;
195195
}
196196

server/src/main/java/org/spine3/server/BoundedContext.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public class BoundedContext extends IntegrationEventSubscriberGrpc.IntegrationEv
8989
private final List<Repository<?, ?>> repositories = Lists.newLinkedList();
9090

9191
private BoundedContext(Builder builder) {
92+
super();
9293
this.name = builder.name;
9394
this.multitenant = builder.multitenant;
9495
this.storageFactory = builder.storageFactory;
@@ -201,8 +202,8 @@ private void checkStorageAssigned(Repository repository) {
201202
}
202203
}
203204

204-
@SuppressWarnings("RefusedBequest") /* We ignore method from super because the default
205-
implementation sets unimplemented status. */
205+
@SuppressWarnings("MethodDoesntCallSuperMethod") /* We ignore method from super because the default
206+
implementation sets unimplemented status. */
206207
@Override
207208
public void notify(IntegrationEvent integrationEvent, StreamObserver<Response> responseObserver) {
208209
final Message eventMsg = unpack(integrationEvent.getMessage());

server/src/main/java/org/spine3/server/CommandService.java

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
import org.spine3.base.Command;
2929
import org.spine3.base.Response;
3030
import org.spine3.client.grpc.CommandServiceGrpc;
31+
import org.spine3.server.command.CommandBus;
3132
import org.spine3.server.command.error.CommandException;
3233
import org.spine3.server.command.error.UnsupportedCommandException;
3334
import org.spine3.server.type.CommandClass;
3435

36+
import java.util.Map;
3537
import java.util.Set;
3638

3739
/**
@@ -40,29 +42,37 @@
4042
*
4143
* @author Alexander Yevsyukov
4244
*/
43-
public class CommandService
44-
extends CommandServiceGrpc.CommandServiceImplBase {
45+
public class CommandService extends CommandServiceGrpc.CommandServiceImplBase {
4546

4647
private final ImmutableMap<CommandClass, BoundedContext> boundedContextMap;
4748

49+
/**
50+
* Creates a new builder for {@code CommandService}.
51+
*/
4852
public static Builder newBuilder() {
4953
return new Builder();
5054
}
5155

52-
protected CommandService(Builder builder) {
53-
this.boundedContextMap = builder.getBoundedContextMap();
56+
/**
57+
* Constructs new instance using the map from a {@code CommandClass} to a {@code BoundedContext} instance
58+
* which handles the command.
59+
*/
60+
protected CommandService(Map<CommandClass, BoundedContext> map) {
61+
super();
62+
this.boundedContextMap = ImmutableMap.copyOf(map);
5463
}
5564

56-
@SuppressWarnings("RefusedBequest") // as we override default implementation with `unimplemented` status.
65+
@SuppressWarnings("MethodDoesntCallSuperMethod")
66+
// as we override default implementation with `unimplemented` status.
5767
@Override
5868
public void post(Command request, StreamObserver<Response> responseObserver) {
5969
final CommandClass commandClass = CommandClass.of(request);
6070
final BoundedContext boundedContext = boundedContextMap.get(commandClass);
6171
if (boundedContext == null) {
6272
handleUnsupported(request, responseObserver);
6373
} else {
64-
boundedContext.getCommandBus()
65-
.post(request, responseObserver);
74+
final CommandBus commandBus = boundedContext.getCommandBus();
75+
commandBus.post(request, responseObserver);
6676
}
6777
}
6878

@@ -75,45 +85,63 @@ private static void handleUnsupported(Command request, StreamObserver<Response>
7585
public static class Builder {
7686

7787
private final Set<BoundedContext> boundedContexts = Sets.newHashSet();
78-
private ImmutableMap<CommandClass, BoundedContext> boundedContextMap;
7988

80-
public Builder addBoundedContext(BoundedContext boundedContext) {
89+
/**
90+
* Adds the {@code BoundedContext} to the builder.
91+
*/
92+
public Builder add(BoundedContext boundedContext) {
8193
// Save it to a temporary set so that it is easy to remove it if needed.
8294
boundedContexts.add(boundedContext);
8395
return this;
8496
}
8597

86-
public Builder removeBoundedContext(BoundedContext boundedContext) {
98+
/**
99+
* Removes the {@code BoundedContext} from the builder.
100+
*/
101+
public Builder remove(BoundedContext boundedContext) {
87102
boundedContexts.remove(boundedContext);
88103
return this;
89104
}
90105

91-
@SuppressWarnings("ReturnOfCollectionOrArrayField") // is immutable
92-
public ImmutableMap<CommandClass, BoundedContext> getBoundedContextMap() {
93-
return boundedContextMap;
106+
/**
107+
* Verifies if the passed {@code BoundedContext} was previously added to the builder.
108+
*
109+
* @param boundedContext the instance to check
110+
* @return {@code true} if the instance was added to the builder, {@code false} otherwise
111+
*/
112+
public boolean contains(BoundedContext boundedContext) {
113+
final boolean contains = boundedContexts.contains(boundedContext);
114+
return contains;
94115
}
95116

96117
/**
97-
* Builds the {@link CommandService}.
118+
* Builds a new {@link CommandService}.
98119
*/
99120
public CommandService build() {
100-
this.boundedContextMap = createBoundedContextMap();
101-
final CommandService result = new CommandService(this);
121+
final ImmutableMap<CommandClass, BoundedContext> map = createMap();
122+
final CommandService result = new CommandService(map);
102123
return result;
103124
}
104125

105-
private ImmutableMap<CommandClass, BoundedContext> createBoundedContextMap() {
126+
/**
127+
* Creates a map from {@code CommandClass}es to {@code BoundedContext}s that handle such commands.
128+
*/
129+
private ImmutableMap<CommandClass, BoundedContext> createMap() {
106130
final ImmutableMap.Builder<CommandClass, BoundedContext> builder = ImmutableMap.builder();
107131
for (BoundedContext boundedContext : boundedContexts) {
108-
addBoundedContext(builder, boundedContext);
132+
putIntoMap(boundedContext, builder);
109133
}
110134
return builder.build();
111135
}
112136

113-
private static void addBoundedContext(ImmutableMap.Builder<CommandClass, BoundedContext> mapBuilder,
114-
BoundedContext boundedContext) {
115-
final Set<CommandClass> cmdClasses = boundedContext.getCommandBus()
116-
.getSupportedCommandClasses();
137+
/**
138+
* Associates {@code CommandClass}es with the instance of {@code BoundedContext}
139+
* that handles such commands.
140+
*/
141+
private static void putIntoMap(BoundedContext boundedContext,
142+
ImmutableMap.Builder<CommandClass, BoundedContext> mapBuilder) {
143+
final CommandBus commandBus = boundedContext.getCommandBus();
144+
final Set<CommandClass> cmdClasses = commandBus.getSupportedCommandClasses();
117145
for (CommandClass commandClass : cmdClasses) {
118146
mapBuilder.put(commandClass, boundedContext);
119147
}

server/src/main/java/org/spine3/server/QueryService.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
import org.spine3.client.QueryResponse;
3131
import org.spine3.client.grpc.QueryServiceGrpc;
3232
import org.spine3.protobuf.TypeUrl;
33+
import org.spine3.server.stand.Stand;
3334

35+
import java.util.Map;
3436
import java.util.Set;
3537

3638
import static com.google.common.base.Preconditions.checkNotNull;
@@ -46,15 +48,17 @@ public class QueryService extends QueryServiceGrpc.QueryServiceImplBase {
4648

4749
private final ImmutableMap<TypeUrl, BoundedContext> typeToContextMap;
4850

49-
private QueryService(Builder builder) {
50-
this.typeToContextMap = builder.getBoundedContextMap();
51+
private QueryService(Map<TypeUrl, BoundedContext> map) {
52+
super();
53+
this.typeToContextMap = ImmutableMap.copyOf(map);
5154
}
5255

5356
public static Builder newBuilder() {
5457
return new Builder();
5558
}
5659

57-
@SuppressWarnings("RefusedBequest") // as we override default implementation with `unimplemented` status.
60+
@SuppressWarnings("MethodDoesntCallSuperMethod")
61+
// as we override default implementation with `unimplemented` status.
5862
@Override
5963
public void read(Query query, StreamObserver<QueryResponse> responseObserver) {
6064
log().debug("Incoming query: {}", query);
@@ -63,9 +67,9 @@ public void read(Query query, StreamObserver<QueryResponse> responseObserver) {
6367
checkNotNull(type, "Unknown type for query target");
6468

6569
final BoundedContext boundedContext = typeToContextMap.get(type);
70+
final Stand stand = boundedContext.getStand();
6671
try {
67-
boundedContext.getStand()
68-
.execute(query, responseObserver);
72+
stand.execute(query, responseObserver);
6973
} catch (@SuppressWarnings("OverlyBroadCatchBlock") Exception e) {
7074
log().error("Error processing query", e);
7175
responseObserver.onError(e);
@@ -74,22 +78,20 @@ public void read(Query query, StreamObserver<QueryResponse> responseObserver) {
7478

7579
public static class Builder {
7680
private final Set<BoundedContext> boundedContexts = Sets.newHashSet();
77-
private ImmutableMap<TypeUrl, BoundedContext> typeToContextMap;
7881

79-
public Builder addBoundedContext(BoundedContext boundedContext) {
82+
public Builder add(BoundedContext boundedContext) {
8083
// Save it to a temporary set so that it is easy to remove it if needed.
8184
boundedContexts.add(boundedContext);
8285
return this;
8386
}
8487

85-
public Builder removeBoundedContext(BoundedContext boundedContext) {
88+
public Builder remove(BoundedContext boundedContext) {
8689
boundedContexts.remove(boundedContext);
8790
return this;
8891
}
8992

90-
@SuppressWarnings("ReturnOfCollectionOrArrayField") // the collection returned is immutable
91-
public ImmutableMap<TypeUrl, BoundedContext> getBoundedContextMap() {
92-
return typeToContextMap;
93+
public boolean contains(BoundedContext boundedContext) {
94+
return boundedContexts.contains(boundedContext);
9395
}
9496

9597
/**
@@ -99,26 +101,26 @@ public ImmutableMap<TypeUrl, BoundedContext> getBoundedContextMap() {
99101
*/
100102
public QueryService build() throws IllegalStateException {
101103
if (boundedContexts.isEmpty()) {
102-
throw new IllegalStateException("Query service must have at least one bounded context.");
104+
throw new IllegalStateException("Query service must have at least one `BoundedContext`.");
103105
}
104-
this.typeToContextMap = createBoundedContextMap();
105-
final QueryService result = new QueryService(this);
106+
final ImmutableMap<TypeUrl, BoundedContext> map = createMap();
107+
final QueryService result = new QueryService(map);
106108
return result;
107109
}
108110

109-
private ImmutableMap<TypeUrl, BoundedContext> createBoundedContextMap() {
111+
private ImmutableMap<TypeUrl, BoundedContext> createMap() {
110112
final ImmutableMap.Builder<TypeUrl, BoundedContext> builder = ImmutableMap.builder();
111113
for (BoundedContext boundedContext : boundedContexts) {
112-
addBoundedContext(builder, boundedContext);
114+
putIntoMap(boundedContext, builder);
113115
}
114116
return builder.build();
115117
}
116118

117-
private static void addBoundedContext(ImmutableMap.Builder<TypeUrl, BoundedContext> mapBuilder,
118-
BoundedContext boundedContext) {
119+
private static void putIntoMap(BoundedContext boundedContext,
120+
ImmutableMap.Builder<TypeUrl, BoundedContext> mapBuilder) {
119121

120-
final ImmutableSet<TypeUrl> exposedTypes = boundedContext.getStand()
121-
.getExposedTypes();
122+
final Stand stand = boundedContext.getStand();
123+
final ImmutableSet<TypeUrl> exposedTypes = stand.getExposedTypes();
122124

123125
for (TypeUrl availableType : exposedTypes) {
124126
mapBuilder.put(availableType, boundedContext);
@@ -135,5 +137,4 @@ private enum LogSingleton {
135137
@SuppressWarnings("NonSerializableFieldInSerializableClass")
136138
private final Logger value = LoggerFactory.getLogger(QueryService.class);
137139
}
138-
139140
}

server/src/main/java/org/spine3/server/SubscriptionService.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.spine3.protobuf.TypeUrl;
3939
import org.spine3.server.stand.Stand;
4040

41+
import java.util.Map;
4142
import java.util.Set;
4243

4344
import static com.google.common.base.Preconditions.checkNotNull;
@@ -49,18 +50,19 @@
4950
*
5051
* @author Alex Tymchenko
5152
*/
53+
@SuppressWarnings("MethodDoesntCallSuperMethod") // as we override default implementation with `unimplemented` status.
5254
public class SubscriptionService extends SubscriptionServiceGrpc.SubscriptionServiceImplBase {
5355
private final ImmutableMap<TypeUrl, BoundedContext> typeToContextMap;
5456

55-
private SubscriptionService(Builder builder) {
56-
this.typeToContextMap = builder.getBoundedContextMap();
57+
private SubscriptionService(Map<TypeUrl, BoundedContext> map) {
58+
super();
59+
this.typeToContextMap = ImmutableMap.copyOf(map);
5760
}
5861

5962
public static Builder newBuilder() {
6063
return new Builder();
6164
}
6265

63-
@SuppressWarnings("RefusedBequest") // as we override default implementation with `unimplemented` status.
6466
@Override
6567
public void subscribe(Topic topic, StreamObserver<Subscription> responseObserver) {
6668
log().debug("Creating the subscription to a topic: {}", topic);
@@ -80,7 +82,6 @@ public void subscribe(Topic topic, StreamObserver<Subscription> responseObserver
8082
}
8183
}
8284

83-
@SuppressWarnings("RefusedBequest") // as we override default implementation with `unimplemented` status.
8485
@Override
8586
public void activate(final Subscription subscription, final StreamObserver<SubscriptionUpdate> responseObserver) {
8687
log().debug("Activating the subscription: {}", subscription);
@@ -108,7 +109,6 @@ public void onStateChanged(Any newEntityState) {
108109
}
109110
}
110111

111-
@SuppressWarnings("RefusedBequest") // as we override default implementation with `unimplemented` status.
112112
@Override
113113
public void cancel(Subscription subscription, StreamObserver<Response> responseObserver) {
114114
log().debug("Incoming cancel request for the subscription topic: {}", subscription);
@@ -143,24 +143,18 @@ private BoundedContext selectBoundedContext(Target target) {
143143

144144
public static class Builder {
145145
private final Set<BoundedContext> boundedContexts = Sets.newHashSet();
146-
private ImmutableMap<TypeUrl, BoundedContext> typeToContextMap;
147146

148-
public Builder addBoundedContext(BoundedContext boundedContext) {
147+
public Builder add(BoundedContext boundedContext) {
149148
// Save it to a temporary set so that it is easy to remove it if needed.
150149
boundedContexts.add(boundedContext);
151150
return this;
152151
}
153152

154-
public Builder removeBoundedContext(BoundedContext boundedContext) {
153+
public Builder remove(BoundedContext boundedContext) {
155154
boundedContexts.remove(boundedContext);
156155
return this;
157156
}
158157

159-
@SuppressWarnings("ReturnOfCollectionOrArrayField") // the collection returned is immutable
160-
public ImmutableMap<TypeUrl, BoundedContext> getBoundedContextMap() {
161-
return typeToContextMap;
162-
}
163-
164158
@SuppressWarnings("ReturnOfCollectionOrArrayField") // the collection returned is immutable
165159
public ImmutableList<BoundedContext> getBoundedContexts() {
166160
return ImmutableList.copyOf(boundedContexts);
@@ -175,22 +169,21 @@ public SubscriptionService build() throws IllegalStateException {
175169
if (boundedContexts.isEmpty()) {
176170
throw new IllegalStateException("Subscription service must have at least one bounded context.");
177171
}
178-
this.typeToContextMap = createBoundedContextMap();
179-
final SubscriptionService result = new SubscriptionService(this);
172+
final ImmutableMap<TypeUrl, BoundedContext> map = createMap();
173+
final SubscriptionService result = new SubscriptionService(map);
180174
return result;
181175
}
182176

183-
private ImmutableMap<TypeUrl, BoundedContext> createBoundedContextMap() {
177+
private ImmutableMap<TypeUrl, BoundedContext> createMap() {
184178
final ImmutableMap.Builder<TypeUrl, BoundedContext> builder = ImmutableMap.builder();
185179
for (BoundedContext boundedContext : boundedContexts) {
186-
addBoundedContext(builder, boundedContext);
180+
putIntoMap(boundedContext, builder);
187181
}
188182
return builder.build();
189183
}
190184

191-
private static void addBoundedContext(ImmutableMap.Builder<TypeUrl, BoundedContext> mapBuilder,
192-
BoundedContext boundedContext) {
193-
185+
private static void putIntoMap(BoundedContext boundedContext,
186+
ImmutableMap.Builder<TypeUrl, BoundedContext> mapBuilder) {
194187
final Stand stand = boundedContext.getStand();
195188
final ImmutableSet<TypeUrl> exposedTypes = stand.getExposedTypes();
196189
for (TypeUrl availableType : exposedTypes) {

0 commit comments

Comments
 (0)