KickJava   Java API By Example, From Geeks To Geeks.

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


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

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 JavaDoc;
24 import java.util.IdentityHashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Vector JavaDoc;
29
30 /**
31  * TODO Dec 21, 2004: I, steve, am too lazy to write a single sentence describing what this class is for.
32  */

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 JavaDoc pojo, TraversedReferences addTo) {
41     for (Iterator JavaDoc i = ((List JavaDoc) pojo).iterator(); i.hasNext();) {
42       Object JavaDoc 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 JavaDoc po) throws IOException JavaDoc,
51       ClassNotFoundException JavaDoc {
52     List JavaDoc list = (List JavaDoc) po;
53     DNACursor cursor = dna.getCursor();
54
55     while (cursor.next(encoding)) {
56       LogicalAction action = cursor.getLogicalAction();
57       int method = action.getMethod();
58       Object JavaDoc[] params = action.getParameters();
59
60       for (int i = 0, n = params.length; i < n; i++) {
61         Object JavaDoc 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 JavaDoc ioobe) {
70         logger.error("Error applying update to " + po, ioobe);
71       }
72     }
73   }
74
75   private void apply(List JavaDoc list, int method, Object JavaDoc[] 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 JavaDoc) 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 JavaDoc) 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 JavaDoc) 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 JavaDoc) 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 JavaDoc) 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 JavaDoc) params[0]).intValue();
150         ((Vector JavaDoc) list).setSize(setSize);
151         break;
152       case SerializationUtil.TRIM_TO_SIZE:
153         // do nothing for now
154
break;
155       default:
156         throw new AssertionError JavaDoc("invalid action:" + method);
157     }
158   }
159
160   public void dehydrate(ClientObjectManager objectManager, TCObject tcObject, DNAWriter writer, Object JavaDoc pojo) {
161     List JavaDoc list = (List JavaDoc) pojo;
162
163     for (int i = 0; i < list.size(); i++) {
164       Object JavaDoc value = list.get(i);
165       if (!objectManager.isPortableInstance(value)) {
166         continue;
167       }
168
169       final Object JavaDoc addValue = getDehydratableObject(value, objectManager);
170       if (addValue == null) {
171         continue;
172       }
173       writer.addLogicalAction(SerializationUtil.ADD, new Object JavaDoc[] { addValue });
174     }
175   }
176
177   public Object JavaDoc getNewInstance(ClientObjectManager objectManager, DNA dna) {
178     throw new UnsupportedOperationException JavaDoc();
179   }
180
181   public Map JavaDoc connectedCopy(Object JavaDoc source, Object JavaDoc dest, Map JavaDoc visited, ClientObjectManager objectManager,
182                            OptimisticTransactionManager txManager) {
183     Map JavaDoc cloned = new IdentityHashMap JavaDoc();
184
185     Manageable sourceManageable = (Manageable) source;
186     Manageable destManaged = (Manageable) dest;
187
188     List JavaDoc sourceList = (List JavaDoc) source;
189     List JavaDoc destList = (List JavaDoc) dest;
190
191     for (Iterator JavaDoc i = sourceList.iterator(); i.hasNext();) {
192       Object JavaDoc v = i.next();
193       Object JavaDoc 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