KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > util > IdentityWeakHashMap


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.object.util;
6
7 import sun.misc.MessageUtils;
8
9 import com.tc.exception.ExceptionWrapper;
10 import com.tc.exception.ExceptionWrapperImpl;
11
12 import java.util.Map JavaDoc;
13 import java.util.WeakHashMap JavaDoc;
14
15 public class IdentityWeakHashMap extends WeakHashMap JavaDoc {
16
17   private static class TestKey {
18     final int val;
19     boolean hashCodeCalled = false;
20     boolean equalsCalled = false;
21
22     TestKey(int val) {
23       this.val = val;
24     }
25
26     public int hashCode() {
27       // this shouldn't be happening, IdentityWeakHashMap should be using System.identityHashcode()
28
hashCodeCalled = true;
29       return val;
30     }
31
32     public boolean equals(Object JavaDoc obj) {
33       // this shouldn't be happening, IdentityWeakHashMap should be using ==
34
equalsCalled = true;
35       return (obj instanceof TestKey) && ((TestKey) obj).val == this.val;
36     }
37   }
38
39   static {
40     /**
41      * This static block is to validate the WeakHashMap superclass is the DSO instrumented version. If not, this class
42      * will function incorrectly.
43      */

44
45     IdentityWeakHashMap m = new IdentityWeakHashMap();
46     TestKey k1 = new TestKey(10); // strongly reference the Integer object so that the garbage collector
47
TestKey k2 = new TestKey(10); // will not kick in before the check for m.size().
48
m.put(k1, "first");
49     m.put(k2, "second");
50     if (m.size() != 2 || k1.hashCodeCalled || k1.equalsCalled || k2.hashCodeCalled || k1.equalsCalled) {
51       ExceptionWrapper wrapper = new ExceptionWrapperImpl();
52       String JavaDoc errorMsg = wrapper
53           .wrap("WeakHashMap does not seem to contain the instrumented method.\n"
54                 + "IdentityWeakHashMap can only be used with the Terracotta instrumented version of WeakHashMap.\n"
55                 + "One possible cause is that WeakHashMap is not contained in the boot jar.");
56       MessageUtils.toStderr(errorMsg);
57       throw new AssertionError JavaDoc(errorMsg);
58     }
59   }
60
61   public IdentityWeakHashMap(int initialCapacity, float loadFactor) {
62     super(initialCapacity, loadFactor);
63   }
64
65   public IdentityWeakHashMap(int initialCapacity) {
66     super(initialCapacity);
67   }
68
69   public IdentityWeakHashMap() {
70     super();
71   }
72
73   public IdentityWeakHashMap(Map JavaDoc m) {
74     super(m);
75   }
76
77   protected int __tc_hash(Object JavaDoc x) {
78     int h = System.identityHashCode(x);
79
80     h += ~(h << 9);
81     h ^= (h >>> 14);
82     h += (h << 4);
83     h ^= (h >>> 10);
84     return h;
85   }
86
87   protected boolean __tc_equal(Object JavaDoc obj1, Object JavaDoc obj2) {
88     return obj1 == obj2;
89   }
90
91   protected Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
92     return super.clone();
93   }
94
95 }
Popular Tags