Skip to content

Commit

Permalink
Merge pull request #73 from A5H73Y/master
Browse files Browse the repository at this point in the history
Fix to allow for nested sorted maps
  • Loading branch information
KotlinFactory authored May 26, 2022
2 parents d984faa + 9dacaf8 commit 5a12f6b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<artifactId>simplixstorage</artifactId>
<version>3.2.4</version>
<version>3.2.5</version>

<build>
<defaultGoal>clean verify -U</defaultGoal>
Expand Down
56 changes: 35 additions & 21 deletions src/main/java/de/leonhard/storage/internal/FileData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,21 +86,21 @@ private Object get(final Map<String, Object> 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<String, Object>) 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<String, Object>) this.localMap.get(parts[0]), parts, value, 1)
: insert(createNewMap(), parts, value, 1));
}

private Object insert(
final Map<String, Object> map, final String[] key, final Object value,
final int id) {
final Map<String, Object> map, final String[] key, final Object value,
final int id) {
if (id < key.length) {
final Map<String, Object> tempMap = new HashMap<>(map);
final Map<String, Object> tempMap = createNewMap(map);
final Map<String, Object> childMap =
map.containsKey(key[id]) && map.get(key[id]) instanceof Map
? (Map<String, Object>) map.get(key[id])
: new HashMap<>();
map.containsKey(key[id]) && map.get(key[id]) instanceof Map
? (Map<String, Object>) map.get(key[id])
: createNewMap();
tempMap.put(key[id], insert(childMap, key, value, id + 1));
return tempMap;
} else {
Expand All @@ -119,8 +120,8 @@ public boolean containsKey(final String key) {
}

private boolean containsKey(
final Map<String, Object> map, final String[] key,
final int id) {
final Map<String, Object> 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<String, Object> tempMap = (Map<String, Object>) map.get(key[id]);
Expand Down Expand Up @@ -161,9 +162,9 @@ private void remove(final @NotNull String[] key) {
}

private Map<String, Object> remove(
final Map<String, Object> map,
final String[] key,
final int keyIndex) {
final Map<String, Object> map,
final String[] key,
final int keyIndex) {
if (keyIndex < key.length - 1) {
final Object tempValue = map.get(key[keyIndex]);
if (tempValue instanceof Map) {
Expand Down Expand Up @@ -195,8 +196,9 @@ public Set<String> singleLayerKeySet() {
* @return the keySet of the given layer or an empty set if the key does not exist.
*/
public Set<String> singleLayerKeySet(final String key) {
return get(key) instanceof Map ? ((Map<String, Object>) get(key)).keySet()
: new HashSet<>();
return get(key) instanceof Map
? ((Map<String, Object>) get(key)).keySet()
: new HashSet<>();
}

/**
Expand Down Expand Up @@ -225,8 +227,8 @@ public Set<Map.Entry<String, Object>> singleLayerEntrySet() {
*/
public Set<String> keySet(final String key) {
return get(key) instanceof Map
? multiLayerKeySet((Map<String, Object>) get(key))
: new HashSet<>();
? multiLayerKeySet((Map<String, Object>) get(key))
: new HashSet<>();
}

/**
Expand All @@ -247,12 +249,12 @@ private Set<String> multiLayerKeySet(final Map<String, Object> map) {
}

private Set<Map.Entry<String, Object>> multiLayerEntrySet(
final Map<String, Object> map) {
final Map<String, Object> map) {
final Set<Map.Entry<String, Object>> out = new HashSet<>();
for (val entry : map.entrySet()) {
if (map.get(entry.getKey()) instanceof Map) {
for (final String tempKey :
multiLayerKeySet((Map<String, Object>) map.get(entry.getKey()))) {
multiLayerKeySet((Map<String, Object>) map.get(entry.getKey()))) {
out.add(new SimpleEntry<>(entry.getKey() + "." + tempKey, entry.getValue()));
}
} else {
Expand Down Expand Up @@ -330,6 +332,18 @@ public JSONObject toJsonObject() {
return JsonUtils.getJsonFromMap(this.localMap);
}

public boolean isSorted() {
return this.localMap instanceof LinkedHashMap;
}

public Map<String, Object> createNewMap() {
return isSorted() ? new LinkedHashMap<>() : new HashMap<>();
}

public Map<String, Object> createNewMap(Map<String, Object> value) {
return isSorted() ? new LinkedHashMap<>(value) : new HashMap<>(value);
}

// ----------------------------------------------------------------------------------------------------
// Overridden methods form Object
// ----------------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import java.util.LinkedHashMap;
import java.util.Map;

public abstract class MapProvider {
public interface MapProvider {

public Map<String, Object> getMapImplementation() {
default Map<String, Object> getMapImplementation() {
return new HashMap<>();
}

public Map<String, Object> getSortedMapImplementation() {
default Map<String, Object> getSortedMapImplementation() {
return new LinkedHashMap<>();
}
}

0 comments on commit 5a12f6b

Please sign in to comment.