Skip to content

Commit

Permalink
Merge pull request #89 from avaje/feature/putAll-wildcard
Browse files Browse the repository at this point in the history
Change putAll() to use wildcard type
  • Loading branch information
rob-bygrave committed Sep 28, 2023
2 parents d75ecd1 + 65cac70 commit abeafff
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 55 deletions.
2 changes: 1 addition & 1 deletion avaje-config/src/main/java/io/avaje/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ public static void setProperty(String key, String value) {
*
* <p>This will fire configuration callback listeners that are registered.
*/
public static void putAll(Map<String, Object> map) {
public static void putAll(Map<String, ?> map) {
data.putAll(map);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public interface Configuration {
* <p>
* This will fire configuration callback listeners that are registered.
*/
void putAll(Map<String, Object> map);
void putAll(Map<String, ?> map);

/**
* Clear the value for the given key. Note that {@link #eventBuilder(String)} should be
Expand Down
13 changes: 2 additions & 11 deletions avaje-config/src/main/java/io/avaje/config/CoreConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -403,18 +403,9 @@ public void setProperty(String key, String newValue) {
}

@Override
public void putAll(Map<String, Object> map) {
public void putAll(Map<String, ?> map) {
requireNonNull(map, "map cannot be null");
final var builder = eventBuilder("PutAll");

map.forEach(
(k, v) -> {
requireNonNull(k, "map key is required");
requireNonNull(v, "map value is required");
builder.put(k, v.toString());
});

builder.publish();
eventBuilder("PutAll").putAll(map).publish();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ final class CoreEventBuilder implements ModificationEvent.Builder {
this.snapshot = snapshot; // at the moment we don't mutate the snapshot so could just use the original map
}

@Override
public ModificationEvent.Builder putAll(Map<String, ?> map) {
map.forEach((key, value) -> {
requireNonNull(value);
put(key, value.toString());
});
return this;
}

@Override
public ModificationEvent.Builder put(String key, String value) {
requireNonNull(key);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.avaje.config;

import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

Expand Down Expand Up @@ -52,6 +53,11 @@ interface Builder {
*/
Builder put(String key, String value);

/**
* Set all the properties from the map.
*/
Builder putAll(Map<String, ?> map);

/**
* Remove a property from the configuration.
*/
Expand Down
81 changes: 49 additions & 32 deletions avaje-config/src/test/java/io/avaje/config/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.net.URI;
import java.time.Duration;
import java.time.LocalDate;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -136,6 +137,22 @@ void asConfiguration() {
assertThat(configuration.get("myapp.fooHome")).isEqualTo(home + "/config");
}

@Test
void putAll() {
Map<String, Object> extra = Map.of("myTempKey", "foo");
Config.putAll(extra);

Map<String, String> extra2 = Map.of("myTempKey2", "foo2");
Config.putAll(extra2);

final Configuration configuration = Config.asConfiguration();
assertThat(configuration.get("myTempKey")).isEqualTo("foo");
assertThat(configuration.get("myTempKey2")).isEqualTo("foo2");

configuration.clearProperty("myTempKey");
configuration.clearProperty("myTempKey2");
}

@Disabled
@Test
void load() {
Expand Down Expand Up @@ -332,55 +349,55 @@ void getAs_example() {
void getAs_func() {
Config.setProperty("func", "amogus");
final var result =
Config.getAs(
"func",
x -> {
assertThat(x).isEqualTo("amogus");
return "sus";
});
Config.getAs(
"func",
x -> {
assertThat(x).isEqualTo("amogus");
return "sus";
});
assertThat(result).isEqualTo("sus");

assertThrows(
IllegalStateException.class,
() ->
Config.getAs(
"func",
x -> {
throw new RuntimeException("broke");
}));
IllegalStateException.class,
() ->
Config.getAs(
"func",
x -> {
throw new RuntimeException("broke");
}));
Config.clearProperty("func");
}

@Test
void getAsOptional_func() {
Config.setProperty("func", "fire");
var result =
Config.getAsOptional(
"func",
x -> {
assertThat(x).isEqualTo("fire");
return "sus";
});
Config.getAsOptional(
"func",
x -> {
assertThat(x).isEqualTo("fire");
return "sus";
});

assertThat(result.orElseThrow()).isEqualTo("sus");

assertThrows(
IllegalStateException.class,
() ->
Config.getAsOptional(
"func",
x -> {
throw new RuntimeException("broke");
}));
IllegalStateException.class,
() ->
Config.getAsOptional(
"func",
x -> {
throw new RuntimeException("broke");
}));
Config.clearProperty("func");

result =
Config.getAsOptional(
"func",
x -> {
assertThat(x).isEqualTo("fire");
return null;
});
Config.getAsOptional(
"func",
x -> {
assertThat(x).isEqualTo("fire");
return null;
});

assertThat(result.isEmpty()).isEqualTo(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
import io.avaje.config.CoreEntry.CoreMap;
import org.junit.jupiter.api.Test;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -192,8 +187,8 @@ void getList() {
assertThat(data.list().ofLong("list.long.notThere", 51L, 52L)).contains(51L, 52L);
assertThat(data.list().ofLong("list.long.notThere2")).isEmpty();

assertThat(data.list().ofType("someValues", Short::parseShort)).contains((short)13, (short)42, (short)55);
assertThat(data.list().ofType("someValues", Short::parseShort)).contains((short)13, (short)42, (short)55);
assertThat(data.list().ofType("someValues", Short::parseShort)).contains((short) 13, (short) 42, (short) 55);
assertThat(data.list().ofType("someValues", Short::parseShort)).contains((short) 13, (short) 42, (short) 55);
assertThat(data.list().ofType("list.long.notThere2", Short::parseShort)).isEmpty();
}

Expand All @@ -214,8 +209,8 @@ void getSet() {
assertThat(data.set().ofLong("1set.long.notThere", 51L, 52L)).contains(51L, 52L);
assertThat(data.set().ofLong("1set.long.notThere2")).isEmpty();

assertThat(data.set().ofType("someValues", Short::parseShort)).contains((short)13, (short)42, (short)55);
assertThat(data.set().ofType("someValues", Short::parseShort)).contains((short)13, (short)42, (short)55);
assertThat(data.set().ofType("someValues", Short::parseShort)).contains((short) 13, (short) 42, (short) 55);
assertThat(data.set().ofType("someValues", Short::parseShort)).contains((short) 13, (short) 42, (short) 55);
assertThat(data.set().ofType("list.long.notThere2", Short::parseShort)).isEmpty();

}
Expand All @@ -236,6 +231,27 @@ void getEnum_doesNotExist() {
assertThrows(IllegalStateException.class, () -> data.getEnum(MyEnum.class, "myEnum.doesNotExist"));
}

@Test
void onChangePutAll() {
final List<ModificationEvent> capturedEvents = new ArrayList<>();
data.onChange(capturedEvents::add);

Map<String, String> myUpdate = Map.of("a", "1", "onChangeTest_1", "one", "onChangeTest_1.2", "two|${user.home}|be");

data.putAll(myUpdate);

assertThat(capturedEvents).hasSize(1);
final var event = capturedEvents.get(0);
assertThat(event.name()).isEqualTo("PutAll");
assertThat(event.modifiedKeys()).containsExactlyInAnyOrder("onChangeTest_1", "onChangeTest_1.2");

var configuration = event.configuration();

String userHome = System.getProperty("user.home");
assertThat(configuration.get("onChangeTest_1")).isEqualTo("one");
assertThat(configuration.get("onChangeTest_1.2")).isEqualTo("two|" + userHome + "|be");
}

@Test
void onChangeNew() {
// we will remove this entry
Expand Down

0 comments on commit abeafff

Please sign in to comment.