summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/esodata/PushdownMap.java
diff options
context:
space:
mode:
authorBen Culkin <scorpress@gmail.com>2020-04-07 21:21:12 -0400
committerBen Culkin <scorpress@gmail.com>2020-04-07 21:21:12 -0400
commit780a3da69b66921fb7bf7b5779fb44830bb45ddc (patch)
tree5589db05d3379a0a910bc9c4ed469d795d83849c /base/src/main/java/bjc/utils/esodata/PushdownMap.java
parent002516bd03b2ea3f731c8139c9a5f716902ab702 (diff)
Remove utils.esodata
utils.esodata now also lives in the esodata project; not here
Diffstat (limited to 'base/src/main/java/bjc/utils/esodata/PushdownMap.java')
-rw-r--r--base/src/main/java/bjc/utils/esodata/PushdownMap.java158
1 files changed, 0 insertions, 158 deletions
diff --git a/base/src/main/java/bjc/utils/esodata/PushdownMap.java b/base/src/main/java/bjc/utils/esodata/PushdownMap.java
deleted file mode 100644
index 35dcf2d..0000000
--- a/base/src/main/java/bjc/utils/esodata/PushdownMap.java
+++ /dev/null
@@ -1,158 +0,0 @@
-package bjc.utils.esodata;
-
-import java.util.function.BiConsumer;
-import java.util.function.Consumer;
-import java.util.function.Function;
-
-import bjc.utils.funcdata.FunctionalMap;
-import bjc.utils.funcdata.IList;
-import bjc.utils.funcdata.IMap;
-
-/**
- * A variant of a map where inserting a duplicate key shadows the existing value
- * instead of replacing it.
- *
- * This could be useful for things like variable scopes.
- *
- * @author EVE
- *
- * @param <KeyType>
- * The key of the map.
- *
- * @param <ValueType>
- * The values in the map.
- */
-public class PushdownMap<KeyType, ValueType> implements IMap<KeyType, ValueType> {
- /* Our backing storage. */
- private final IMap<KeyType, Stack<ValueType>> backing;
-
- /** Create a new empty stack-based map. */
- public PushdownMap() {
- backing = new FunctionalMap<>();
- }
-
- /** Create a new empty stack-based map using the specified backing. */
- private PushdownMap(final IMap<KeyType, Stack<ValueType>> back) {
- backing = back;
- }
-
- @Override
- public void clear() {
- backing.clear();
- }
-
- @Override
- public boolean containsKey(final KeyType key) {
- return backing.containsKey(key);
- }
-
- @Override
- public IMap<KeyType, ValueType> extend() {
- return new PushdownMap<>(backing.extend());
- }
-
- @Override
- public void forEach(final BiConsumer<KeyType, ValueType> action) {
- backing.forEach((key, stk) -> action.accept(key, stk.top()));
- }
-
- @Override
- public void forEachKey(final Consumer<KeyType> action) {
- backing.forEachKey(action);
- }
-
- @Override
- public void forEachValue(final Consumer<ValueType> action) {
- backing.forEachValue(stk -> action.accept(stk.top()));
- }
-
- @Override
- public ValueType get(final KeyType key) {
- return backing.get(key).top();
- }
-
- @Override
- public int size() {
- return backing.size();
- }
-
- @Override
- public IList<KeyType> keyList() {
- return backing.keyList();
- }
-
- @Override
- public <V2> IMap<KeyType, V2> transform(final Function<ValueType, V2> transformer) {
- /*
- * @NOTE Can and should we support this? More to the point,
- * maybe this should be a map sub-type that does what it needs
- * to?
- */
- throw new UnsupportedOperationException("Cannot transform pushdown maps.");
- }
-
- @Override
- public ValueType put(final KeyType key, final ValueType val) {
- if(backing.containsKey(key)) {
- final Stack<ValueType> stk = backing.get(key);
-
- final ValueType vl = stk.top();
-
- stk.push(val);
-
- return vl;
- }
-
- final Stack<ValueType> stk = new SimpleStack<>();
-
- stk.push(val);
-
- return null;
- }
-
- @Override
- public ValueType remove(final KeyType key) {
- final Stack<ValueType> stk = backing.get(key);
-
- if(stk.size() > 1) {
- return stk.pop();
- }
-
- return backing.remove(key).top();
- }
-
- @Override
- public IList<ValueType> valueList() {
- return backing.valueList().map(Stack::top);
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
-
- int result = 1;
- result = prime * result + (backing == null ? 0 : backing.hashCode());
-
- return result;
- }
-
- @Override
- public boolean equals(final Object obj) {
- if(this == obj) return true;
- if(obj == null) return false;
- if(!(obj instanceof PushdownMap<?, ?>)) return false;
-
- final PushdownMap<?, ?> other = (PushdownMap<?, ?>) obj;
-
- if(backing == null) {
- if(other.backing != null) return false;
- } else if(!backing.equals(other.backing)) return false;
-
- return true;
- }
-
- @Override
- public String toString() {
- return String.format("PushdownMap [backing=%s]", backing);
- }
-}