KickJava   Java API By Example, From Geeks To Geeks.

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


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.SCOIterator;
18
19 import javax.jdo.spi.PersistenceCapable;
20 import java.io.Serializable JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.TreeSet JavaDoc;
24
25 import com.versant.core.common.BindingSupportImpl;
26
27 /**
28  * Detached TreeSet SCO.
29  */

30 public class DetachSCOTreeSet extends TreeSet JavaDoc implements Serializable JavaDoc, VersantSimpleSCO {
31
32     private PersistenceCapable owner;
33     private VersantStateManager stateManager;
34     private int fieldNo;
35
36     public DetachSCOTreeSet(PersistenceCapable owner, VersantStateManager stateManager,
37                             VersantFieldMetaData fmd, Object JavaDoc[] originalData) {
38         super(fmd.getComparator());
39         this.owner = owner;
40         this.stateManager = stateManager;
41         this.fieldNo = fmd.getManagedFieldNo();
42         int n = originalData == null ? 0 : originalData.length;
43         if (!owner.jdoIsNew()) {
44             for (int i = 0; i < n; i++) {
45                 Object JavaDoc o = originalData[i];
46                 if (o == null) break;
47                 super.add(o);
48             }
49         } else {
50             for (int i = 0; i < n; i++) {
51                 Object JavaDoc o = originalData[i];
52                 if (o == null) throw createNPE();
53                 super.add(o);
54             }
55         }
56     }
57
58     private RuntimeException JavaDoc createNPE() {
59         return BindingSupportImpl.getInstance().nullElement("Null element not allowed.");
60     }
61
62     public boolean add(Object JavaDoc o) {
63         if (o == null) throw createNPE();
64         boolean result = super.add(o);
65         if (result) makeDirty();
66         return result;
67     }
68
69     public boolean remove(Object JavaDoc o) {
70         boolean result = super.remove(o);
71         if (result) {
72             makeDirty();
73         }
74         return result;
75     }
76
77     public void clear() {
78         super.clear();
79         makeDirty();
80     }
81
82     public boolean retainAll(Collection JavaDoc c) {
83         if (super.retainAll(c)) {
84             makeDirty();
85             return true;
86         }
87         return false;
88     }
89
90     public boolean removeAll(Collection JavaDoc c) {
91         if (super.removeAll(c)) {
92             makeDirty();
93             return true;
94         }
95         return false;
96     }
97
98     public boolean addAll(Collection JavaDoc c) {
99         if (super.addAll(c)) {
100             makeDirty();
101             return true;
102         }
103         return false;
104
105     }
106
107     public Iterator iterator() {
108         return new SCOIterator(super.iterator(), stateManager, owner, fieldNo);
109     }
110
111     private void makeDirty() {
112         if (stateManager != null && owner != null) {
113             stateManager.makeDirty(owner, fieldNo);
114         }
115     }
116
117     public void makeTransient() {
118         owner = null;
119         stateManager = null;
120     }
121 }
122
Popular Tags