KickJava   Java API By Example, From Geeks To Geeks.

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


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

30 public final class DetachSCOLinkedList extends LinkedList JavaDoc implements Serializable JavaDoc, VersantSimpleSCO {
31
32     private PersistenceCapable owner;
33     private int fieldNo;
34     private VersantStateManager stateManager;
35
36     public DetachSCOLinkedList(PersistenceCapable owner, VersantStateManager stateManager,
37                                VersantFieldMetaData fmd, Object JavaDoc[] originalData) {
38         this.owner = owner;
39         this.fieldNo = fmd.getManagedFieldNo();
40         this.stateManager = stateManager;
41         int n = originalData == null ? 0 : originalData.length;
42         for (int i = 0; i < n; i++) {
43             Object JavaDoc o = originalData[i];
44             if (o == null) throw createNPE();
45             super.add(o);
46         }
47     }
48
49     public Object JavaDoc removeFirst() {
50         Object JavaDoc result = super.removeFirst();
51         makeDirty();
52         return result;
53     }
54
55     public Object JavaDoc removeLast() {
56         Object JavaDoc result = super.removeLast();
57         makeDirty();
58         return result;
59     }
60
61     private RuntimeException JavaDoc createNPE() {
62         return BindingSupportImpl.getInstance().nullElement("Null element not allowed.");
63     }
64
65     public void addFirst(Object JavaDoc o) {
66         if (o == null) throw createNPE();
67         super.addFirst(o);
68         makeDirty();
69     }
70
71     public void addLast(Object JavaDoc o) {
72         if (o == null) throw createNPE();
73         super.addLast(o);
74         makeDirty();
75     }
76
77     public boolean addAll(int index, Collection JavaDoc c) {
78         if (super.addAll(index, c)) {
79             makeDirty();
80             return true;
81         }
82         return false;
83     }
84
85     protected void removeRange(int fromIndex, int toIndex) {
86         super.removeRange(fromIndex, toIndex);
87         makeDirty();
88     }
89
90     public boolean removeAll(Collection JavaDoc c) {
91         boolean modified = false;
92 // get unwrapped iterator from super so we can do remove
93
Iterator JavaDoc e = super.listIterator(0);
94         while (e.hasNext()) {
95             Object JavaDoc o = e.next();
96             if (c.contains(o)) {
97                 e.remove();
98                 modified = true;
99             }
100         }
101         if (modified) makeDirty();
102         return modified;
103
104     }
105
106     public boolean retainAll(Collection JavaDoc c) {
107         boolean modified = false;
108         // get an unwrapped Iterator so we can call remove
109
Iterator JavaDoc e = super.listIterator(0);
110         while (e.hasNext()) {
111             Object JavaDoc o = e.next();
112             if (!c.contains(o)) {
113                 e.remove();
114                 modified = true;
115             }
116         }
117         if (modified) makeDirty();
118         return modified;
119     }
120
121     public Object JavaDoc set(int index, Object JavaDoc element) {
122         if (element == null) throw createNPE();
123         Object JavaDoc result = super.set(index, element);
124         makeDirty();
125         return result;
126     }
127
128     public ListIterator JavaDoc listIterator(int index) {
129         return new ListIteratorImp(super.listIterator(index));
130     }
131
132     /**
133      * The set call in our superclass ListIterator is the only modification
134      * operation that does not delegate to the list. This is disallowed for
135      * managed relationships as there is no way for us to get at the current
136      * element.
137      */

138     private class ListIteratorImp implements ListIterator JavaDoc {
139
140         private ListIterator JavaDoc i;
141
142         public void set(Object JavaDoc o) {
143             i.set(o);
144             DetachSCOLinkedList.this.makeDirty();
145         }
146
147         public void remove() {
148             i.remove();
149             DetachSCOLinkedList.this.makeDirty();
150         }
151
152         public void add(Object JavaDoc o) {
153             i.add(o);
154             DetachSCOLinkedList.this.makeDirty();
155         }
156
157         public ListIteratorImp(ListIterator JavaDoc i) {
158             this.i = i;
159         }
160
161         public boolean hasNext() {
162             return i.hasNext();
163         }
164
165         public Object JavaDoc next() {
166             return i.next();
167         }
168
169         public boolean hasPrevious() {
170             return i.hasPrevious();
171         }
172
173         public Object JavaDoc previous() {
174             return i.previous();
175         }
176
177         public int nextIndex() {
178             return i.nextIndex();
179         }
180
181         public int previousIndex() {
182             return i.previousIndex();
183         }
184     }
185
186     public boolean add(Object JavaDoc o) {
187         if (o == null) throw createNPE();
188         super.add(o);
189         makeDirty();
190         return true;
191     }
192
193     public void add(int index, Object JavaDoc element) {
194         if (element == null) throw createNPE();
195         super.add(index, element);
196         makeDirty();
197     }
198
199     public Object JavaDoc remove(int index) {
200         Object JavaDoc result = super.remove(index);
201         if (result != null) {
202             makeDirty();
203         }
204         return result;
205     }
206
207     public boolean remove(Object JavaDoc o) {
208         if (super.remove(o)) {
209             makeDirty();
210             return true;
211         }
212         return false;
213     }
214
215     public void clear() {
216         super.clear();
217         makeDirty();
218     }
219
220     private void makeDirty() {
221         if (stateManager != null && owner != null) {
222             stateManager.makeDirty(owner, fieldNo);
223         }
224     }
225
226     public void makeTransient() {
227         owner = null;
228         stateManager = null;
229     }
230 }
231
Popular Tags