一、前言
关于hibernate(3.0.1)源码包中org.hibernate.util.IdentityMap主键映射关系,基于org.apache.commons.collections.SequencedHashMap的序列哈希关系实现java.util.Map接口,详情参见源码部分。
二、源码说明
package org.hibernate.util;@b@@b@import java.io.Serializable;@b@import java.util.ArrayList;@b@import java.util.Collection;@b@import java.util.HashMap;@b@import java.util.HashSet;@b@import java.util.Iterator;@b@import java.util.List;@b@import java.util.Map;@b@import java.util.Map.Entry;@b@import java.util.Set;@b@import org.apache.commons.collections.SequencedHashMap;@b@@b@public final class IdentityMap@b@ implements Map@b@{@b@ private final Map map;@b@ private transient Map.Entry[] entryArray = new Map.Entry[0];@b@ private transient boolean dirty = false;@b@@b@ public static Map instantiate(int size)@b@ {@b@ return new IdentityMap(new HashMap(size));@b@ }@b@@b@ public static Map instantiateSequenced(int size)@b@ {@b@ return new IdentityMap(new SequencedHashMap(size));@b@ }@b@@b@ private IdentityMap(Map underlyingMap) {@b@ this.map = underlyingMap;@b@ this.dirty = true;@b@ }@b@@b@ public static Map.Entry[] concurrentEntries(Map map)@b@ {@b@ return ((IdentityMap)map).entryArray();@b@ }@b@@b@ public static List entries(Map map) {@b@ return ((IdentityMap)map).entryList();@b@ }@b@@b@ public static Iterator keyIterator(Map map) {@b@ return ((IdentityMap)map).keyIterator();@b@ }@b@@b@ public Iterator keyIterator() {@b@ return new KeyIterator(this.map.keySet().iterator(), null);@b@ }@b@@b@ public int size()@b@ {@b@ return this.map.size();@b@ }@b@@b@ public boolean isEmpty() {@b@ return this.map.isEmpty();@b@ }@b@@b@ public boolean containsKey(Object key) {@b@ IdentityKey k = new IdentityKey(key);@b@ return this.map.containsKey(k);@b@ }@b@@b@ public boolean containsValue(Object val) {@b@ return this.map.containsValue(val);@b@ }@b@@b@ public Object get(Object key) {@b@ IdentityKey k = new IdentityKey(key);@b@ return this.map.get(k);@b@ }@b@@b@ public Object put(Object key, Object value) {@b@ this.dirty = true;@b@ return this.map.put(new IdentityKey(key), value);@b@ }@b@@b@ public Object remove(Object key) {@b@ this.dirty = true;@b@ IdentityKey k = new IdentityKey(key);@b@ return this.map.remove(k);@b@ }@b@@b@ public void putAll(Map otherMap) {@b@ Iterator iter = otherMap.entrySet().iterator();@b@ while (iter.hasNext()) {@b@ Map.Entry me = (Map.Entry)iter.next();@b@ put(me.getKey(), me.getValue());@b@ }@b@ }@b@@b@ public void clear() {@b@ this.dirty = true;@b@ this.map.clear();@b@ }@b@@b@ public Set keySet()@b@ {@b@ throw new UnsupportedOperationException();@b@ }@b@@b@ public Collection values() {@b@ return this.map.values();@b@ }@b@@b@ public Set entrySet() {@b@ Set set = new HashSet(this.map.size());@b@ Iterator iter = this.map.entrySet().iterator();@b@ while (iter.hasNext()) {@b@ Map.Entry me = (Map.Entry)iter.next();@b@ set.add(new IdentityMapEntry(IdentityKey.access$100((IdentityKey)me.getKey()), me.getValue()));@b@ }@b@ return set;@b@ }@b@@b@ public List entryList() {@b@ ArrayList list = new ArrayList(this.map.size());@b@ Iterator iter = this.map.entrySet().iterator();@b@ while (iter.hasNext()) {@b@ Map.Entry me = (Map.Entry)iter.next();@b@ list.add(new IdentityMapEntry(IdentityKey.access$100((IdentityKey)me.getKey()), me.getValue()));@b@ }@b@ return list;@b@ }@b@@b@ public Map.Entry[] entryArray() {@b@ if (this.dirty) {@b@ this.entryArray = new Map.Entry[this.map.size()];@b@ Iterator iter = this.map.entrySet().iterator();@b@ int i = 0;@b@ while (iter.hasNext()) {@b@ Map.Entry me = (Map.Entry)iter.next();@b@ this.entryArray[(i++)] = new IdentityMapEntry(IdentityKey.access$100((IdentityKey)me.getKey()), me.getValue());@b@ }@b@ this.dirty = false;@b@ }@b@ return this.entryArray;@b@ }@b@@b@ public static Object serialize(Map map)@b@ {@b@ return ((IdentityMap)map).map;@b@ }@b@@b@ public static Map deserialize(Object o)@b@ {@b@ return new IdentityMap((Map)o);@b@ }@b@@b@ public String toString() {@b@ return this.map.toString(); }@b@@b@ static final class KeyIterator implements Iterator {@b@ private final Iterator identityKeyIterator;@b@@b@ private KeyIterator(Iterator iter) {@b@ this.identityKeyIterator = iter;@b@ }@b@@b@ public boolean hasNext()@b@ {@b@ return this.identityKeyIterator.hasNext();@b@ }@b@@b@ public Object next() {@b@ return IdentityMap.IdentityKey.access$100((IdentityMap.IdentityKey)this.identityKeyIterator.next());@b@ }@b@@b@ public void remove() {@b@ throw new UnsupportedOperationException();@b@ }@b@@b@ KeyIterator(Iterator x0, IdentityMap.1 x1)@b@ {@b@ this(x0);@b@ }@b@ }@b@@b@ public static final class IdentityKey@b@ implements Serializable@b@ {@b@ private Object key;@b@@b@ IdentityKey(Object key)@b@ {@b@ this.key = key; }@b@@b@ public boolean equals(Object other) {@b@ return (this.key == ((IdentityKey)other).key); }@b@@b@ public int hashCode() {@b@ return System.identityHashCode(this.key); }@b@@b@ public String toString() {@b@ return this.key.toString();@b@ }@b@@b@ static Object access$100(IdentityKey x0)@b@ {@b@ return x0.key;@b@ }@b@ }@b@@b@ public static final class IdentityMapEntry@b@ implements Map.Entry@b@ {@b@ private Object key;@b@ private Object value;@b@@b@ IdentityMapEntry(Object key, Object value)@b@ {@b@ this.key = key;@b@ this.value = value;@b@ }@b@@b@ public Object getKey()@b@ {@b@ return this.key;@b@ }@b@@b@ public Object getValue() {@b@ return this.value;@b@ }@b@@b@ public Object setValue(Object value) {@b@ Object result = this.value;@b@ this.value = value;@b@ return result;@b@ }@b@ }@b@}