Skip to content

Commit ced6b46

Browse files
committed
feat(core): add MapNode#getOrDefault
1 parent 04a1f49 commit ced6b46

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

core/src/main/java/com/github/siroshun09/configapi/core/node/MapNode.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,19 @@ public sealed interface MapNode extends CommentableNode<Map<Object, Node<?>>> pe
8989
* @param key the key to get
9090
* @return a {@link Node} to which the specified key is mapped, or {@link NullNode#NULL} if this {@link MapNode} contains no mapping for the key
9191
*/
92-
@NotNull Node<?> get(@NotNull Object key);
92+
default @NotNull Node<?> get(@NotNull Object key) {
93+
return this.getOrDefault(key, NullNode.NULL);
94+
}
95+
96+
/**
97+
* Gets a {@link Node} to which the specified key is mapped,
98+
* or the specified {@link Node} if this {@link MapNode} contains no mapping for the key.
99+
*
100+
* @param key the key to get
101+
* @param defaultNode the {@link Node} to return when this {@link MapNode} contains no mapping for the key
102+
* @return a {@link Node} to which the specified key is mapped, or the specified {@link Node} if this {@link MapNode} contains no mapping for the key
103+
*/
104+
@NotNull Node<?> getOrDefault(@NotNull Object key, @NotNull Node<?> defaultNode);
93105

94106
/**
95107
* Sets a {@link Node} to the specified key.
@@ -448,7 +460,7 @@ default short getShort(@NotNull Object key) {
448460
default short getShort(@NotNull Object key, short def) {
449461
return this.raw(key) instanceof NumberValue value ? value.asShort() : def;
450462
}
451-
463+
452464
private @NotNull Node<?> raw(@NotNull Object key) {
453465
var node = this.get(key);
454466
return node instanceof CommentedNode<?> commentedNode ? commentedNode.node() : node;

core/src/main/java/com/github/siroshun09/configapi/core/node/MapNodeImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ public boolean hasValue() {
5454
}
5555

5656
@Override
57-
public @NotNull Node<?> get(@NotNull Object key) {
57+
public @NotNull Node<?> getOrDefault(@NotNull Object key, @NotNull Node<?> defaultNode) {
5858
Objects.requireNonNull(key);
59-
return this.backing.getOrDefault(key, NullNode.NULL);
59+
Objects.requireNonNull(defaultNode);
60+
return this.backing.getOrDefault(key, defaultNode);
6061
}
6162

6263
@Override

core/src/test/java/com/github/siroshun09/configapi/core/node/MapNodeTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,12 @@ void testGet() {
7979
var mapNode = MapNode.create(Map.of("a", "b", 1, 2));
8080
Assertions.assertEquals(new StringValue("b"), mapNode.get("a"));
8181
Assertions.assertEquals(new IntValue(2), mapNode.get(1));
82+
8283
Assertions.assertSame(NullNode.NULL, mapNode.get("b"));
8384
Assertions.assertSame(NullNode.NULL, MapNode.empty().get("a"));
85+
86+
Assertions.assertSame(BooleanValue.TRUE, mapNode.getOrDefault("b", BooleanValue.TRUE));
87+
Assertions.assertSame(BooleanValue.TRUE, MapNode.empty().getOrDefault("a", BooleanValue.TRUE));
8488
}
8589

8690
@Test

0 commit comments

Comments
 (0)