KickJava   Java API By Example, From Geeks To Geeks.

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


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.SerializationUtil;
8 import com.tc.object.TCObject;
9 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.DNAWriter;
14 import com.tc.object.dna.api.LogicalAction;
15 import com.tc.object.dna.impl.DNAEncoding;
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 JavaDoc;
21 import java.sql.Timestamp JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.Map JavaDoc;
25
26 /**
27  * ChangeApplicator for HashSets.
28  */

29 public class DateApplicator extends BaseApplicator {
30
31   public DateApplicator(DNAEncoding encoding) {
32     super(encoding);
33   }
34
35   public void hydrate(ClientObjectManager objectManager, TCObject tcObject, DNA dna, Object JavaDoc pojo) throws IOException JavaDoc,
36       ClassNotFoundException JavaDoc {
37     Date JavaDoc date = (Date JavaDoc) pojo;
38     DNACursor cursor = dna.getCursor();
39
40     while (cursor.next(encoding)) {
41       LogicalAction action = cursor.getLogicalAction();
42       int method = action.getMethod();
43       Object JavaDoc[] params = action.getParameters();
44       switch (method) {
45         case SerializationUtil.SET_TIME:
46           Assert.assertNotNull(params[0]);
47           date.setTime(((Long JavaDoc) params[0]).longValue());
48           break;
49         case SerializationUtil.SET_NANOS:
50           if (date instanceof Timestamp JavaDoc) {
51             Assert.assertNotNull(params[0]);
52             ((Timestamp JavaDoc) date).setNanos(((Integer JavaDoc) params[0]).intValue());
53           }
54           break;
55         default:
56           throw new AssertionError JavaDoc("invalid action:" + method);
57       }
58     }
59   }
60
61   public void dehydrate(ClientObjectManager objectManager, TCObject tcObject, DNAWriter writer, Object JavaDoc pojo) {
62     Date JavaDoc date = (Date JavaDoc) pojo;
63     writer.addLogicalAction(SerializationUtil.SET_TIME, new Object JavaDoc[] { new Long JavaDoc(date.getTime()) });
64   }
65
66   public TraversedReferences getPortableObjects(Object JavaDoc pojo, TraversedReferences addTo) {
67     addTo.addAnonymousReference(pojo);
68     return addTo;
69   }
70
71   public Object JavaDoc getNewInstance(ClientObjectManager objectManager, DNA dna) {
72     throw new UnsupportedOperationException JavaDoc();
73   }
74
75   public Map JavaDoc connectedCopy(Object JavaDoc source, Object JavaDoc dest, Map JavaDoc visited, ClientObjectManager objectManager,
76                            OptimisticTransactionManager txManager) {
77     Date JavaDoc sourceDate = (Date JavaDoc) source;
78     Date JavaDoc destDate = (Date JavaDoc) dest;
79     destDate.setTime(sourceDate.getTime());
80     if (source instanceof Timestamp JavaDoc) {
81       ((Timestamp JavaDoc) destDate).setNanos(((Timestamp JavaDoc) sourceDate).getNanos());
82     }
83
84     ((Manageable) destDate).__tc_managed(new TCObjectClone(((Manageable) sourceDate).__tc_managed(), txManager));
85     return Collections.EMPTY_MAP;
86   }
87 }
88
Popular Tags