Skip to content

Commit

Permalink
Add documentation for new methods of the EnvSetting class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem-Semenov-dev committed May 24, 2024
1 parent 8608475 commit e5cf1dd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
41 changes: 27 additions & 14 deletions server/src/main/java/io/spine/server/EnvSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public final class EnvSetting<V> {
/**
* Creates a new instance without any fallback configuration.
*/
EnvSetting() {
public EnvSetting() {
}

/**
Expand All @@ -114,7 +114,7 @@ public final class EnvSetting<V> {
* <p>If a value for {@code type} is not {@linkplain #use(Object, Class) set explicitly},
* {@link #value(Class)} and {@link #optionalValue(Class)} return the {@code fallback} result.
*/
EnvSetting(Class<? extends EnvironmentType> type, Supplier<V> fallback) {
public EnvSetting(Class<? extends EnvironmentType> type, Supplier<V> fallback) {
lockWriteOperation(() -> {
this.fallbacks.put(type, fallback);
});
Expand All @@ -124,7 +124,7 @@ public final class EnvSetting<V> {
* If the value for the specified environment has been configured, returns it. Returns an
* empty {@code Optional} otherwise.
*/
Optional<V> optionalValue(Class<? extends EnvironmentType> type) {
public Optional<V> optionalValue(Class<? extends EnvironmentType> type) {
return valueFor(type);
}

Expand All @@ -138,7 +138,7 @@ Optional<V> optionalValue(Class<? extends EnvironmentType> type) {
* @param operation
* operation to run
*/
void ifPresentForEnvironment(Class<? extends EnvironmentType> type,
public void ifPresentForEnvironment(Class<? extends EnvironmentType> type,
SettingOperation<V> operation) throws Exception {
Optional<V> value = valueFor(type);
if (value.isPresent()) {
Expand All @@ -155,7 +155,7 @@ void ifPresentForEnvironment(Class<? extends EnvironmentType> type,
* @apiNote The not yet run {@linkplain #fallbacks fallback suppliers} are ignored to avoid an
* unnecessary value instantiation.
*/
void apply(SettingOperation<V> operation) throws Exception {
public void apply(SettingOperation<V> operation) throws Exception {
lockWriteOperation(() -> {
for (Value<V> v : environmentValues.values()) {
if (v.isResolved()) {
Expand All @@ -176,7 +176,7 @@ void apply(SettingOperation<V> operation) throws Exception {
* <p>If it is not set, returns a fallback value. If no fallback was configured, an
* {@code IllegalStateException} is thrown.
*/
V value(Class<? extends EnvironmentType> type) {
public V value(Class<? extends EnvironmentType> type) {
checkNotNull(type);
Optional<V> result = valueFor(type);
return result.orElseThrow(
Expand All @@ -191,15 +191,24 @@ V value(Class<? extends EnvironmentType> type) {
* return a fallback value. If no fallback was configured,
* an {@code IllegalStateException} is thrown.
*/
V value() {
public V value() {
Environment environment = Environment.instance();
return value(environment.type());
}

V valueFor(Supplier<Class<? extends EnvironmentType>> type) {
/**
* This is a test-only method allowing to retrieve the value for environment type
* passed through {@code Supplier}.
*/
Optional<V> valueFor(Supplier<Class<? extends EnvironmentType>> type) {
return lockReadOperation(() -> {
Class<? extends EnvironmentType> environmentType = type.get();
return this.environmentValues.get(environmentType).value;
Class<? extends EnvironmentType> envType = type.get();
checkNotNull(envType);
Value<V> value = this.environmentValues.get(envType);
if (value == null) {
return Optional.empty();
}
return Optional.of(value.value);
});
}

Expand All @@ -209,8 +218,7 @@ V valueFor(Supplier<Class<? extends EnvironmentType>> type) {
* <p>Cached default values are also cleared and will be recalculated using the {@code
* Supplier} passed to the {@linkplain #EnvSetting(Class, Supplier) constructor}.
*/
@VisibleForTesting
void reset() {
public void reset() {
lockWriteOperation(environmentValues::clear);
}

Expand All @@ -222,18 +230,23 @@ void reset() {
* @param type
* the type of the environment
*/
void use(V value, Class<? extends EnvironmentType> type) {
public void use(V value, Class<? extends EnvironmentType> type) {
lockWriteOperation(() -> {
checkNotNull(value);
checkNotNull(type);
this.environmentValues.put(type, new Value<>(value));
});
}

/**
* This is a test-only method allowing to set the value for the specified environment type
* using the provided initializer.
*/
void useWithInit(Supplier<V> initializer, Class<? extends EnvironmentType> type) {
lockWriteOperation(() -> {
checkNotNull(type);
V value = initializer.get();
checkNotNull(value);
this.environmentValues.put(type, new Value<>(value));
});
}
Expand All @@ -250,7 +263,7 @@ void useWithInit(Supplier<V> initializer, Class<? extends EnvironmentType> type)
* @param type
* the type of the environment
*/
void lazyUse(Supplier<V> value, Class<? extends EnvironmentType> type) {
public void lazyUse(Supplier<V> value, Class<? extends EnvironmentType> type) {
lockWriteOperation(() -> {
checkNotNull(value);
checkNotNull(type);
Expand Down
5 changes: 3 additions & 2 deletions server/src/test/java/io/spine/server/EnvSettingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,12 @@ void testReadOperations() {
setting.use(initalValue, Local.class);

Future<?> readBlockingFuture = readWriteExecutors.submit(() -> {
UUID actualValue = setting.valueFor(() -> {
Optional<UUID> actualValue = setting.valueFor(() -> {
awaitUninterruptibly(latch);
return Local.class;
});
assertThat(actualValue).isEqualTo(initalValue);
assertThat(actualValue).isPresent();
assertThat(actualValue.get()).isEqualTo(initalValue);
});

sleepUninterruptibly(100, MILLISECONDS);
Expand Down

0 comments on commit e5cf1dd

Please sign in to comment.