一、前言
关于hibernate(3.0.1)源码包中org.hibernate.util.IdentityMap主键映射关系,基于org.apache.commons.collections.SequencedHashMap的序列哈希关系实现java.util.Map接口,详情参见源码部分。
二、源码说明
![]() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | package org.hibernate.util; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.apache.commons.collections.SequencedHashMap; public final class IdentityMap implements Map { private final Map map; private transient Map.Entry[] entryArray = new Map.Entry[ 0 ]; private transient boolean dirty = false ; public static Map instantiate( int size) { return new IdentityMap( new HashMap(size)); } public static Map instantiateSequenced( int size) { return new IdentityMap( new SequencedHashMap(size)); } private IdentityMap(Map underlyingMap) { this .map = underlyingMap; this .dirty = true ; } public static Map.Entry[] concurrentEntries(Map map) { return ((IdentityMap)map).entryArray(); } public static List entries(Map map) { return ((IdentityMap)map).entryList(); } public static Iterator keyIterator(Map map) { return ((IdentityMap)map).keyIterator(); } public Iterator keyIterator() { return new KeyIterator( this .map.keySet().iterator(), null ); } public int size() { return this .map.size(); } public boolean isEmpty() { return this .map.isEmpty(); } public boolean containsKey(Object key) { IdentityKey k = new IdentityKey(key); return this .map.containsKey(k); } public boolean containsValue(Object val) { return this .map.containsValue(val); } public Object get(Object key) { IdentityKey k = new IdentityKey(key); return this .map.get(k); } public Object put(Object key, Object value) { this .dirty = true ; return this .map.put( new IdentityKey(key), value); } public Object remove(Object key) { this .dirty = true ; IdentityKey k = new IdentityKey(key); return this .map.remove(k); } public void putAll(Map otherMap) { Iterator iter = otherMap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry me = (Map.Entry)iter.next(); put(me.getKey(), me.getValue()); } } public void clear() { this .dirty = true ; this .map.clear(); } public Set keySet() { throw new UnsupportedOperationException(); } public Collection values() { return this .map.values(); } public Set entrySet() { Set set = new HashSet( this .map.size()); Iterator iter = this .map.entrySet().iterator(); while (iter.hasNext()) { Map.Entry me = (Map.Entry)iter.next(); set.add( new IdentityMapEntry(IdentityKey.access$ 100 ((IdentityKey)me.getKey()), me.getValue())); } return set; } public List entryList() { ArrayList list = new ArrayList( this .map.size()); Iterator iter = this .map.entrySet().iterator(); while (iter.hasNext()) { Map.Entry me = (Map.Entry)iter.next(); list.add( new IdentityMapEntry(IdentityKey.access$ 100 ((IdentityKey)me.getKey()), me.getValue())); } return list; } public Map.Entry[] entryArray() { if ( this .dirty) { this .entryArray = new Map.Entry[ this .map.size()]; Iterator iter = this .map.entrySet().iterator(); int i = 0 ; while (iter.hasNext()) { Map.Entry me = (Map.Entry)iter.next(); this .entryArray[(i++)] = new IdentityMapEntry(IdentityKey.access$ 100 ((IdentityKey)me.getKey()), me.getValue()); } this .dirty = false ; } return this .entryArray; } public static Object serialize(Map map) { return ((IdentityMap)map).map; } public static Map deserialize(Object o) { return new IdentityMap((Map)o); } public String toString() { return this .map.toString(); } static final class KeyIterator implements Iterator { private final Iterator identityKeyIterator; private KeyIterator(Iterator iter) { this .identityKeyIterator = iter; } public boolean hasNext() { return this .identityKeyIterator.hasNext(); } public Object next() { return IdentityMap.IdentityKey.access$ 100 ((IdentityMap.IdentityKey) this .identityKeyIterator.next()); } public void remove() { throw new UnsupportedOperationException(); } KeyIterator(Iterator x0, IdentityMap. 1 x1) { this (x0); } } public static final class IdentityKey implements Serializable { private Object key; IdentityKey(Object key) { this .key = key; } public boolean equals(Object other) { return ( this .key == ((IdentityKey)other).key); } public int hashCode() { return System.identityHashCode( this .key); } public String toString() { return this .key.toString(); } static Object access$ 100 (IdentityKey x0) { return x0.key; } } public static final class IdentityMapEntry implements Map.Entry { private Object key; private Object value; IdentityMapEntry(Object key, Object value) { this .key = key; this .value = value; } public Object getKey() { return this .key; } public Object getValue() { return this .value; } public Object setValue(Object value) { Object result = this .value; this .value = value; return result; } } } |