org.archive.util.NestedMap Maven / Gradle / Ivy
package org.archive.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class NestedMap implements Map {
private Map inner = null;
private Map parent = null;
public NestedMap() {
this(null);
}
public NestedMap(Map parent) {
this(parent,new HashMap());
}
public NestedMap(Map parent, Map inner) {
this.parent = parent;
this.inner = inner;
}
public void setParent(Map parent) {
this.parent = parent;
}
public void clear() {
inner.clear();
parent = null;
}
public boolean containsKey(Object key) {
return inner.containsKey(key) ? true :
parent == null ? false : parent.containsKey(key);
}
public boolean containsValue(Object value) {
return inner.containsValue(value) ? true :
parent == null ? false : parent.containsValue(value);
}
public Set> entrySet() {
if(parent == null) {
return inner.entrySet();
}
HashSet> tmp =
new HashSet>();
tmp.addAll(parent.entrySet());
tmp.addAll(inner.entrySet());
return tmp;
}
public V get(Object key) {
if(inner.containsKey(key)) {
return inner.get(key);
}
return (parent == null) ? null : parent.get(key);
}
public boolean isEmpty() {
return inner.isEmpty() && ((parent == null) ? true : parent.isEmpty());
}
public Set keySet() {
if(parent == null) {
return inner.keySet();
}
HashSet tmp = new HashSet();
tmp.addAll(parent.keySet());
tmp.addAll(inner.keySet());
return tmp;
}
public V put(K key, V value) {
return inner.put(key, value);
}
public void putAll(Map extends K, ? extends V> map) {
inner.putAll(map);
}
public V remove(Object key) {
return inner.remove(key);
}
public int size() {
return keySet().size();
// return inner.size() + (parent == null ? 0 : parent.size());
}
public Collection values() {
if(parent == null) {
return inner.values();
}
ArrayList tmp = new ArrayList();
tmp.addAll(parent.values());
tmp.addAll(inner.values());
return tmp;
}
}