From 119930b26fb1052eacfee3487ca31c802963008d Mon Sep 17 00:00:00 2001 From: stonar96 Date: Thu, 19 Dec 2019 19:49:24 +0100 Subject: [PATCH] Add a clear method --- pom.xml | 2 +- .../utils/configuration/Configuration.java | 34 ++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 1dd2ded..a474a32 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 com.vanillage.utils.configuration configuration - 1.1 + 1.2 Configuration A Java configuration API with an implementation for parsing data from XML-based configuration files to a (Map) memory structure. diff --git a/src/main/java/com/vanillage/utils/configuration/Configuration.java b/src/main/java/com/vanillage/utils/configuration/Configuration.java index 96138ed..dca8c51 100644 --- a/src/main/java/com/vanillage/utils/configuration/Configuration.java +++ b/src/main/java/com/vanillage/utils/configuration/Configuration.java @@ -284,16 +284,42 @@ public Object set(Object value, Object... path) { // insert, set, create, constr } } + public Object clear(Object... path) { // delete, remove, reset, clear, erase + if (path == null) { + throw new IllegalArgumentException("path cannot be null"); + } + + Map previous = null; + Object object = content; + + for (int i = 0; i < path.length; i++) { + if (object instanceof Map) { + previous = (Map) object; + object = previous.get(path[i]); + } else { + return NoValue.INSTANCE; + } + } + + if (object == null && (!configurationOptions.isNullTreatedAsValue() || previous != null && !previous.containsKey(path[path.length - 1]))) { + object = NoValue.INSTANCE; + } + + if (previous == null) { // can be merged with the if statement above + content = new LinkedHashMap<>(); + } else { + previous.remove(path[path.length - 1]); // not necessary if previous doesn't contain the path + } + + return object; + } + /*public boolean exists(Object... path) { // exists, contains, isSet return false; } public Object insertIfAbsent(Object value, Object... path) { // insert, set, create, construct, add, put return null; - } - - public Object delete(Object... path) { // delete, remove, reset, clear, erase - return null; }*/ public static enum NoValue {