From 9dacaf8afa57fbef8ad4d2e930ce485dd5f5e675 Mon Sep 17 00:00:00 2001 From: A5H73Y Date: Wed, 25 May 2022 22:15:59 +0100 Subject: [PATCH] Fix to allow for nested sorted maps --- pom.xml | 2 +- .../leonhard/storage/internal/FileData.java | 56 ++++++++++++------- .../internal/provider/MapProvider.java | 6 +- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/pom.xml b/pom.xml index 49eacac1..dfa48c39 100755 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> simplixstorage - 3.2.4 + 3.2.5 clean verify -U diff --git a/src/main/java/de/leonhard/storage/internal/FileData.java b/src/main/java/de/leonhard/storage/internal/FileData.java index 60f2272d..20c27481 100755 --- a/src/main/java/de/leonhard/storage/internal/FileData.java +++ b/src/main/java/de/leonhard/storage/internal/FileData.java @@ -5,6 +5,7 @@ import java.util.AbstractMap.SimpleEntry; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import lombok.val; @@ -85,21 +86,21 @@ private Object get(final Map map, final String[] key, final int public synchronized void insert(final String key, final Object value) { final String[] parts = key.split("\\."); this.localMap.put( - parts[0], - this.localMap.containsKey(parts[0]) && this.localMap.get(parts[0]) instanceof Map - ? insert((Map) this.localMap.get(parts[0]), parts, value, 1) - : insert(new HashMap<>(), parts, value, 1)); + parts[0], + this.localMap.containsKey(parts[0]) && this.localMap.get(parts[0]) instanceof Map + ? insert((Map) this.localMap.get(parts[0]), parts, value, 1) + : insert(createNewMap(), parts, value, 1)); } private Object insert( - final Map map, final String[] key, final Object value, - final int id) { + final Map map, final String[] key, final Object value, + final int id) { if (id < key.length) { - final Map tempMap = new HashMap<>(map); + final Map tempMap = createNewMap(map); final Map childMap = - map.containsKey(key[id]) && map.get(key[id]) instanceof Map - ? (Map) map.get(key[id]) - : new HashMap<>(); + map.containsKey(key[id]) && map.get(key[id]) instanceof Map + ? (Map) map.get(key[id]) + : createNewMap(); tempMap.put(key[id], insert(childMap, key, value, id + 1)); return tempMap; } else { @@ -119,8 +120,8 @@ public boolean containsKey(final String key) { } private boolean containsKey( - final Map map, final String[] key, - final int id) { + final Map map, final String[] key, + final int id) { if (id < key.length - 1) { if (map.containsKey(key[id]) && map.get(key[id]) instanceof Map) { final Map tempMap = (Map) map.get(key[id]); @@ -161,9 +162,9 @@ private void remove(final @NotNull String[] key) { } private Map remove( - final Map map, - final String[] key, - final int keyIndex) { + final Map map, + final String[] key, + final int keyIndex) { if (keyIndex < key.length - 1) { final Object tempValue = map.get(key[keyIndex]); if (tempValue instanceof Map) { @@ -195,8 +196,9 @@ public Set singleLayerKeySet() { * @return the keySet of the given layer or an empty set if the key does not exist. */ public Set singleLayerKeySet(final String key) { - return get(key) instanceof Map ? ((Map) get(key)).keySet() - : new HashSet<>(); + return get(key) instanceof Map + ? ((Map) get(key)).keySet() + : new HashSet<>(); } /** @@ -225,8 +227,8 @@ public Set> singleLayerEntrySet() { */ public Set keySet(final String key) { return get(key) instanceof Map - ? multiLayerKeySet((Map) get(key)) - : new HashSet<>(); + ? multiLayerKeySet((Map) get(key)) + : new HashSet<>(); } /** @@ -247,12 +249,12 @@ private Set multiLayerKeySet(final Map map) { } private Set> multiLayerEntrySet( - final Map map) { + final Map map) { final Set> out = new HashSet<>(); for (val entry : map.entrySet()) { if (map.get(entry.getKey()) instanceof Map) { for (final String tempKey : - multiLayerKeySet((Map) map.get(entry.getKey()))) { + multiLayerKeySet((Map) map.get(entry.getKey()))) { out.add(new SimpleEntry<>(entry.getKey() + "." + tempKey, entry.getValue())); } } else { @@ -330,6 +332,18 @@ public JSONObject toJsonObject() { return JsonUtils.getJsonFromMap(this.localMap); } + public boolean isSorted() { + return this.localMap instanceof LinkedHashMap; + } + + public Map createNewMap() { + return isSorted() ? new LinkedHashMap<>() : new HashMap<>(); + } + + public Map createNewMap(Map value) { + return isSorted() ? new LinkedHashMap<>(value) : new HashMap<>(value); + } + // ---------------------------------------------------------------------------------------------------- // Overridden methods form Object // ---------------------------------------------------------------------------------------------------- diff --git a/src/main/java/de/leonhard/storage/internal/provider/MapProvider.java b/src/main/java/de/leonhard/storage/internal/provider/MapProvider.java index a90a8d23..d54f492f 100644 --- a/src/main/java/de/leonhard/storage/internal/provider/MapProvider.java +++ b/src/main/java/de/leonhard/storage/internal/provider/MapProvider.java @@ -4,13 +4,13 @@ import java.util.LinkedHashMap; import java.util.Map; -public abstract class MapProvider { +public interface MapProvider { - public Map getMapImplementation() { + default Map getMapImplementation() { return new HashMap<>(); } - public Map getSortedMapImplementation() { + default Map getSortedMapImplementation() { return new LinkedHashMap<>(); } }