01: /*
02: * Copyright (c) 1998 - 2005 Versant Corporation
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * Versant Corporation - initial API and implementation
10: */
11: package com.versant.core.jdo.sco.detached;
12:
13: import com.versant.core.jdo.VersantStateManager;
14: import com.versant.core.common.VersantFieldMetaData;
15: import com.versant.core.jdo.sco.VersantSimpleSCO;
16: import com.versant.core.jdo.sco.MapData;
17: import com.versant.core.jdo.VersantStateManager;
18:
19: import javax.jdo.spi.PersistenceCapable;
20: import java.io.Serializable;
21: import java.util.Hashtable;
22: import java.util.Map;
23:
24: public class DetachSCOHashtable extends Hashtable implements
25: Serializable, VersantSimpleSCO {
26:
27: private PersistenceCapable owner;
28: private VersantStateManager stateManager;
29: private int fieldNo;
30:
31: private DetachSCOHashtable(PersistenceCapable owner,
32: VersantFieldMetaData fmd) {
33: this .owner = owner;
34: this .fieldNo = fmd.getManagedFieldNo();
35: }
36:
37: public DetachSCOHashtable(PersistenceCapable owner,
38: VersantStateManager stateManager, VersantFieldMetaData fmd,
39: Map beforeMap) {
40: this (owner, fmd);
41: putAll(beforeMap);
42: this .stateManager = stateManager;
43: }
44:
45: public DetachSCOHashtable(PersistenceCapable owner,
46: VersantStateManager stateManager, VersantFieldMetaData fmd,
47: MapData mapData) {
48: this (owner, fmd);
49: int n = mapData.entryCount;
50: Object[] keys = mapData.keys;
51: Object[] values = mapData.values;
52: for (int i = 0; i < n; i++) {
53: put(keys[i], values[i]);
54: }
55: this .stateManager = stateManager;
56: }
57:
58: public Object put(Object key, Object value) {
59: makeDirty();
60: return super .put(key, value);
61: }
62:
63: public Object remove(Object key) {
64: return checkModified(super .remove(key));
65: }
66:
67: public void putAll(Map t) {
68: makeDirty();
69: super .putAll(t);
70: }
71:
72: public void clear() {
73: final int size = size();
74: super .clear();
75: if (size != 0)
76: makeDirty();
77: }
78:
79: private void makeDirty() {
80: if (stateManager != null && owner != null) {
81: stateManager.makeDirty(owner, fieldNo);
82: }
83: }
84:
85: private Object checkModified(Object obj) {
86: if (obj != null) {
87: makeDirty();
88: }
89: return obj;
90: }
91:
92: public void makeTransient() {
93: owner = null;
94: stateManager = null;
95: }
96: }
|