diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-09-06 10:42:24 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-09-06 10:42:24 -0300 |
| commit | 80886fbac2de4430cb13dd333c40fdec2902ce1a (patch) | |
| tree | fd4ee35877f85d3e208d1b763a72db1365f2949c /base/src/main/java/bjc/utils/esodata/MapSet.java | |
| parent | 9b3e2e079590e9a17a32bfbdbfa81f477614449c (diff) | |
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.
Diffstat (limited to 'base/src/main/java/bjc/utils/esodata/MapSet.java')
| -rw-r--r-- | base/src/main/java/bjc/utils/esodata/MapSet.java | 97 |
1 files changed, 97 insertions, 0 deletions
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<KeyType, ValueType> extends AbstractMap<KeyType, ValueType> { + private Map<String, Map<KeyType, ValueType>> backing; + + private Map<KeyType, ValueType> currentMap = null; + + public MapSet() { + backing = new HashMap<>(); + } + + public MapSet(Map<String, Map<KeyType, ValueType>> back) { + backing = back; + } + + public void addMap(String key, Map<KeyType, ValueType> map) { + backing.put(key, map); + } + + public void clearMap() { + currentMap = null; + + backing.clear(); + } + + public boolean containsMap(String key) { + return backing.containsKey(key); + } + + public Map<KeyType, ValueType> getMap(String key) { + return backing.get(key); + } + + public Set<Map.Entry<String, Map<KeyType, ValueType>>> getMapEntries() { + return backing.entrySet(); + } + + public Set<String> getMapKeys() { + return backing.keySet(); + } + + public Collection<Map<KeyType, ValueType>> 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<KeyType, ValueType> map) { + if (!backing.containsKey(key)) { + currentMap = map; + + backing.put(key, map); + + return; + } + + currentMap = backing.get(key); + } + + @Override + public Set<Map.Entry<KeyType, ValueType>> 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); + } +} |
