KickJava   Java API By Example, From Geeks To Geeks.

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


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

31 public class DetachSCOHashSet extends HashSet JavaDoc implements Serializable JavaDoc, VersantSimpleSCO {
32
33     private PersistenceCapable owner;
34     private VersantStateManager stateManager;
35     private int fieldNo;
36
37     public DetachSCOHashSet(PersistenceCapable owner, VersantStateManager stateManager,
38                             VersantFieldMetaData fmd, Object JavaDoc[] originalData) {
39         this.owner = owner;
40         this.stateManager = stateManager;
41         this.fieldNo = fmd.getManagedFieldNo();
42         int n = originalData == null ? 0 : originalData.length;
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 boolean add(Object JavaDoc o) {
55         if (o == null) throw createNPE();
56         boolean result = super.add(o);
57         if (result) makeDirty();
58         return result;
59     }
60
61     public boolean remove(Object JavaDoc o) {
62         boolean result = super.remove(o);
63         if (result) {
64             makeDirty();
65         }
66         return result;
67     }
68
69     public void clear() {
70         super.clear();
71         makeDirty();
72     }
73
74     public boolean retainAll(Collection JavaDoc c) {
75         if (super.retainAll(c)) {
76             makeDirty();
77             return true;
78         }
79         return false;
80     }
81
82     public boolean removeAll(Collection JavaDoc c) {
83         if (super.removeAll(c)) {
84             makeDirty();
85             return true;
86         }
87         return false;
88     }
89
90     public boolean addAll(Collection JavaDoc c) {
91         if (super.addAll(c)) {
92             makeDirty();
93             return true;
94         }
95         return false;
96
97     }
98
99     public Iterator iterator() {
100         return new SCOIterator(super.iterator(), stateManager, owner, fieldNo);
101     }
102
103     private void makeDirty() {
104         if (stateManager != null && owner != null) {
105             stateManager.makeDirty(owner, fieldNo);
106         }
107     }
108
109     public void makeTransient() {
110         owner = null;
111         stateManager = null;
112     }
113 }
114
Popular Tags