KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.versant.core.jdo.sco.SCOListIterator;
19 import com.versant.core.jdo.VersantStateManager;
20
21 import javax.jdo.spi.PersistenceCapable;
22 import java.io.Serializable JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.ListIterator JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import com.versant.core.common.BindingSupportImpl;
29
30 /**
31  * A Detached SCO implementation of a Vector.
32  */

33 public final class DetachSCOVector extends Vector JavaDoc implements Serializable JavaDoc, VersantSimpleSCO {
34
35     private PersistenceCapable owner;
36     private int fierldNo;
37     private VersantStateManager stateManager;
38
39     public DetachSCOVector(PersistenceCapable owner, VersantStateManager stateManager,
40                            VersantFieldMetaData fmd, Object JavaDoc[] originalData) {
41         this.owner = owner;
42         this.fierldNo = fmd.getManagedFieldNo();
43         this.stateManager = stateManager;
44         int n = originalData == null ? 0 : originalData.length;
45         if (n > 0) ensureCapacity(n);
46         if (!owner.jdoIsNew()) {
47             for (int i = 0; i < n; i++) {
48                 Object JavaDoc o = originalData[i];
49                 if (o == null) break;
50                 super.add(o);
51             }
52         } else {
53             for (int i = 0; i < n; i++) {
54                 Object JavaDoc o = originalData[i];
55                 if (o == null) throw createNPE();
56                 super.add(o);
57             }
58         }
59     }
60
61     private RuntimeException JavaDoc createNPE() {
62         return BindingSupportImpl.getInstance().nullElement("Null element not allowed.");
63     }
64
65     public synchronized void setSize(int newSize) {
66         if (newSize > elementCount) {
67             throw BindingSupportImpl.getInstance().nullElement("setSize called with " + newSize + " > " + elementCount + " and " +
68                     "null elements are not allowed.");
69         }
70         super.setSize(newSize);
71         makeDirty();
72     }
73
74     public synchronized void setElementAt(Object JavaDoc obj, int index) {
75         if (obj == null) throw createNPE();
76         super.setElementAt(obj, index);
77         makeDirty();
78     }
79
80     public synchronized void removeElementAt(int index) {
81         super.removeElementAt(index);
82         makeDirty();
83     }
84
85     public synchronized void insertElementAt(Object JavaDoc obj, int index) {
86         if (obj == null) throw createNPE();
87         makeDirty();
88         super.insertElementAt(obj, index);
89     }
90
91     public synchronized void addElement(Object JavaDoc obj) {
92         if (obj == null) throw createNPE();
93         super.addElement(obj);
94         makeDirty();
95     }
96
97     public synchronized void removeAllElements() {
98         modCount++;
99         // Let gc do its work
100
for (int i = 0; i < elementCount; i++) {
101             elementData[i] = null;
102         }
103         elementCount = 0;
104         makeDirty();
105     }
106
107     public synchronized Object JavaDoc set(int index, Object JavaDoc element) {
108         if (element == null) throw createNPE();
109         Object JavaDoc obj = super.set(index, element);
110         makeDirty();
111         return obj;
112     }
113
114     public synchronized boolean add(Object JavaDoc o) {
115         if (o == null) throw createNPE();
116         super.add(o);
117         makeDirty();
118         return true;
119     }
120
121     public synchronized Object JavaDoc remove(int index) {
122         Object JavaDoc obj = super.remove(index);
123         makeDirty();
124         return obj;
125     }
126
127     public synchronized boolean addAll(Collection JavaDoc c) {
128         if (super.addAll(c)) {
129             makeDirty();
130             return true;
131         }
132         return false;
133     }
134
135     public synchronized boolean removeAll(Collection JavaDoc c) {
136         boolean modified = false;
137         Iterator e = super.iterator();
138         Object JavaDoc o = null;
139         while (e.hasNext()) {
140             o = e.next();
141             if (c.contains(o)) {
142                 e.remove();
143                 modified = true;
144             }
145         }
146         if (modified) makeDirty();
147         return modified;
148     }
149
150     public synchronized boolean retainAll(Collection JavaDoc c) {
151         boolean modified = false;
152         Iterator e = super.iterator();
153         Object JavaDoc o = null;
154         while (e.hasNext()) {
155             o = e.next();
156             if (!c.contains(o)) {
157                 e.remove();
158                 modified = true;
159             }
160         }
161         if (modified) {
162             makeDirty();
163         }
164         return modified;
165     }
166
167     public synchronized boolean addAll(int index, Collection JavaDoc c) {
168         if (super.addAll(index, c)) {
169             makeDirty();
170             return true;
171         }
172         return false;
173     }
174
175     protected void removeRange(int fromIndex, int toIndex) {
176         super.removeRange(fromIndex, toIndex);
177         makeDirty();
178     }
179
180     public ListIterator listIterator() {
181         return new SCOListIterator(super.listIterator(), stateManager, owner, fierldNo);
182     }
183
184     public ListIterator listIterator(int index) {
185         return new SCOListIterator(super.listIterator(index), stateManager, owner, fierldNo);
186     }
187
188     public Iterator iterator() {
189         return new SCOIterator(super.iterator(), stateManager, owner, fierldNo);
190     }
191
192     private void makeDirty() {
193         if (stateManager != null && owner != null) {
194             stateManager.makeDirty(owner, fierldNo);
195         }
196     }
197
198     public void makeTransient() {
199         owner = null;
200         stateManager = null;
201     }
202 }
203
Popular Tags