diff --git a/src/main/java/me/topchetoeu/jscript/common/environment/Environment.java b/src/main/java/me/topchetoeu/jscript/common/environment/Environment.java index 02302d0..74be652 100644 --- a/src/main/java/me/topchetoeu/jscript/common/environment/Environment.java +++ b/src/main/java/me/topchetoeu/jscript/common/environment/Environment.java @@ -3,7 +3,6 @@ package me.topchetoeu.jscript.common.environment; import java.util.HashMap; import java.util.HashSet; import java.util.Map; -import java.util.Random; import java.util.Set; import java.util.function.Supplier; @@ -12,71 +11,13 @@ public class Environment { private final Map, Object> map = new HashMap<>(); private final Set> hidden = new HashSet<>(); - private final Map, Set> multi = new HashMap<>(); - private final Map, Set> multiHidden = new HashMap<>(); - - @SuppressWarnings("unchecked") - private Set getAll(MultiKey key, boolean forceClone) { - Set parent = null, child = null; - boolean cloned = false; - - if (this.parent != null && !hidden.contains(key)) { - parent = this.parent.getAll(key, false); - if (parent.size() == 0) parent = null; - else if (multiHidden.containsKey(key)) { - parent = new HashSet<>(parent); - parent.removeAll(multiHidden.get(key)); - cloned = true; - } - } - if (multi.containsKey(key)) { - child = (Set)multi.get(key); - if (child.size() == 0) child = null; - } - - if (!forceClone) { - if (parent == null && child == null) return new HashSet<>(); - if (parent == null && child != null) return child; - if (parent != null && child == null) return parent; - } - - if (!cloned) parent = new HashSet<>(); - parent.addAll(child); - return parent; - } - private T getMulti(MultiKey key) { - return key.of(getAll(key, false)); - } - private boolean hasMulti(MultiKey key) { - return getAll(key, false).size() > 0; - } - - @SuppressWarnings("all") - private Environment addMulti(MultiKey key, T value) { - if (!multi.containsKey(key)) { - if (hidden.contains(key)) { - multiHidden.put((MultiKey)key, (Set)parent.getAll(key, true)); - hidden.remove(key); - } - - multi.put((MultiKey)key, new HashSet<>()); - } - - multi.get(key).add(value); - return this; - } - @SuppressWarnings("unchecked") public T get(Key key) { - if (key instanceof MultiKey) return getMulti((MultiKey)key); - if (map.containsKey(key)) return (T)map.get(key); else if (!hidden.contains(key) && parent != null) return parent.get(key); else return null; } public boolean has(Key key) { - if (key instanceof MultiKey) return hasMulti((MultiKey)key); - if (map.containsKey(key)) return true; else if (!hidden.contains(key) && parent != null) return parent.has(key); else return false; @@ -97,8 +38,6 @@ public class Environment { @SuppressWarnings("unchecked") public Environment add(Key key, T val) { - if (key instanceof MultiKey) return add(key, val); - map.put((Key)key, val); hidden.remove(key); return this; @@ -108,14 +47,6 @@ public class Environment { } @SuppressWarnings("all") public Environment addAll(Map, ?> map, boolean iterableAsMulti) { - for (var pair : map.entrySet()) { - if (iterableAsMulti && pair.getKey() instanceof MultiKey && pair.getValue() instanceof Iterable) { - for (var val : (Iterable)pair.getValue()) { - addMulti((MultiKey)pair.getKey(), val); - } - } - else add((Key)pair.getKey(), pair.getValue()); - } map.putAll((Map)map); hidden.removeAll(map.keySet()); return this; @@ -127,26 +58,9 @@ public class Environment { @SuppressWarnings("unchecked") public Environment remove(Key key) { map.remove(key); - multi.remove(key); - multiHidden.remove(key); hidden.add((Key)key); return this; } - @SuppressWarnings("all") - public Environment remove(MultiKey key, T val) { - if (multi.containsKey(key)) { - multi.get(key).remove(val); - multiHidden.get(key).add(val); - - if (multi.get(key).size() == 0) { - multi.remove(key); - multiHidden.remove(key); - hidden.add((Key)key); - } - } - - return this; - } public T init(Key key, T val) { if (!has(key)) this.add(key, val); @@ -180,8 +94,4 @@ public class Environment { public static Environment empty() { return new Environment(); } - - public static int nextId() { - return new Random().nextInt(); - } } diff --git a/src/main/java/me/topchetoeu/jscript/common/environment/Key.java b/src/main/java/me/topchetoeu/jscript/common/environment/Key.java index 1bb96b3..60470dd 100644 --- a/src/main/java/me/topchetoeu/jscript/common/environment/Key.java +++ b/src/main/java/me/topchetoeu/jscript/common/environment/Key.java @@ -1,7 +1,7 @@ package me.topchetoeu.jscript.common.environment; -public interface Key { +public final class Key { public static Key of() { - return new Key<>() { }; + return new Key<>(); } } diff --git a/src/main/java/me/topchetoeu/jscript/common/environment/MultiKey.java b/src/main/java/me/topchetoeu/jscript/common/environment/MultiKey.java deleted file mode 100644 index 79e8404..0000000 --- a/src/main/java/me/topchetoeu/jscript/common/environment/MultiKey.java +++ /dev/null @@ -1,7 +0,0 @@ -package me.topchetoeu.jscript.common.environment; - -import java.util.Set; - -public interface MultiKey extends Key { - public T of(Set values); -}