KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > tx > optimistic > OptimisticTransaction


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.tx.optimistic;
5
6 import com.tc.object.TCObject;
7 import com.tc.object.bytecode.Manageable;
8 import com.tc.object.bytecode.ManagerUtil;
9 import com.tc.object.change.TCChangeBuffer;
10 import com.tc.object.change.TCChangeBufferImpl;
11
12 import java.util.HashMap JavaDoc;
13 import java.util.IdentityHashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 public class OptimisticTransaction {
17   private final Map JavaDoc objectChanges = new HashMap JavaDoc();
18   private final Map JavaDoc clones = new IdentityHashMap JavaDoc();
19
20   public boolean hasClone(Object JavaDoc obj) {
21     return clones.containsValue(obj);
22   }
23
24   public TCObject getTCObjectFor(Object JavaDoc obj) {
25     if (obj.getClass().isArray()) {
26       return ManagerUtil.getObject(obj);
27     } else {
28       return ((Manageable) obj).__tc_managed();
29     }
30   }
31
32   public void addAll(Map JavaDoc newClones) {
33     clones.putAll(newClones);
34   }
35
36   public boolean hasChanges() {
37     return !objectChanges.isEmpty();
38   }
39
40   public Map JavaDoc getChangeBuffers() {
41     return this.objectChanges;
42   }
43
44   public void objectFieldChanged(TCObject source, String JavaDoc classname, String JavaDoc fieldname, Object JavaDoc newValue, int index) {
45     getOrCreateChangeBuffer(source).fieldChanged(classname, fieldname, newValue, index);
46   }
47
48   public void logicalInvoke(TCObject source, int method, Object JavaDoc[] parameters) {
49     getOrCreateChangeBuffer(source).logicalInvoke(method, parameters);
50   }
51
52   private TCChangeBuffer getOrCreateChangeBuffer(TCObject object) {
53
54     TCChangeBuffer cb = (TCChangeBuffer) objectChanges.get(object);
55     if (cb == null) {
56       cb = new TCChangeBufferImpl(object);
57       objectChanges.put(object, cb);
58     }
59
60     return cb;
61   }
62 }
63
Popular Tags