From e921e50c3cf9f5589b857da6797680368990d8eb Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 23 Oct 2016 07:30:12 +0800 Subject: [PATCH] Rewrite FilePreferences --- .../util/prefs/PropertyFileBackingStore.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/source/net/filebot/util/prefs/PropertyFileBackingStore.java b/source/net/filebot/util/prefs/PropertyFileBackingStore.java index a7ee6a6a..8b843bcb 100644 --- a/source/net/filebot/util/prefs/PropertyFileBackingStore.java +++ b/source/net/filebot/util/prefs/PropertyFileBackingStore.java @@ -13,11 +13,17 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Properties; +import java.util.regex.Pattern; +import java.util.stream.IntStream; public class PropertyFileBackingStore { + private static final char nodeSeparatorChar = '/'; + private static final Pattern nodeSeparatorPattern = Pattern.compile(String.valueOf(nodeSeparatorChar), Pattern.LITERAL); + private Path store; private int modCount = 0; @@ -66,7 +72,21 @@ public class PropertyFileBackingStore { } public synchronized String[] getChildren(String node) { - return nodes.keySet().stream().filter(k -> k.length() > node.length() && k.indexOf('/', node.length()) < 0 && k.startsWith(node)).map(k -> k.substring(node.length())).toArray(String[]::new); + HashSet keys = new HashSet(); + String[] path = node.isEmpty() ? new String[0] : nodeSeparatorPattern.split(node); + + for (String n : nodes.keySet()) { + String[] p = nodeSeparatorPattern.split(n); + + if (p.length > path.length) { + if (path.length == 0 || IntStream.range(0, path.length).allMatch(i -> p[i].equals(path[i]))) { + keys.add(p[path.length]); + } + } + } + + return keys.toArray(new String[0]); + } public synchronized Properties toProperties() { @@ -74,7 +94,7 @@ public class PropertyFileBackingStore { nodes.forEach((node, values) -> { values.forEach((key, value) -> { - props.put(node + '/' + key, value); + props.put(node + nodeSeparatorChar + key, value); }); }); @@ -107,7 +127,7 @@ public class PropertyFileBackingStore { props.forEach((k, v) -> { String propertyKey = k.toString(); - int s = propertyKey.lastIndexOf('/'); + int s = propertyKey.lastIndexOf(nodeSeparatorChar); String node = propertyKey.substring(0, s); String key = propertyKey.substring(s + 1);