KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
23 import java.util.TreeMap JavaDoc;
24
25 /**
26  * Detached SCO for TreeMap.
27  */

28 public class DetachSCOTreeMap extends TreeMap JavaDoc implements Serializable JavaDoc, VersantSimpleSCO {
29
30     private PersistenceCapable owner;
31     private VersantStateManager stateManager;
32     private int fieldNo;
33
34     private DetachSCOTreeMap(PersistenceCapable owner,
35                              VersantFieldMetaData fmd) {
36         super(fmd.getComparator());
37         this.owner = owner;
38         this.fieldNo = fmd.getManagedFieldNo();
39     }
40
41     public DetachSCOTreeMap(PersistenceCapable owner, VersantStateManager stateManager,
42                             VersantFieldMetaData fmd, Map JavaDoc beforeMap) {
43         this(owner, fmd);
44         putAll(beforeMap);
45         this.stateManager = stateManager;
46     }
47
48     public DetachSCOTreeMap(PersistenceCapable owner, VersantStateManager stateManager,
49                             VersantFieldMetaData fmd, MapData mapData) {
50         this(owner, fmd);
51         int n = mapData.entryCount;
52         Object JavaDoc[] keys = mapData.keys;
53         Object JavaDoc[] values = mapData.values;
54         for (int i = 0; i < n; i++) {
55             put(keys[i], values[i]);
56         }
57         this.stateManager = stateManager;
58     }
59
60     public Class JavaDoc getJavaType() {
61         return TreeMap JavaDoc.class;
62     }
63
64     public Object JavaDoc remove(Object JavaDoc key) {
65         return checkModified(super.remove(key));
66     }
67
68     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
69         makeDirty();
70         return super.put(key, value);
71     }
72
73     public void putAll(Map JavaDoc t) {
74         makeDirty();
75         super.putAll(t);
76     }
77
78     public void clear() {
79         final int size = size();
80         super.clear();
81         if (size != 0) makeDirty();
82     }
83
84     public Object JavaDoc getOwner() {
85         return owner;
86     }
87
88     public void makeTransient() {
89         owner = null;
90         stateManager = null;
91     }
92
93     public void makeDirty() {
94         if (stateManager != null && owner != null) {
95             stateManager.makeDirty(owner, fieldNo);
96         }
97     }
98
99     private Object JavaDoc checkModified(Object JavaDoc obj) {
100         if (obj != null) {
101             makeDirty();
102         }
103         return obj;
104     }
105 }
106
Popular Tags