From 80886fbac2de4430cb13dd333c40fdec2902ce1a Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Thu, 6 Sep 2018 10:42:24 -0300 Subject: Add MapSet A MapSet is a set of maps of which one is accessible at a time, indexed by strings to pick the available one. --- base/src/main/java/bjc/utils/esodata/MapSet.java | 97 ++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 base/src/main/java/bjc/utils/esodata/MapSet.java (limited to 'base/src') diff --git a/base/src/main/java/bjc/utils/esodata/MapSet.java b/base/src/main/java/bjc/utils/esodata/MapSet.java new file mode 100644 index 0000000..94d7700 --- /dev/null +++ b/base/src/main/java/bjc/utils/esodata/MapSet.java @@ -0,0 +1,97 @@ +package bjc.utils.esodata; + +import java.util.AbstractMap; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +public class MapSet extends AbstractMap { + private Map> backing; + + private Map currentMap = null; + + public MapSet() { + backing = new HashMap<>(); + } + + public MapSet(Map> back) { + backing = back; + } + + public void addMap(String key, Map map) { + backing.put(key, map); + } + + public void clearMap() { + currentMap = null; + + backing.clear(); + } + + public boolean containsMap(String key) { + return backing.containsKey(key); + } + + public Map getMap(String key) { + return backing.get(key); + } + + public Set>> getMapEntries() { + return backing.entrySet(); + } + + public Set getMapKeys() { + return backing.keySet(); + } + + public Collection> getMapValues() { + return backing.values(); + } + + public boolean setMap(String key) { + if (!backing.containsKey(key)) return false; + + currentMap = backing.get(key); + + return true; + } + + public void setCreateMap(String key) { + if (!backing.containsKey(key)) { + currentMap = new HashMap<>(); + + backing.put(key, currentMap); + + return; + } + + currentMap = backing.get(key); + } + + public void setPutMap(String key, Map map) { + if (!backing.containsKey(key)) { + currentMap = map; + + backing.put(key, map); + + return; + } + + currentMap = backing.get(key); + } + + @Override + public Set> entrySet() { + if (currentMap == null) throw new NullPointerException("Current map is not set"); + + return currentMap.entrySet(); + } + + @Override + public ValueType put(KeyType key, ValueType value) { + if (currentMap == null) throw new NullPointerException("Current map is not set"); + + return currentMap.put(key, value); + } +} -- cgit v1.2.3