Skip to content

Commit 098b1bd

Browse files
committed
Add ModificationEvent.Builder.putAll taking java.util.Properties
As a convenience method
1 parent a9c30f6 commit 098b1bd

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

avaje-config/src/main/java/io/avaje/config/CoreEventBuilder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.LinkedHashMap;
44
import java.util.Map;
5+
import java.util.Properties;
56
import java.util.function.BiConsumer;
67

78
import static java.util.Objects.requireNonNull;
@@ -20,6 +21,15 @@ final class CoreEventBuilder implements ModificationEvent.Builder {
2021
this.snapshot = snapshot; // at the moment we don't mutate the snapshot so could just use the original map
2122
}
2223

24+
@Override
25+
public ModificationEvent.Builder putAll(Properties properties) {
26+
properties.forEach((key, value) -> {
27+
requireNonNull(value);
28+
put(key.toString(), value.toString());
29+
});
30+
return this;
31+
}
32+
2333
@Override
2434
public ModificationEvent.Builder putAll(Map<String, ?> map) {
2535
map.forEach((key, value) -> {

avaje-config/src/main/java/io/avaje/config/ModificationEvent.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.avaje.config;
22

33
import java.util.Map;
4+
import java.util.Properties;
45
import java.util.Set;
56
import java.util.function.Consumer;
67

@@ -58,6 +59,11 @@ interface Builder {
5859
*/
5960
Builder putAll(Map<String, ?> map);
6061

62+
/**
63+
* Set all the properties from the Properties object.
64+
*/
65+
Builder putAll(Properties properties);
66+
6167
/**
6268
* Remove a property from the configuration.
6369
*/

avaje-config/src/test/java/io/avaje/config/CoreConfigurationTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,44 @@ void onChangeNew() {
358358
assertThat(capturedEventsMatchOnKey.get(0).modifiedKeys()).containsExactlyInAnyOrder("onChangeTest_1", "onChangeTest_1.2", "foo.bar");
359359
}
360360

361+
@Test
362+
void onChangeMap() {
363+
AtomicInteger count = new AtomicInteger();
364+
StringBuilder sb = new StringBuilder();
365+
data.onChange("modify", newValue -> {
366+
count.incrementAndGet();
367+
sb.append(newValue).append(",");
368+
});
369+
370+
var map = Map.of("modify", "change1", "a", "2");
371+
data.eventBuilder("myTest")
372+
.putAll(map)
373+
.publish();
374+
375+
assertThat(count.get()).isEqualTo(1);
376+
assertThat(sb.toString()).isEqualTo("change1,");
377+
}
378+
379+
@Test
380+
void onChangeProperties() {
381+
AtomicInteger count = new AtomicInteger();
382+
StringBuilder sb = new StringBuilder();
383+
data.onChange("modify", newValue -> {
384+
count.incrementAndGet();
385+
sb.append(newValue).append(",");
386+
});
387+
388+
var properties = new Properties();
389+
properties.setProperty("modify", "change1");
390+
properties.setProperty("a", "2");
391+
data.eventBuilder("myTest")
392+
.putAll(properties)
393+
.publish();
394+
395+
assertThat(count.get()).isEqualTo(1);
396+
assertThat(sb.toString()).isEqualTo("change1,");
397+
}
398+
361399
@Test
362400
void onChange() {
363401
AtomicInteger count = new AtomicInteger();

0 commit comments

Comments
 (0)