net.jkcode.jkmvc.common.LazyAllocatedMap Maven / Gradle / Ivy
package net.jkcode.jkmvc.common;
import java.io.Serializable;
import java.util.*;
/**
* 延迟申请内存的map
* 线程不安全, 尽量在单线程中使用
*
* @deprecated 其实我搞错了, HashMap的table数组数组也是延迟创建, 只要没有put(), table即为null
* 真正的优化在于 FixedKeyMap
* @author shijianhang<[email protected]>
* @date 2019-03-31 9:43 PM
*/
public class LazyAllocatedMap extends AbstractMap implements Serializable {
/**
* 被代理的map
*/
protected Map map = Collections.EMPTY_MAP;
/**
* 获得可添加元素的map
* @return
*/
protected Map getPutableMap(){
if(map == Collections.EMPTY_MAP)
map = new HashMap();
return map;
}
@Override
public V get(final Object key) {
return map.get(key);
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public boolean containsValue(final Object value) {
return map.containsValue(value);
}
@Override
public boolean containsKey(final Object key) {
return map.containsKey(key);
}
@Override
public V put(final K key, final V value) {
return getPutableMap().put(key, value);
}
@Override
public void putAll(final Map extends K, ? extends V> m) {
getPutableMap().putAll(m);
}
@Override
public V remove(final Object key) {
if(map == Collections.EMPTY_MAP)
return null;
return map.remove(key);
}
@Override
public void clear() {
if(map == Collections.EMPTY_MAP)
return;
map.clear();
}
@Override
public Set keySet() {
return map.keySet();
}
@Override
public Collection values() {
return map.values();
}
@Override
public int size() {
return map.size();
}
@Override
public Set> entrySet() {
return map.entrySet();
}
}