Skip to content

Commit 721e2de

Browse files
Enable method chaining for EnvSetting class.
1 parent 12193bc commit 721e2de

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

server/src/main/java/io/spine/server/EnvSetting.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
package io.spine.server;
2828

29+
import com.google.common.annotations.VisibleForTesting;
30+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2931
import io.spine.environment.Environment;
3032
import io.spine.environment.EnvironmentType;
3133
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@@ -47,8 +49,8 @@
4749
* <p>For example:
4850
* <pre>{@code
4951
* EnvSetting<StorageFactory> storageFactory = new EnvSetting<>();
50-
* storageFactory.use(InMemoryStorageFactory.newInstance(), Production.class);
51-
* storageFactory.use(new MemoizingStorageFactory(), Tests.class);
52+
* storageFactory.use(InMemoryStorageFactory.newInstance(), Production.class)
53+
* .use(new MemoizingStorageFactory(), Tests.class);
5254
*
5355
* // Provides the `StorageFactory` for the current environment of the application.
5456
* StorageFactory currentStorageFactory = storageFactory.value();
@@ -196,6 +198,7 @@ public V value() {
196198
* <p>The environment type is retrieved from {@code Supplier} under the read lock
197199
* for the possibility of controlling this lock.
198200
*/
201+
@VisibleForTesting
199202
Optional<V> valueFor(Supplier<Class<? extends EnvironmentType>> type) {
200203
Value<V> value = lockReadOperation(() -> {
201204
Class<? extends EnvironmentType> envType = type.get();
@@ -227,12 +230,14 @@ public void reset() {
227230
* @param type
228231
* the type of the environment
229232
*/
230-
public void use(V value, Class<? extends EnvironmentType> type) {
233+
@CanIgnoreReturnValue
234+
public EnvSetting<V> use(V value, Class<? extends EnvironmentType> type) {
231235
checkNotNull(value);
232236
checkNotNull(type);
233237
lockWriteOperation(() -> {
234238
this.environmentValues.put(type, new Value<>(value));
235239
});
240+
return this;
236241
}
237242

238243
/**
@@ -242,13 +247,16 @@ public void use(V value, Class<? extends EnvironmentType> type) {
242247
* <p>The value to set is initialized under the write lock
243248
* for the possibility of controlling this lock.
244249
*/
245-
void useViaInit(Supplier<V> initializer, Class<? extends EnvironmentType> type) {
250+
@CanIgnoreReturnValue
251+
@VisibleForTesting
252+
EnvSetting<V> useViaInit(Supplier<V> initializer, Class<? extends EnvironmentType> type) {
246253
checkNotNull(type);
247254
lockWriteOperation(() -> {
248255
V value = initializer.get();
249256
checkNotNull(value);
250257
this.environmentValues.put(type, new Value<>(value));
251258
});
259+
return this;
252260
}
253261

254262
/**
@@ -263,12 +271,14 @@ void useViaInit(Supplier<V> initializer, Class<? extends EnvironmentType> type)
263271
* @param type
264272
* the type of the environment
265273
*/
266-
public void lazyUse(Supplier<V> value, Class<? extends EnvironmentType> type) {
274+
@CanIgnoreReturnValue
275+
public EnvSetting<V> lazyUse(Supplier<V> value, Class<? extends EnvironmentType> type) {
267276
checkNotNull(value);
268277
checkNotNull(type);
269278
lockWriteOperation(() -> {
270279
this.environmentValues.put(type, new Value<>(value));
271280
});
281+
return this;
272282
}
273283

274284
private Optional<V> valueFor(Class<? extends EnvironmentType> type) {

server/src/test/java/io/spine/server/EnvSettingTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,9 @@ void resetTheValues() {
379379
InMemoryStorageFactory localStorageFactory = InMemoryStorageFactory.newInstance();
380380

381381
EnvSetting<StorageFactory> storageFactory = new EnvSetting<>();
382-
storageFactory.use(defaultStorageFactory, DefaultMode.class);
383-
storageFactory.use(testingStorageFactory, Tests.class);
384-
storageFactory.use(localStorageFactory, Local.class);
382+
storageFactory.use(defaultStorageFactory, DefaultMode.class)
383+
.use(testingStorageFactory, Tests.class)
384+
.use(localStorageFactory, Local.class);
385385

386386
storageFactory.reset();
387387

@@ -412,9 +412,9 @@ void runOperationForAll() throws Exception {
412412

413413
EnvSetting<StorageFactory> storageSetting = new EnvSetting<>();
414414

415-
storageSetting.use(defaultStorageFactory, DefaultMode.class);
416-
storageSetting.use(testingStorageFactory, Tests.class);
417-
storageSetting.use(localStorageFactory, Local.class);
415+
storageSetting.use(defaultStorageFactory, DefaultMode.class)
416+
.use(testingStorageFactory, Tests.class)
417+
.use(localStorageFactory, Local.class);
418418

419419
storageSetting.apply(AutoCloseable::close);
420420

0 commit comments

Comments
 (0)