1 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 ; 22 import java.util.Collection ; 23 import java.util.IdentityHashMap ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.Map.Entry; 27 28 31 public class HashMapApplicator extends BaseApplicator { 32 33 public HashMapApplicator(DNAEncoding encoding) { 34 super(encoding); 35 } 36 37 public TraversedReferences getPortableObjects(Object 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 objects, TraversedReferences addTo) { 45 for (Iterator i = objects.iterator(); i.hasNext();) { 46 Object o = i.next(); 47 if (o != null && isPortableReference(o.getClass())) { 48 addTo.addAnonymousReference(o); 49 } 50 } 51 } 52 53 57 public Map connectedCopy(Object source, Object dest, Map visited, ClientObjectManager objectManager, 58 OptimisticTransactionManager txManager) { 59 Map cloned = new IdentityHashMap (); 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 i = sourceMap.entrySet().iterator(); i.hasNext();) { 68 Entry e = (Entry) i.next(); 69 70 Object copyKey = createCopyIfNecessary(objectManager, visited, cloned, e.getKey()); 71 Object 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 po) throws IOException , 80 ClassNotFoundException { 81 DNACursor cursor = dna.getCursor(); 82 83 while (cursor.next(encoding)) { 84 LogicalAction action = cursor.getLogicalAction(); 85 int method = action.getMethod(); 86 Object [] params = action.getParameters(); 87 apply(objectManager, po, method, params); 88 } 89 } 90 91 protected void apply(ClientObjectManager objectManager, Object po, int method, Object [] params) throws ClassNotFoundException { 92 Map m = (Map) po; 93 switch (method) { 94 case SerializationUtil.PUT: 95 Object k = getKey(params); 96 Object v = getValue(params); 97 Object pkey = getObjectForKey(objectManager, k); 98 99 Object 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 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 ("invalid action:" + method); 122 } 123 } 124 125 protected Object getObjectForValue(ClientObjectManager objectManager, Object v) throws ClassNotFoundException { 127 return (v instanceof ObjectID ? objectManager.lookupObject((ObjectID) v) : v); 128 } 129 130 protected Object getObjectForKey(ClientObjectManager objectManager, Object k) throws ClassNotFoundException { 132 return (k instanceof ObjectID ? objectManager.lookupObject((ObjectID) k) : k); 133 } 134 135 private Object getValue(Object [] params) { 136 return params.length == 3 ? params[2] : params[1]; 138 } 139 140 private Object getKey(Object [] params) { 141 return params.length == 3 ? params[1] : params[0]; 142 } 143 144 public void dehydrate(ClientObjectManager objectManager, TCObject tcObject, DNAWriter writer, Object pojo) { 145 146 Map map = (Map) pojo; 147 for (Iterator i = map.entrySet().iterator(); i.hasNext();) { 148 Entry entry = (Entry) i.next(); 149 Object key = entry.getKey(); 150 Object value = entry.getValue(); 151 152 if (!objectManager.isPortableInstance(key)) { 153 continue; 154 } 155 if (!objectManager.isPortableInstance(value)) { 156 continue; 157 } 158 159 final Object addKey = getDehydratableObject(key, objectManager); 160 final Object addValue = getDehydratableObject(value, objectManager); 161 162 if (addKey == null || addValue == null) { 163 continue; 164 } 165 166 writer.addLogicalAction(SerializationUtil.PUT, new Object [] { addKey, addValue }); 167 } 168 } 169 170 public Object getNewInstance(ClientObjectManager objectManager, DNA dna) throws IOException , ClassNotFoundException { 171 if (false) { throw new IOException (); } if (false) { throw new ClassNotFoundException (); } throw new UnsupportedOperationException (); 174 } 175 } 176 | Popular Tags |