KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > sco > detached > DetachSCOHashtable


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo.sco.detached;
13
14 import com.versant.core.jdo.VersantStateManager;
15 import com.versant.core.common.VersantFieldMetaData;
16 import com.versant.core.jdo.sco.VersantSimpleSCO;
17 import com.versant.core.jdo.sco.MapData;
18 import com.versant.core.jdo.VersantStateManager;
19
20 import javax.jdo.spi.PersistenceCapable;
21 import java.io.Serializable JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Map JavaDoc;
24
25 public class DetachSCOHashtable extends Hashtable JavaDoc implements Serializable JavaDoc, 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, VersantStateManager stateManager,
38                               VersantFieldMetaData fmd, Map JavaDoc beforeMap) {
39         this(owner, fmd);
40         putAll(beforeMap);
41         this.stateManager = stateManager;
42     }
43
44     public DetachSCOHashtable(PersistenceCapable owner, VersantStateManager stateManager,
45                               VersantFieldMetaData fmd, MapData mapData) {
46         this(owner, fmd);
47         int n = mapData.entryCount;
48         Object JavaDoc[] keys = mapData.keys;
49         Object JavaDoc[] values = mapData.values;
50         for (int i = 0; i < n; i++) {
51             put(keys[i], values[i]);
52         }
53         this.stateManager = stateManager;
54     }
55
56     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
57         makeDirty();
58         return super.put(key, value);
59     }
60
61     public Object JavaDoc remove(Object JavaDoc key) {
62         return checkModified(super.remove(key));
63     }
64
65     public void putAll(Map JavaDoc t) {
66         makeDirty();
67         super.putAll(t);
68     }
69
70     public void clear() {
71         final int size = size();
72         super.clear();
73         if (size != 0) makeDirty();
74     }
75
76     private void makeDirty() {
77         if (stateManager != null && owner != null) {
78             stateManager.makeDirty(owner, fieldNo);
79         }
80     }
81
82     private Object JavaDoc checkModified(Object JavaDoc obj) {
83         if (obj != null) {
84             makeDirty();
85         }
86         return obj;
87     }
88
89     public void makeTransient() {
90         owner = null;
91         stateManager = null;
92     }
93 }
94
Popular Tags