KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > util > IdentityHashMap


1 package org.jacorb.util;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.Set JavaDoc;
6
7 /**
8  * A simple implementation of an IdentityHashMap, as it is introduced in
9  * JDK 1.4. This version here is not fully implemented and not particularly
10  * efficient, and serves only as a substitute when running under earlier JDKs.
11  *
12  * (In an IdentityHashMap, two keys k1 and k2 are considered equal if and only
13  * if k1 == k2.)
14  *
15  * @author <a HREF="mailto:spiegel@gnu.org">Andre Spiegel</a>
16  * @version $Id: IdentityHashMap.java,v 1.1 2003/11/11 23:03:13 andre.spiegel Exp $
17  */

18 public class IdentityHashMap extends HashMap JavaDoc
19 {
20     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
21     {
22         return super.put(new IdentityWrapper(key), value);
23     }
24
25     public Object JavaDoc get(Object JavaDoc key)
26     {
27         return super.get(new IdentityWrapper(key));
28     }
29
30     public Object JavaDoc remove(Object JavaDoc key)
31     {
32         return super.remove(new IdentityWrapper(key));
33     }
34
35     public boolean containsKey(Object JavaDoc key)
36     {
37         return super.containsKey(new IdentityWrapper(key));
38     }
39
40     public boolean containsValue(Object JavaDoc value)
41     {
42         throw new RuntimeException JavaDoc("containsValue() not implemented");
43     }
44
45     public Set JavaDoc entrySet()
46     {
47         throw new RuntimeException JavaDoc("entrySet() not implemented");
48     }
49
50     public Set JavaDoc keySet()
51     {
52         throw new RuntimeException JavaDoc("keySet() not implemented");
53     }
54
55     public void putAll(Map JavaDoc m)
56     {
57         throw new RuntimeException JavaDoc("putAll() not implemented");
58     }
59
60     /**
61      * A wrapper around an object that redefines the equals method
62      * as an identity test.
63      */

64     private static class IdentityWrapper
65     {
66         private Object JavaDoc value;
67
68         public IdentityWrapper(Object JavaDoc value)
69         {
70             this.value = value;
71         }
72
73         public boolean equals(Object JavaDoc other)
74         {
75             if (other instanceof IdentityWrapper)
76                 return this.value == ((IdentityWrapper)other).value;
77             else
78                 return false;
79         }
80         
81         public int hashCode()
82         {
83             return value.hashCode();
84         }
85     }
86
87 }
88
Popular Tags