KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > LinkedHashMapTC


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

4 package java.util;
5
6 import com.tc.object.ObjectID;
7 import com.tc.object.bytecode.Manageable;
8 import com.tc.object.bytecode.ManagerUtil;
9
10 import java.util.HashMapTC.ValueWrapper JavaDoc;
11
12 /*
13  * This class is merged with java.util.LinkedHashMap in the bootjar. Since HashMapTC will also be merged with
14  * java.util.HashMap, this class will inherit all behavior of HashMapTC including the Manageable methods and
15  * Clearable method. It is declared abstract to make the compiler happy
16  */

17 public abstract class LinkedHashMapTC extends LinkedHashMap JavaDoc implements Manageable {
18
19   // XXX:: Its dumb to store two copies of this
20
private final boolean accessOrder;
21
22   public LinkedHashMapTC() {
23     super();
24     accessOrder = false;
25   }
26
27   public LinkedHashMapTC(int initialCapacity) {
28     super(initialCapacity);
29     accessOrder = false;
30   }
31
32   public LinkedHashMapTC(Map JavaDoc arg0) {
33     super(arg0);
34     accessOrder = false;
35   }
36
37   public LinkedHashMapTC(int initialCapacity, float loadFactor) {
38     super(initialCapacity, loadFactor);
39     accessOrder = false;
40   }
41
42   public LinkedHashMapTC(int initialCapacity, float loadFactor, boolean accessOrder) {
43     super(initialCapacity, loadFactor, accessOrder);
44     this.accessOrder = accessOrder;
45   }
46
47   // This is pretty much a C&P of the one in HashMapTC
48
public boolean containsValue(Object JavaDoc value) {
49     if (__tc_isManaged()) {
50       synchronized (__tc_managed().getResolveLock()) {
51         if (value != null) {
52           // XXX:: This is tied closely to HashMap implementation which calls equals on the passed value rather than the
53
// other way around
54
return super.containsValue(new ValueWrapper(value));
55         } else {
56           return super.containsValue(value);
57         }
58       }
59     } else {
60       return super.containsValue(value);
61     }
62   }
63
64   public Object JavaDoc get(Object JavaDoc key) {
65     if (__tc_isManaged()) {
66       synchronized (__tc_managed().getResolveLock()) {
67         Object JavaDoc val = super.get(key);
68         if (accessOrder) {
69           ManagerUtil.checkWriteAccess(this);
70           ManagerUtil.logicalInvoke(this, "get(Ljava/lang/Object;)Ljava/lang/Object;", new Object JavaDoc[] { key });
71         }
72         return lookUpAndStoreIfNecessary(key, val);
73       }
74     } else {
75       return super.get(key);
76     }
77   }
78
79   private Object JavaDoc lookUpAndStoreIfNecessary(Object JavaDoc key, Object JavaDoc value) {
80     if (value instanceof ObjectID) {
81       Object JavaDoc newVal = ManagerUtil.lookupObject((ObjectID) value);
82       Map.Entry JavaDoc e = getEntry(key);
83       // e should not be null here
84
e.setValue(newVal);
85       return newVal;
86     } else {
87       return value;
88     }
89   }
90
91 }
92
Popular Tags