01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.object.applicator;
05:
06: import com.tc.object.ClientObjectManager;
07: import com.tc.object.SerializationUtil;
08: import com.tc.object.TCObject;
09: import com.tc.object.TraversedReferences;
10: import com.tc.object.bytecode.Manageable;
11: import com.tc.object.dna.api.DNA;
12: import com.tc.object.dna.api.DNACursor;
13: import com.tc.object.dna.api.DNAEncoding;
14: import com.tc.object.dna.api.DNAWriter;
15: import com.tc.object.dna.api.LogicalAction;
16: import com.tc.object.tx.optimistic.OptimisticTransactionManager;
17: import com.tc.object.tx.optimistic.TCObjectClone;
18: import com.tc.util.Assert;
19:
20: import java.io.IOException;
21: import java.sql.Timestamp;
22: import java.util.Collections;
23: import java.util.Date;
24: import java.util.Map;
25:
26: /**
27: * ChangeApplicator for Dates.
28: */
29: public class DateApplicator extends BaseApplicator {
30:
31: public DateApplicator(DNAEncoding encoding) {
32: super (encoding);
33: }
34:
35: public void hydrate(ClientObjectManager objectManager,
36: TCObject tcObject, DNA dna, Object pojo)
37: throws IOException, ClassNotFoundException {
38: Date date = (Date) pojo;
39: DNACursor cursor = dna.getCursor();
40:
41: while (cursor.next(encoding)) {
42: LogicalAction action = cursor.getLogicalAction();
43: int method = action.getMethod();
44: Object[] params = action.getParameters();
45: switch (method) {
46: case SerializationUtil.SET_TIME:
47: Assert.assertNotNull(params[0]);
48: date.setTime(((Long) params[0]).longValue());
49: break;
50: case SerializationUtil.SET_NANOS:
51: if (date instanceof Timestamp) {
52: Assert.assertNotNull(params[0]);
53: ((Timestamp) date).setNanos(((Integer) params[0])
54: .intValue());
55: }
56: break;
57: default:
58: throw new AssertionError("invalid action:" + method);
59: }
60: }
61: }
62:
63: public void dehydrate(ClientObjectManager objectManager,
64: TCObject tcObject, DNAWriter writer, Object pojo) {
65: Date date = (Date) pojo;
66: writer.addLogicalAction(SerializationUtil.SET_TIME,
67: new Object[] { new Long(date.getTime()) });
68: }
69:
70: public TraversedReferences getPortableObjects(Object pojo,
71: TraversedReferences addTo) {
72: addTo.addAnonymousReference(pojo);
73: return addTo;
74: }
75:
76: public Object getNewInstance(ClientObjectManager objectManager,
77: DNA dna) {
78: throw new UnsupportedOperationException();
79: }
80:
81: public Map connectedCopy(Object source, Object dest, Map visited,
82: ClientObjectManager objectManager,
83: OptimisticTransactionManager txManager) {
84: Date sourceDate = (Date) source;
85: Date destDate = (Date) dest;
86: destDate.setTime(sourceDate.getTime());
87: if (source instanceof Timestamp) {
88: ((Timestamp) destDate).setNanos(((Timestamp) sourceDate)
89: .getNanos());
90: }
91:
92: ((Manageable) destDate).__tc_managed(new TCObjectClone(
93: ((Manageable) sourceDate).__tc_managed(), txManager));
94: return Collections.EMPTY_MAP;
95: }
96: }
|