1 5 package com.tc.object.applicator; 6 7 import com.tc.logging.TCLogger; 8 import com.tc.logging.TCLogging; 9 import com.tc.object.ClientObjectManager; 10 import com.tc.object.ObjectID; 11 import com.tc.object.SerializationUtil; 12 import com.tc.object.TCObject; 13 import com.tc.object.TraversedReferences; 14 import com.tc.object.bytecode.Manageable; 15 import com.tc.object.dna.api.DNA; 16 import com.tc.object.dna.api.DNACursor; 17 import com.tc.object.dna.api.DNAWriter; 18 import com.tc.object.dna.api.LogicalAction; 19 import com.tc.object.dna.impl.DNAEncoding; 20 import com.tc.object.tx.optimistic.OptimisticTransactionManager; 21 import com.tc.object.tx.optimistic.TCObjectClone; 22 23 import java.io.IOException ; 24 import java.util.IdentityHashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.Vector ; 29 30 33 public class ListApplicator extends BaseApplicator { 34 private static final TCLogger logger = TCLogging.getLogger(ListApplicator.class); 35 36 public ListApplicator(DNAEncoding encoding) { 37 super(encoding); 38 } 39 40 public TraversedReferences getPortableObjects(Object pojo, TraversedReferences addTo) { 41 for (Iterator i = ((List ) pojo).iterator(); i.hasNext();) { 42 Object o = i.next(); 43 if (o != null && isPortableReference(o.getClass())) { 44 addTo.addAnonymousReference(o); 45 } 46 } 47 return addTo; 48 } 49 50 public void hydrate(ClientObjectManager objectManager, TCObject tcObject, DNA dna, Object po) throws IOException , 51 ClassNotFoundException { 52 List list = (List ) po; 53 DNACursor cursor = dna.getCursor(); 54 55 while (cursor.next(encoding)) { 56 LogicalAction action = cursor.getLogicalAction(); 57 int method = action.getMethod(); 58 Object [] params = action.getParameters(); 59 60 for (int i = 0, n = params.length; i < n; i++) { 61 Object param = params[i]; 62 if (param instanceof ObjectID) { 63 params[i] = objectManager.lookupObject((ObjectID) param); 64 } 65 } 66 67 try { 68 apply(list, method, params); 69 } catch (IndexOutOfBoundsException ioobe) { 70 logger.error("Error applying update to " + po, ioobe); 71 } 72 } 73 } 74 75 private void apply(List list, int method, Object [] params) { 76 final int size = list.size(); 77 switch (method) { 78 case SerializationUtil.ADD: 79 case SerializationUtil.ADD_LAST: 80 list.add(params[0]); 81 break; 82 case SerializationUtil.INSERT_AT: 83 case SerializationUtil.ADD_AT: 84 int aaindex = ((Integer ) params[0]).intValue(); 85 if (aaindex > size) { 86 logger.error("Inserting at index " + size + " instead of requested index " + aaindex 87 + "because list is only of size " + size); 88 aaindex = size; 89 } 90 list.add(aaindex, params[1]); 91 break; 92 case SerializationUtil.ADD_FIRST: 93 list.add(0, params[0]); 94 break; 95 case SerializationUtil.SET_ELEMENT: 96 case SerializationUtil.SET: 97 int sindex = ((Integer ) params[0]).intValue(); 98 if (sindex >= size) { 99 logger.error("Cannot set element at index + " + sindex + " becuase object is only of size " + size); 100 return; 101 } 102 list.set(sindex, params[1]); 103 break; 104 case SerializationUtil.REMOVE: 105 list.remove(params[0]); 106 break; 107 case SerializationUtil.REMOVE_AT: 108 int raindex = ((Integer ) params[0]).intValue(); 109 if (raindex >= size) { 110 logger.error("Cannot remove element at index + " + raindex + " becuase object is only of size " + size); 111 return; 112 } 113 list.remove(raindex); 114 break; 115 case SerializationUtil.REMOVE_RANGE: 116 int fromIndex = ((Integer ) params[0]).intValue(); 117 if (fromIndex >= size) { 118 logger.error("Cannot remove element at index + " + fromIndex + " becuase object is only of size " + size); 119 return; 120 } 121 int toIndex = ((Integer ) params[1]).intValue(); 122 if (toIndex > size) { 123 logger.error("Cannot remove element at index + " + (toIndex - 1) + " becuase object is only of size " + size); 124 return; 125 } 126 int removeIndex = fromIndex; 127 while (fromIndex++ < toIndex) { 128 list.remove(removeIndex); 129 } 130 break; 131 case SerializationUtil.REMOVE_FIRST: 132 if (size > 0) { 133 list.remove(0); 134 } else { 135 logger.error("Cannot removeFirst() because Vector is empty"); 136 } 137 break; 138 case SerializationUtil.REMOVE_LAST: 139 if (size > 0) { 140 list.remove(list.size() - 1); 141 } else { 142 logger.error("Cannot removeLast() because Vector is empty"); 143 } 144 break; 145 case SerializationUtil.CLEAR: 146 list.clear(); 147 break; 148 case SerializationUtil.SET_SIZE: 149 int setSize = ((Integer ) params[0]).intValue(); 150 ((Vector ) list).setSize(setSize); 151 break; 152 case SerializationUtil.TRIM_TO_SIZE: 153 break; 155 default: 156 throw new AssertionError ("invalid action:" + method); 157 } 158 } 159 160 public void dehydrate(ClientObjectManager objectManager, TCObject tcObject, DNAWriter writer, Object pojo) { 161 List list = (List ) pojo; 162 163 for (int i = 0; i < list.size(); i++) { 164 Object value = list.get(i); 165 if (!objectManager.isPortableInstance(value)) { 166 continue; 167 } 168 169 final Object addValue = getDehydratableObject(value, objectManager); 170 if (addValue == null) { 171 continue; 172 } 173 writer.addLogicalAction(SerializationUtil.ADD, new Object [] { addValue }); 174 } 175 } 176 177 public Object getNewInstance(ClientObjectManager objectManager, DNA dna) { 178 throw new UnsupportedOperationException (); 179 } 180 181 public Map connectedCopy(Object source, Object dest, Map visited, ClientObjectManager objectManager, 182 OptimisticTransactionManager txManager) { 183 Map cloned = new IdentityHashMap (); 184 185 Manageable sourceManageable = (Manageable) source; 186 Manageable destManaged = (Manageable) dest; 187 188 List sourceList = (List ) source; 189 List destList = (List ) dest; 190 191 for (Iterator i = sourceList.iterator(); i.hasNext();) { 192 Object v = i.next(); 193 Object copyValue = null; 194 195 copyValue = createCopyIfNecessary(objectManager, visited, cloned, v); 196 destList.add(copyValue); 197 } 198 199 destManaged.__tc_managed(new TCObjectClone(sourceManageable.__tc_managed(), txManager)); 200 return cloned; 201 } 202 } 203 | Popular Tags |