1 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 ; 13 import java.util.WeakHashMap ; 14 15 public class IdentityWeakHashMap extends WeakHashMap { 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 hashCodeCalled = true; 29 return val; 30 } 31 32 public boolean equals(Object obj) { 33 equalsCalled = true; 35 return (obj instanceof TestKey) && ((TestKey) obj).val == this.val; 36 } 37 } 38 39 static { 40 44 45 IdentityWeakHashMap m = new IdentityWeakHashMap(); 46 TestKey k1 = new TestKey(10); TestKey k2 = new TestKey(10); 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 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 (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 m) { 74 super(m); 75 } 76 77 protected int __tc_hash(Object 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 obj1, Object obj2) { 88 return obj1 == obj2; 89 } 90 91 protected Object clone() throws CloneNotSupportedException { 92 return super.clone(); 93 } 94 95 } | Popular Tags |