1 package org.hibernate.util; 3 4 import java.io.Serializable ; 5 import java.util.ArrayList ; 6 import java.util.Collection ; 7 import java.util.HashMap ; 8 import java.util.HashSet ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 import java.util.Map ; 12 import java.util.Set ; 13 import java.util.Map.Entry; 14 15 import org.apache.commons.collections.SequencedHashMap; 16 17 21 22 public final class IdentityMap implements Map { 23 24 private final Map map; 25 private transient Map.Entry [] entryArray = new Map.Entry [0]; 26 private transient boolean dirty = false; 27 28 34 public static Map instantiate(int size) { 35 return new IdentityMap( new HashMap (size) ); 36 } 37 38 42 public static Map instantiateSequenced(int size) { 43 return new IdentityMap( new SequencedHashMap(size) ); 44 } 45 46 private IdentityMap(Map underlyingMap) { 47 map = underlyingMap; 48 dirty = true; 49 } 50 51 59 public static Map.Entry [] concurrentEntries(Map map) { 60 return ( (IdentityMap) map ).entryArray(); 61 } 62 63 public static List entries(Map map) { 64 return ( (IdentityMap) map ).entryList(); 65 } 66 67 public static Iterator keyIterator(Map map) { 68 return ( (IdentityMap) map ).keyIterator(); 69 } 70 71 public Iterator keyIterator() { 72 return new KeyIterator( map.keySet().iterator() ); 73 } 74 75 public static final class IdentityMapEntry implements java.util.Map.Entry { 76 IdentityMapEntry(Object key, Object value) { 77 this.key=key; 78 this.value=value; 79 } 80 private Object key; 81 private Object value; 82 public Object getKey() { 83 return key; 84 } 85 86 public Object getValue() { 87 return value; 88 } 89 90 public Object setValue(Object value) { 91 Object result = this.value; 92 this.value = value; 93 return result; 94 } 95 } 96 97 public static final class IdentityKey implements Serializable { 98 private Object key; 99 100 IdentityKey(Object key) { 101 this.key=key; 102 } 103 public boolean equals(Object other) { 104 return key == ( (IdentityKey) other ).key; 105 } 106 public int hashCode() { 107 return System.identityHashCode(key); 108 } 109 public String toString() { 110 return key.toString(); 111 } 112 } 113 114 public int size() { 115 return map.size(); 116 } 117 118 public boolean isEmpty() { 119 return map.isEmpty(); 120 } 121 122 public boolean containsKey(Object key) { 123 IdentityKey k = new IdentityKey(key); 124 return map.containsKey(k); 125 } 126 127 public boolean containsValue(Object val) { 128 return map.containsValue(val); 129 } 130 131 public Object get(Object key) { 132 IdentityKey k = new IdentityKey(key); 133 return map.get(k); 134 } 135 136 public Object put(Object key, Object value) { 137 dirty = true; 138 return map.put( new IdentityKey(key), value ); 139 } 140 141 public Object remove(Object key) { 142 dirty = true; 143 IdentityKey k = new IdentityKey(key); 144 return map.remove(k); 145 } 146 147 public void putAll(Map otherMap) { 148 Iterator iter = otherMap.entrySet().iterator(); 149 while ( iter.hasNext() ) { 150 Map.Entry me = (Map.Entry ) iter.next(); 151 put( me.getKey(), me.getValue() ); 152 } 153 } 154 155 public void clear() { 156 dirty = true; 157 entryArray = null; 158 map.clear(); 159 } 160 161 public Set keySet() { 162 throw new UnsupportedOperationException (); 164 } 165 166 public Collection values() { 167 return map.values(); 168 } 169 170 public Set entrySet() { 171 Set set = new HashSet ( map.size() ); 172 Iterator iter = map.entrySet().iterator(); 173 while ( iter.hasNext() ) { 174 Map.Entry me = (Map.Entry ) iter.next(); 175 set.add( new IdentityMapEntry( ( (IdentityKey) me.getKey() ).key, me.getValue() ) ); 176 } 177 return set; 178 } 179 180 public List entryList() { 181 ArrayList list = new ArrayList ( map.size() ); 182 Iterator iter = map.entrySet().iterator(); 183 while ( iter.hasNext() ) { 184 Map.Entry me = (Map.Entry ) iter.next(); 185 list.add( new IdentityMapEntry( ( (IdentityKey) me.getKey() ).key, me.getValue() ) ); 186 } 187 return list; 188 } 189 190 public Map.Entry [] entryArray() { 191 if (dirty) { 192 entryArray = new Map.Entry [ map.size() ]; 193 Iterator iter = map.entrySet().iterator(); 194 int i=0; 195 while ( iter.hasNext() ) { 196 Map.Entry me = (Map.Entry ) iter.next(); 197 entryArray[i++] = new IdentityMapEntry( ( (IdentityKey) me.getKey() ).key, me.getValue() ); 198 } 199 dirty = false; 200 } 201 return entryArray; 202 } 203 204 211 public static Object serialize(Map map) { 212 return ( (IdentityMap) map ).map; 213 } 214 215 222 public static Map deserialize(Object o) { 223 return new IdentityMap( (Map ) o ); 224 } 225 226 public String toString() { 227 return map.toString(); 228 } 229 230 public static Map invert(Map map) { 231 Map result = instantiate( map.size() ); 232 Iterator iter = map.entrySet().iterator(); 233 while ( iter.hasNext() ) { 234 Map.Entry me = (Map.Entry ) iter.next(); 235 result.put( me.getValue(), me.getKey() ); 236 } 237 return result; 238 } 239 240 static final class KeyIterator implements Iterator { 241 242 private KeyIterator(Iterator iter) { 243 identityKeyIterator = iter; 244 } 245 246 private final Iterator identityKeyIterator; 247 248 public boolean hasNext() { 249 return identityKeyIterator.hasNext(); 250 } 251 252 public Object next() { 253 return ( (IdentityKey) identityKeyIterator.next() ).key; 254 } 255 256 public void remove() { 257 throw new UnsupportedOperationException (); 258 } 259 260 } 261 } 262 263 264 265 266 267 268 269 | Popular Tags |