KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > applicator > HashMapApplicator


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 com.tc.object.applicator;
5
6 import com.tc.object.ClientObjectManager;
7 import com.tc.object.ObjectID;
8 import com.tc.object.SerializationUtil;
9 import com.tc.object.TCObject;
10 import com.tc.object.TraversedReferences;
11 import com.tc.object.bytecode.Manageable;
12 import com.tc.object.bytecode.TCMap;
13 import com.tc.object.dna.api.DNA;
14 import com.tc.object.dna.api.DNACursor;
15 import com.tc.object.dna.api.DNAWriter;
16 import com.tc.object.dna.api.LogicalAction;
17 import com.tc.object.dna.impl.DNAEncoding;
18 import com.tc.object.tx.optimistic.OptimisticTransactionManager;
19 import com.tc.object.tx.optimistic.TCObjectClone;
20
21 import java.io.IOException JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.IdentityHashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Map.Entry;
27
28 /**
29  * Apply a logical action to an object
30  */

31 public class HashMapApplicator extends BaseApplicator {
32
33   public HashMapApplicator(DNAEncoding encoding) {
34     super(encoding);
35   }
36
37   public TraversedReferences getPortableObjects(Object JavaDoc pojo, TraversedReferences addTo) {
38     Map m = (Map) pojo;
39     filterPortableObjects(m.keySet(), addTo);
40     filterPortableObjects(m.values(), addTo);
41     return addTo;
42   }
43
44   private void filterPortableObjects(Collection JavaDoc objects, TraversedReferences addTo) {
45     for (Iterator JavaDoc i = objects.iterator(); i.hasNext();) {
46       Object JavaDoc o = i.next();
47       if (o != null && isPortableReference(o.getClass())) {
48         addTo.addAnonymousReference(o);
49       }
50     }
51   }
52
53   /**
54    * return the key value pairs of field names to shared objects for this source. We already updated the literals and
55    * set the new TCObjectClone
56    */

57   public Map connectedCopy(Object JavaDoc source, Object JavaDoc dest, Map visited, ClientObjectManager objectManager,
58                            OptimisticTransactionManager txManager) {
59     Map cloned = new IdentityHashMap JavaDoc();
60
61     Manageable sourceManageable = (Manageable) source;
62     Manageable destManaged = (Manageable) dest;
63
64     Map sourceMap = (Map) source;
65     Map destMap = (Map) dest;
66
67     for (Iterator JavaDoc i = sourceMap.entrySet().iterator(); i.hasNext();) {
68       Entry e = (Entry) i.next();
69
70       Object JavaDoc copyKey = createCopyIfNecessary(objectManager, visited, cloned, e.getKey());
71       Object JavaDoc copyValue = createCopyIfNecessary(objectManager, visited, cloned, e.getValue());
72       destMap.put(copyKey, copyValue);
73     }
74
75     destManaged.__tc_managed(new TCObjectClone(sourceManageable.__tc_managed(), txManager));
76     return cloned;
77   }
78
79   public void hydrate(ClientObjectManager objectManager, TCObject tcObject, DNA dna, Object JavaDoc po) throws IOException JavaDoc,
80       ClassNotFoundException JavaDoc {
81     DNACursor cursor = dna.getCursor();
82
83     while (cursor.next(encoding)) {
84       LogicalAction action = cursor.getLogicalAction();
85       int method = action.getMethod();
86       Object JavaDoc[] params = action.getParameters();
87       apply(objectManager, po, method, params);
88     }
89   }
90
91   protected void apply(ClientObjectManager objectManager, Object JavaDoc po, int method, Object JavaDoc[] params) throws ClassNotFoundException JavaDoc {
92     Map m = (Map) po;
93     switch (method) {
94       case SerializationUtil.PUT:
95         Object JavaDoc k = getKey(params);
96         Object JavaDoc v = getValue(params);
97         Object JavaDoc pkey = getObjectForKey(objectManager, k);
98
99         Object JavaDoc value = getObjectForValue(objectManager, v);
100
101         if (m instanceof TCMap) {
102           ((TCMap)m).__tc_applicator_put(pkey, value);
103         } else {
104           m.put(pkey, value);
105         }
106
107         break;
108       case SerializationUtil.REMOVE:
109         Object JavaDoc rkey = params[0] instanceof ObjectID ? objectManager.lookupObject((ObjectID) params[0]) : params[0];
110         if (m instanceof TCMap) {
111           ((TCMap)m).__tc_applicator_remove(rkey);
112         } else {
113           m.remove(rkey);
114         }
115
116         break;
117       case SerializationUtil.CLEAR:
118         m.clear();
119         break;
120       default:
121         throw new AssertionError JavaDoc("invalid action:" + method);
122     }
123   }
124
125   // This can be overridden by subclass if you want different behavior.
126
protected Object JavaDoc getObjectForValue(ClientObjectManager objectManager, Object JavaDoc v) throws ClassNotFoundException JavaDoc {
127     return (v instanceof ObjectID ? objectManager.lookupObject((ObjectID) v) : v);
128   }
129
130   // This can be overridden by subclass if you want different behavior.
131
protected Object JavaDoc getObjectForKey(ClientObjectManager objectManager, Object JavaDoc k) throws ClassNotFoundException JavaDoc {
132     return (k instanceof ObjectID ? objectManager.lookupObject((ObjectID) k) : k);
133   }
134
135   private Object JavaDoc getValue(Object JavaDoc[] params) {
136     // Hack hack big hack for trove maps which replace the key on set as opposed to HashMaps which do not.
137
return params.length == 3 ? params[2] : params[1];
138   }
139
140   private Object JavaDoc getKey(Object JavaDoc[] params) {
141     return params.length == 3 ? params[1] : params[0];
142   }
143
144   public void dehydrate(ClientObjectManager objectManager, TCObject tcObject, DNAWriter writer, Object JavaDoc pojo) {
145
146     Map map = (Map) pojo;
147     for (Iterator JavaDoc i = map.entrySet().iterator(); i.hasNext();) {
148       Entry entry = (Entry) i.next();
149       Object JavaDoc key = entry.getKey();
150       Object JavaDoc value = entry.getValue();
151
152       if (!objectManager.isPortableInstance(key)) {
153         continue;
154       }
155       if (!objectManager.isPortableInstance(value)) {
156         continue;
157       }
158
159       final Object JavaDoc addKey = getDehydratableObject(key, objectManager);
160       final Object JavaDoc addValue = getDehydratableObject(value, objectManager);
161
162       if (addKey == null || addValue == null) {
163         continue;
164       }
165
166       writer.addLogicalAction(SerializationUtil.PUT, new Object JavaDoc[] { addKey, addValue });
167     }
168   }
169
170   public Object JavaDoc getNewInstance(ClientObjectManager objectManager, DNA dna) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
171     if (false) { throw new IOException JavaDoc(); } // silence compiler warning
172
if (false) { throw new ClassNotFoundException JavaDoc(); } // silence compiler warning
173
throw new UnsupportedOperationException JavaDoc();
174   }
175 }
176
Popular Tags