KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > util > IdentityMap


1 //$Id: IdentityMap.java,v 1.6 2005/07/16 22:20:48 oneovthafew Exp $
2
package org.hibernate.util;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.HashSet JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.Set JavaDoc;
13 import java.util.Map.Entry;
14
15 import org.apache.commons.collections.SequencedHashMap;
16
17 /**
18  * A <tt>Map</tt> where keys are compared by object identity,
19  * rather than <tt>equals()</tt>.
20  */

21
22 public final class IdentityMap implements Map JavaDoc {
23
24     private final Map JavaDoc map;
25     private transient Map.Entry JavaDoc[] entryArray = new Map.Entry JavaDoc[0];
26     private transient boolean dirty = false;
27
28     /**
29      * Return a new instance of this class, with an undefined
30      * iteration order
31      *
32      * @return Map
33      */

34     public static Map JavaDoc instantiate(int size) {
35         return new IdentityMap( new HashMap JavaDoc(size) );
36     }
37
38     /**
39      * Return a new instance of this class, with iteration
40      * order defined by the order that entries were added
41      */

42     public static Map JavaDoc instantiateSequenced(int size) {
43         return new IdentityMap( new SequencedHashMap(size) );
44     }
45
46     private IdentityMap(Map JavaDoc underlyingMap) {
47         map = underlyingMap;
48         dirty = true;
49     }
50
51     /**
52      * Return the map entries (as instances of <tt>Map.Entry</tt> in a collection that
53      * is safe from concurrent modification). ie. we may safely add new instances to
54      * the underlying <tt>Map</tt> during iteration of the <tt>entries()</tt>.
55      *
56      * @param map
57      * @return Collection
58      */

59     public static Map.Entry JavaDoc[] concurrentEntries(Map JavaDoc map) {
60         return ( (IdentityMap) map ).entryArray();
61     }
62
63     public static List JavaDoc entries(Map JavaDoc map) {
64         return ( (IdentityMap) map ).entryList();
65     }
66
67     public static Iterator JavaDoc keyIterator(Map JavaDoc map) {
68         return ( (IdentityMap) map ).keyIterator();
69     }
70
71     public Iterator JavaDoc keyIterator() {
72         return new KeyIterator( map.keySet().iterator() );
73     }
74
75     public static final class IdentityMapEntry implements java.util.Map.Entry {
76         IdentityMapEntry(Object JavaDoc key, Object JavaDoc value) {
77             this.key=key;
78             this.value=value;
79         }
80         private Object JavaDoc key;
81         private Object JavaDoc value;
82         public Object JavaDoc getKey() {
83             return key;
84         }
85
86         public Object JavaDoc getValue() {
87             return value;
88         }
89
90         public Object JavaDoc setValue(Object JavaDoc value) {
91             Object JavaDoc result = this.value;
92             this.value = value;
93             return result;
94         }
95     }
96
97     public static final class IdentityKey implements Serializable JavaDoc {
98         private Object JavaDoc key;
99
100         IdentityKey(Object JavaDoc key) {
101             this.key=key;
102         }
103         public boolean equals(Object JavaDoc other) {
104             return key == ( (IdentityKey) other ).key;
105         }
106         public int hashCode() {
107             return System.identityHashCode(key);
108         }
109         public String JavaDoc 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 JavaDoc key) {
123         IdentityKey k = new IdentityKey(key);
124         return map.containsKey(k);
125     }
126
127     public boolean containsValue(Object JavaDoc val) {
128         return map.containsValue(val);
129     }
130
131     public Object JavaDoc get(Object JavaDoc key) {
132         IdentityKey k = new IdentityKey(key);
133         return map.get(k);
134     }
135
136     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
137         dirty = true;
138         return map.put( new IdentityKey(key), value );
139     }
140
141     public Object JavaDoc remove(Object JavaDoc key) {
142         dirty = true;
143         IdentityKey k = new IdentityKey(key);
144         return map.remove(k);
145     }
146
147     public void putAll(Map JavaDoc otherMap) {
148         Iterator JavaDoc iter = otherMap.entrySet().iterator();
149         while ( iter.hasNext() ) {
150             Map.Entry JavaDoc me = (Map.Entry JavaDoc) 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 JavaDoc keySet() {
162         // would need an IdentitySet for this!
163
throw new UnsupportedOperationException JavaDoc();
164     }
165
166     public Collection JavaDoc values() {
167         return map.values();
168     }
169
170     public Set JavaDoc entrySet() {
171         Set JavaDoc set = new HashSet JavaDoc( map.size() );
172         Iterator JavaDoc iter = map.entrySet().iterator();
173         while ( iter.hasNext() ) {
174             Map.Entry JavaDoc me = (Map.Entry JavaDoc) iter.next();
175             set.add( new IdentityMapEntry( ( (IdentityKey) me.getKey() ).key, me.getValue() ) );
176         }
177         return set;
178     }
179
180     public List JavaDoc entryList() {
181         ArrayList JavaDoc list = new ArrayList JavaDoc( map.size() );
182         Iterator JavaDoc iter = map.entrySet().iterator();
183         while ( iter.hasNext() ) {
184             Map.Entry JavaDoc me = (Map.Entry JavaDoc) iter.next();
185             list.add( new IdentityMapEntry( ( (IdentityKey) me.getKey() ).key, me.getValue() ) );
186         }
187         return list;
188     }
189
190     public Map.Entry JavaDoc[] entryArray() {
191         if (dirty) {
192             entryArray = new Map.Entry JavaDoc[ map.size() ];
193             Iterator JavaDoc iter = map.entrySet().iterator();
194             int i=0;
195             while ( iter.hasNext() ) {
196                 Map.Entry JavaDoc me = (Map.Entry JavaDoc) iter.next();
197                 entryArray[i++] = new IdentityMapEntry( ( (IdentityKey) me.getKey() ).key, me.getValue() );
198             }
199             dirty = false;
200         }
201         return entryArray;
202     }
203
204     /**
205      * Workaround for a JDK 1.4.1 bug where <tt>IdentityHashMap</tt>s are not
206      * correctly deserialized.
207      *
208      * @param map
209      * @return Object
210      */

211     public static Object JavaDoc serialize(Map JavaDoc map) {
212         return ( (IdentityMap) map ).map;
213     }
214
215     /**
216      * Workaround for a JDK 1.4.1 bug where <tt>IdentityHashMap</tt>s are not
217      * correctly deserialized.
218      *
219      * @param o
220      * @return Map
221      */

222     public static Map JavaDoc deserialize(Object JavaDoc o) {
223         return new IdentityMap( (Map JavaDoc) o );
224     }
225     
226     public String JavaDoc toString() {
227         return map.toString();
228     }
229
230     public static Map JavaDoc invert(Map JavaDoc map) {
231         Map JavaDoc result = instantiate( map.size() );
232         Iterator JavaDoc iter = map.entrySet().iterator();
233         while ( iter.hasNext() ) {
234             Map.Entry JavaDoc me = (Map.Entry JavaDoc) iter.next();
235             result.put( me.getValue(), me.getKey() );
236         }
237         return result;
238     }
239
240     static final class KeyIterator implements Iterator JavaDoc {
241
242         private KeyIterator(Iterator JavaDoc iter) {
243             identityKeyIterator = iter;
244         }
245
246         private final Iterator JavaDoc identityKeyIterator;
247
248         public boolean hasNext() {
249             return identityKeyIterator.hasNext();
250         }
251
252         public Object JavaDoc next() {
253             return ( (IdentityKey) identityKeyIterator.next() ).key;
254         }
255
256         public void remove() {
257             throw new UnsupportedOperationException JavaDoc();
258         }
259
260 }
261 }
262
263
264
265
266
267
268
269
Popular Tags