KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import com.versant.core.common.BindingSupportImpl;
26
27 /**
28  * Detached SCO for ArrayList.
29  */

30 public final class DetachSCOArrayList extends ArrayList JavaDoc implements Serializable JavaDoc, VersantSimpleSCO {
31
32     private int fieldNo;
33     private VersantStateManager stateManager;
34     private PersistenceCapable owner;
35
36     public DetachSCOArrayList(PersistenceCapable owner, VersantStateManager stateManager,
37                               VersantFieldMetaData fmd, Object JavaDoc[] originalData) {
38         this.fieldNo = fmd.getManagedFieldNo();
39         this.stateManager = stateManager;
40         this.owner = owner;
41         int n = originalData == null ? 0 : originalData.length;
42         if (n > 0) ensureCapacity(n);
43         for (int i = 0; i < n; i++) {
44             Object JavaDoc o = originalData[i];
45             if (o == null) throw createNPE();
46             super.add(o);
47         }
48     }
49
50     private RuntimeException JavaDoc createNPE() {
51         return BindingSupportImpl.getInstance().nullElement("Null element not allowed.");
52     }
53
54     public Object JavaDoc set(int index, Object JavaDoc element) {
55         if (element == null) throw createNPE();
56         Object JavaDoc result = super.set(index, element);
57         makeDirty();
58         return result;
59     }
60
61     public boolean add(Object JavaDoc o) {
62         if (o == null) throw createNPE();
63         super.add(o);
64         makeDirty();
65         return true;
66     }
67
68     public void add(int index, Object JavaDoc element) {
69         if (element == null) throw createNPE();
70         super.add(index, element);
71         makeDirty();
72     }
73
74     public Object JavaDoc remove(int index) {
75         Object JavaDoc result = super.remove(index);
76         if (result == null) return null; // ok as we do not allow nulls in list
77
makeDirty();
78         return result;
79     }
80
81     public boolean remove(Object JavaDoc o) {
82         // Finding the index and removing it is faster as ArrayList leaves this
83
// to AbstractCollection which creates an Iterator to search for the
84
// element and calls Iterator.remove when it is found. This in turn
85
// calls remove(index) on us. Braindead but true!
86
int i = indexOf(o);
87         if (i < 0) return false;
88         remove(i);
89         return true;
90     }
91
92     public boolean addAll(Collection JavaDoc c) {
93         if (super.addAll(c)) {
94             makeDirty();
95             return true;
96         }
97         return false;
98     }
99
100     public void clear() {
101         super.clear();
102         makeDirty();
103     }
104
105     public boolean addAll(int index, Collection JavaDoc c) {
106         boolean result;
107         result = super.addAll(index, c);
108         if (result) makeDirty();
109         return result;
110     }
111
112     protected void removeRange(int fromIndex, int toIndex) {
113         super.removeRange(fromIndex, toIndex);
114         makeDirty();
115     }
116
117     public boolean removeAll(Collection JavaDoc c) {
118         if (super.removeAll(c)) {
119             makeDirty();
120             return true;
121         }
122         return false;
123     }
124
125     public boolean retainAll(Collection JavaDoc c) {
126         boolean modified = false;
127         Iterator e = super.iterator();
128         while (e.hasNext()) {
129             Object JavaDoc o = e.next();
130             if (!c.contains(o)) {
131                 e.remove();
132                 modified = true;
133             }
134         }
135         if (modified) makeDirty();
136         return modified;
137     }
138
139     public void makeDirty() {
140         if (stateManager != null && owner != null) {
141             stateManager.makeDirty(owner, fieldNo);
142         }
143     }
144
145     public Iterator iterator() {
146         return new SCOIterator(super.iterator(), stateManager, owner, fieldNo);
147     }
148
149     public void makeTransient() {
150         owner = null;
151         stateManager = null;
152     }
153 }
154
Popular Tags