net.sf.xmlform.util.ModifiabilityMap Maven / Gradle / Ivy
package net.sf.xmlform.util;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* @author Liu Zhikun
*/
public class ModifiabilityMap implements Map{
private Map m,u;
private Modifiability modifiability;
public ModifiabilityMap(Modifiability modifi) {
this(modifi,new HashMap());
}
public ModifiabilityMap(Modifiability modifi,int initialCapacity) {
this(modifi,new HashMap(initialCapacity));
}
public ModifiabilityMap(Modifiability modifi,Map map) {
modifiability=modifi;
m=map;
u=Collections.unmodifiableMap(map);
}
public Modifiability getModifiability() {
return modifiability;
}
public void setModifiability(Modifiability modifiability) {
this.modifiability = modifiability;
}
@Override
public int size() {
return m.size();
}
@Override
public boolean isEmpty() {
return m.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return m.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return m.containsValue(value);
}
@Override
public V get(Object key) {
return m.get(key);
}
@Override
public V put(K key, V value) {
return isModifiable()?m.put(key, value):u.put(key, value);
}
@Override
public V remove(Object key) {
return isModifiable()?m.remove(key):u.remove(key);
}
@Override
public void putAll(Map extends K, ? extends V> map) {
if(isModifiable())
m.putAll(map);
else
u.putAll(map);
}
@Override
public void clear() {
if(isModifiable())
m.clear();
else
u.clear();
}
@Override
public Set keySet() {
return isModifiable()?m.keySet():u.keySet();
}
@Override
public Collection values() {
return isModifiable()?m.values():u.values();
}
@Override
public Set> entrySet() {
return isModifiable()?m.entrySet():u.entrySet();
}
@Override
public int hashCode() {
return m.hashCode();
}
@Override
public boolean equals(Object obj) {
if(obj==null||!(obj instanceof ModifiabilityMap))
return false;
return m.equals(obj);
}
@Override
public String toString() {
return m.toString();
}
private boolean isModifiable() {
if(modifiability==null)
return true;
return modifiability.isModifiable();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy