一、前言
关于hibernate(3.0.1)源码包中org.hibernate.util.FastHashMap类, 基于java.util.HashMap实现java.util.Map的同时,对于其并发场景下线程不安全进行了优化。
二、源码说明
package org.hibernate.util;@b@@b@import java.io.Serializable;@b@import java.util.Collection;@b@import java.util.HashMap;@b@import java.util.Iterator;@b@import java.util.Map;@b@import java.util.Map.Entry;@b@import java.util.Set;@b@@b@public final class FastHashMap@b@ implements Map, Serializable@b@{@b@ private HashMap map = null;@b@@b@ public FastHashMap()@b@ {@b@ this.map = new HashMap();@b@ }@b@@b@ public FastHashMap(int capacity)@b@ {@b@ this.map = new HashMap(capacity);@b@ }@b@@b@ public FastHashMap(int capacity, float factor)@b@ {@b@ this.map = new HashMap(capacity, factor);@b@ }@b@@b@ public FastHashMap(Map map)@b@ {@b@ this.map = new HashMap(map);@b@ }@b@@b@ public void clear()@b@ {@b@ synchronized (this) {@b@ HashMap temp = (HashMap)this.map.clone();@b@ temp.clear();@b@ this.map = temp;@b@ }@b@ }@b@@b@ public Object clone()@b@ {@b@ return new FastHashMap(this.map);@b@ }@b@@b@ public boolean containsKey(Object key)@b@ {@b@ return this.map.containsKey(key);@b@ }@b@@b@ public boolean containsValue(Object value)@b@ {@b@ return this.map.containsValue(value);@b@ }@b@@b@ public Set entrySet()@b@ {@b@ return this.map.entrySet();@b@ }@b@@b@ public boolean equals(Object o)@b@ {@b@ if (o == this)@b@ return true;@b@ if (!(o instanceof Serializable))@b@ return false;@b@ Map mo = (Serializable)o;@b@@b@ if (mo.size() != this.map.size())@b@ return false;@b@ Iterator i = this.map.entrySet().iterator();@b@ while (i.hasNext()) {@b@ Map.Entry e = (Map.Entry)i.next();@b@ Object key = e.getKey();@b@ Object value = e.getValue();@b@ if (value == null) {@b@ if ((mo.get(key) == null) && (mo.containsKey(key))) continue;@b@ return false;@b@ }@b@@b@ if (!(value.equals(mo.get(key))))@b@ return false;@b@ }@b@@b@ return true;@b@ }@b@@b@ public Object get(Object key)@b@ {@b@ return this.map.get(key);@b@ }@b@@b@ public int hashCode()@b@ {@b@ int h = 0;@b@ Iterator i = this.map.entrySet().iterator();@b@ while (i.hasNext())@b@ h += i.next().hashCode();@b@ return h;@b@ }@b@@b@ public boolean isEmpty()@b@ {@b@ return this.map.isEmpty();@b@ }@b@@b@ public Set keySet()@b@ {@b@ return this.map.keySet();@b@ }@b@@b@ public Object put(Object key, Object value)@b@ {@b@ synchronized (this) {@b@ HashMap temp = (HashMap)this.map.clone();@b@ Object result = temp.put(key, value);@b@ this.map = temp;@b@ return result;@b@ }@b@ }@b@@b@ public void putAll(Map in)@b@ {@b@ synchronized (this) {@b@ HashMap temp = (HashMap)this.map.clone();@b@ temp.putAll(in);@b@ this.map = temp;@b@ }@b@ }@b@@b@ public Object remove(Object key)@b@ {@b@ synchronized (this) {@b@ HashMap temp = (HashMap)this.map.clone();@b@ Object result = temp.remove(key);@b@ this.map = temp;@b@ return result;@b@ }@b@ }@b@@b@ public int size()@b@ {@b@ return this.map.size();@b@ }@b@@b@ public Collection values()@b@ {@b@ return this.map.values();@b@ }@b@@b@ public String toString() {@b@ return this.map.toString();@b@ }@b@}