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