Skip to content

Commit

Permalink
Make node append optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubenicos committed Jan 24, 2024
1 parent 3731bb7 commit eb4b2fb
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/main/java/com/saicone/settings/node/MapNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,15 +356,30 @@ public MapNode merge(@NotNull Map<?, ?> map, boolean replace) {
*/
@NotNull
public MapNode merge(@NotNull Map<?, ?> map, boolean replace, boolean deep) {
return merge(map, replace, deep, false);
}

/**
* Merge any type of map into map node by creating the
* required settings nodes or overriding the parent node.
*
* @param map the map to merge.
* @param replace true to replace any value that already exist.
* @param deep true to go inside subsequent maps to merge the subsequent maps inside given map.
* @param append true to append any node inside provided map into current map instead of creating a new one.
* @return this object itself.
*/
@NotNull
public MapNode merge(@NotNull Map<?, ?> map, boolean replace, boolean deep, boolean append) {
SettingsNode tempNode;
for (Entry<?, ?> entry : map.entrySet()) {
final String key = String.valueOf(entry.getKey());
if (!containsKey(entry.getKey())) {
put(key, child(key, entry.getValue()));
put(key, child(key, entry.getValue(), append));
} else if (deep && entry.getValue() instanceof Map && (tempNode = get(entry.getKey())) instanceof MapNode) {
tempNode.asMapNode().merge((Map<?, ?>) entry.getValue(), replace, true);
} else if (replace) {
final SettingsNode child = child(key, entry.getValue());
final SettingsNode child = child(key, entry.getValue(), append);
final SettingsNode replaced = put(key, child);
if (replaced != null) {
child.mergeComment(replaced);
Expand Down Expand Up @@ -497,7 +512,7 @@ protected SettingsNode child(@NotNull String key) {
}

/**
* Create or override a child node with the given key.<br>
* Create a child node with the given key and value.<br>
* This method put the node into current map.
*
* @param key the node key.
Expand All @@ -506,7 +521,21 @@ protected SettingsNode child(@NotNull String key) {
*/
@NotNull
protected SettingsNode child(@NotNull String key, @Nullable Object value) {
if (value instanceof SettingsNode) {
return child(key, value, false);
}

/**
* Create or append a child node with the given key and value.<br>
* This method put the node into current map.
*
* @param key the node key.
* @param value the node value.
* @param append true to append any settings node value into map.
* @return a newly created node or the same value.
*/
@NotNull
protected SettingsNode child(@NotNull String key, @Nullable Object value, boolean append) {
if (append && value instanceof SettingsNode) {
return ((SettingsNode) value).setParent(this);
}
final SettingsNode node = NodeKey.of(this, key, value);
Expand Down

0 comments on commit eb4b2fb

Please sign in to comment.