Skip to content

Commit

Permalink
Add a clear method
Browse files Browse the repository at this point in the history
  • Loading branch information
stonar96 committed Dec 19, 2019
1 parent 6119a07 commit 119930b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.vanillage.utils.configuration</groupId>
<artifactId>configuration</artifactId>
<version>1.1</version>
<version>1.2</version>
<name>Configuration</name>
<description>A Java configuration API with an implementation for parsing data from XML-based configuration files to a (Map) memory structure.</description>
<build>
Expand Down
34 changes: 30 additions & 4 deletions src/main/java/com/vanillage/utils/configuration/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 119930b

Please sign in to comment.