KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > ejb > cmp > EJBHashSet


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25
26 /*
27  * EJBHashSet.java
28  *
29  * Created on December 10, 2001
30  */

31
32 package com.sun.jdo.spi.persistence.support.ejb.cmp;
33
34 import java.util.*;
35
36 import javax.ejb.EJBLocalObject JavaDoc;
37
38 import com.sun.jdo.api.persistence.support.PersistenceManager;
39 import com.sun.jdo.api.persistence.support.Transaction;
40 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.JDOEJB20Helper;
41 import com.sun.jdo.spi.persistence.utility.logging.Logger;
42 import com.sun.jdo.spi.persistence.support.sqlstore.LogHelperSQLStore;
43 /*
44  * This is the implementation of the java.util.Set interface for the CMP
45  * fields of the Collection type. Represents many side of the relationships.
46  *
47  * @author Marina Vatkina
48  */

49 public class EJBHashSet extends HashSet {
50     // Reference to the PersistenceManager and Transaction that were active
51
// at the time of this Set creation.
52
private PersistenceManager pm = null;
53     private Transaction tx = null;
54
55     // HashSet of the persistence-capable instances associated with this Set.
56
private HashSet pcSet = null;
57
58     // Helper instance for conversion of the persistence-capable instances
59
// to the EJBLocalObject type and back.
60
private JDOEJB20Helper helper = null;
61
62     // Flag that indicates invalid state of this Set.
63
private boolean valid = false;
64     
65     //The logger
66
private static Logger logger = LogHelperSQLStore.getLogger();
67
68     /**
69      * Creates new instance of <code>EJBHashSet</code> for this parameters.
70      * @param pm the PersistenceManager associated with the calling bean.
71      * @param helper the JDOEJB20Helper instance.
72      * @param pcs a Collection of persistence-capable instances.
73      */

74     public EJBHashSet(PersistenceManager pm, JDOEJB20Helper helper, Collection pcs) {
75         this.pm = pm;
76         tx = pm.currentTransaction();
77         this.helper = helper;
78         if (logger.isLoggable(Logger.FINEST)) {
79             logger.finest("---EJBHashSet.new--- " + // NOI18N
80
((pcs == null)? -1: pcs.size()));
81         }
82
83         // Convert Collection.
84
setSCOHashSet(pcs);
85
86         valid = true;
87     }
88
89         // -------------------------Public Methods------------------
90

91     /**
92      * Adds the specified element to this set if it is not already
93      * present.
94      *
95      * @param o element to be added to this set.
96      * @return <tt>true</tt> if the set did not already contain the specified
97      * element.
98      * @see java.util.HashSet
99      */

100     public boolean add(Object JavaDoc o) {
101         logger.finest("---EJBHashSet.add---"); // NOI18N
102
assertIsValid();
103         assertInTransaction();
104         helper.assertInstanceOfLocalInterfaceImpl(o);
105         Object JavaDoc pc = helper.convertEJBLocalObjectToPC((EJBLocalObject JavaDoc) o, pm, true);
106         return pcSet.add(pc);
107     }
108
109
110     /**
111      * Adds all of the elements in the specified collection to this collection
112      *
113      * @param c collection whose elements are to be added to this collection.
114      * @return <tt>true</tt> if this collection changed as a result of the
115      * call.
116      * @throws UnsupportedOperationException if the <tt>addAll</tt> method is
117      * not supported by this collection.
118      *
119      * @see java.util.AbstractCollection
120      * @see java.util.HashSet
121      */

122     public boolean addAll(Collection c) {
123         logger.finest("---EJBHashSet.addAll---"); // NOI18N
124
assertIsValid();
125         assertInTransaction();
126         assertInstancesOfLocalInterfaceImpl(c);
127         return pcSet.addAll(helper.convertCollectionEJBLocalObjectToPC(c, pm, true));
128     }
129
130     /**
131      * Removes the given element from this set if it is present.
132      *
133      * @param o object to be removed from this set, if present.
134      * @return <tt>true</tt> if the set contained the specified element.
135      * @see java.util.HashSet
136      */

137     public boolean remove(Object JavaDoc o) {
138         logger.finest("---EJBHashSet.remove---"); // NOI18N
139
assertIsValid();
140         assertInTransaction();
141         helper.assertInstanceOfLocalInterfaceImpl(o);
142         EJBLocalObject JavaDoc lo = (EJBLocalObject JavaDoc) o;
143         return pcSet.remove(helper.convertEJBLocalObjectToPC(lo, pm, true));
144     }
145
146     /**
147      * Removes from this collection all of its elements that are contained in
148      * the specified collection (optional operation). <p>
149      * Processes each element remove internally not to have call backs
150      * into #remove(Object).
151      *
152      * @param c elements to be removed from this collection.
153      * @return <tt>true</tt> if this collection changed as a result of the
154      * call.
155      *
156      * @throws UnsupportedOperationException removeAll is not supported
157      * by this collection.
158      *
159      * @see java.util.HashSet
160      * @see java.util.AbstractCollection
161      */

162     public boolean removeAll(Collection c) {
163         logger.finest("---EJBHashSet.removeAll---"); // NOI18N
164
assertIsValid();
165         assertInTransaction();
166         assertInstancesOfLocalInterfaceImpl(c);
167         return pcSet.removeAll(helper.convertCollectionEJBLocalObjectToPC(c, pm, true));
168     }
169
170     /**
171      * Retains only the elements in this collection that are contained in the
172      * specified collection (optional operation).
173      *
174      * @return <tt>true</tt> if this collection changed as a result of the
175      * call.
176      *
177      * @throws UnsupportedOperationException if the <tt>retainAll</tt> method
178      * is not supported by this collection.
179      *
180      * @see java.util.HashSet
181      * @see java.util.AbstractCollection
182      */

183     public boolean retainAll(Collection c) {
184         logger.finest("---EJBHashSet.retainAll---"); // NOI18N
185
assertIsValid();
186         assertInTransaction();
187         assertInstancesOfLocalInterfaceImpl(c);
188         return pcSet.retainAll(helper.convertCollectionEJBLocalObjectToPC(c, pm, true));
189     }
190
191
192     /**
193      * Removes all of the elements from this set.
194      * @see java.util.HashSet
195      */

196     public void clear() {
197         logger.finest("---EJBHashSet.clear---"); // NOI18N
198
assertIsValid();
199         assertInTransaction();
200         pcSet.clear();
201     }
202
203     /**
204      * Returns the number of elements in this set (its cardinality).
205      *
206      * @return the number of elements in this set (its cardinality).
207      */

208     public int size() {
209         logger.finest("---EJBHashSet.size---"); // NOI18N
210
assertIsValid();
211         assertInTransaction();
212         return pcSet.size();
213     }
214
215     /**
216      * Returns <tt>true</tt> if this set contains no elements.
217      *
218      * @return <tt>true</tt> if this set contains no elements.
219      */

220     public boolean isEmpty() {
221         logger.finest("---EJBHashSet.isEmpty---"); // NOI18N
222
assertIsValid();
223         assertInTransaction();
224         return pcSet.isEmpty();
225     }
226
227     /**
228      * Returns <tt>true</tt> if this set contains the specified element.
229      *
230      * @param o element whose presence in this set is to be tested.
231      * @return <tt>true</tt> if this set contains the specified element.
232      */

233     public boolean contains(Object JavaDoc o) {
234         logger.finest("---EJBHashSet.contains---"); // NOI18N
235
assertIsValid();
236         assertInTransaction();
237         helper.assertInstanceOfLocalInterfaceImpl(o);
238         EJBLocalObject JavaDoc lo = (EJBLocalObject JavaDoc) o;
239         return pcSet.contains(helper.convertEJBLocalObjectToPC(lo, pm, true));
240     }
241
242     /**
243      * Returns <tt>true</tt> if this collection contains all of the elements
244      * in the specified collection. <p>
245      *
246      * This implementation iterates over the specified collection, checking
247      * each element returned by the iterator in turn to see if it's
248      * contained in this collection. If all elements are so contained
249      * <tt>true</tt> is returned, otherwise <tt>false</tt>.
250      *
251      * @param c collection to be checked for containment in this collection.
252      * @return <tt>true</tt> if this collection contains all of the elements
253      * in the specified collection.
254      *
255      * @see #contains(Object)
256      */

257     public boolean containsAll(Collection c) {
258         logger.finest("---EJBHashSet.containsAll---"); // NOI18N
259
assertIsValid();
260         assertInTransaction();
261         assertInstancesOfLocalInterfaceImpl(c);
262         return pcSet.containsAll(helper.convertCollectionEJBLocalObjectToPC(c, pm, true));
263     }
264
265     /**
266      * Returns a shallow copy of this <tt>HashSet</tt> instance: the elements
267      * themselves are not cloned.
268      *
269      * @return a shallow copy of this set.
270      */

271     public Object JavaDoc clone() {
272         logger.finest("---EJBHashSet.clone---"); // NOI18N
273
EJBHashSet newSet = (EJBHashSet)super.clone();
274         newSet.pcSet = (HashSet)pcSet.clone();
275         return newSet;
276     }
277
278     /**
279      * Returns set of the persistence-capable instances associated
280      * with this Set.
281      * @return Set of the persistence-capable instances.
282      */

283     public HashSet getSCOHashSet() {
284         assertIsValid();
285         assertInTransaction();
286         return (pcSet != null) ? (HashSet)pcSet.clone() : null;
287     }
288
289     /**
290      * Replace the set of the persistence-capable instances associated
291      * with this EJBHashSet.
292      * There is no need to check transaction as it has already been checked
293      * in this case.
294      */

295     public void setSCOHashSet(Collection coll) {
296         if (coll instanceof java.util.HashSet JavaDoc)
297             pcSet = (java.util.HashSet JavaDoc)coll;
298         else
299             pcSet = new java.util.HashSet JavaDoc(coll);
300     }
301
302     /**
303      * Returns an iterator over the elements in this set. The elements
304      * are returned in no particular order.
305      *
306      * @return an Iterator over the elements in this set.
307      * @see ConcurrentModificationException
308      */

309     public Iterator iterator() {
310         assertIsValid();
311         assertInTransaction();
312         return new EJBHashIterator();
313     }
314
315     private class EJBHashIterator implements Iterator {
316         Iterator _iterator = null;
317         Object JavaDoc lastReturned = null;
318
319         EJBHashIterator() {
320             _iterator = pcSet.iterator();
321         }
322
323         public boolean hasNext() {
324             assertIsValid();
325             assertInTransaction();
326             return _iterator.hasNext();
327         }
328
329         public Object JavaDoc next() {
330             assertIsValid();
331             assertInTransaction();
332             try {
333                 lastReturned = _iterator.next();
334             } catch(ConcurrentModificationException e) {
335                 IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc(
336                         e.toString());
337                 ise.initCause(e);
338                 throw ise;
339             }
340             return helper.convertPCToEJBLocalObject(lastReturned, pm);
341         }
342
343         public void remove() {
344             assertIsValid();
345             assertInTransaction();
346             try {
347                 _iterator.remove();
348             } catch(ConcurrentModificationException e) {
349                 IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc(
350                         e.toString());
351                 ise.initCause(e);
352                 throw ise;
353             }
354         }
355     }
356
357     /**
358      * Verifies that this Set is not marked as invalid.
359      * @throw IllegalStateException of validation fails.
360      */

361     private void assertIsValid() {
362         if (!valid)
363             throw new IllegalStateException JavaDoc(); // RESOLVE Exception text.
364
}
365
366     /**
367      * Verifies that persistence manager is not closed and
368      * the current transaction is active.
369      * @throw IllegalStateException of validation fails.
370      */

371     private void assertInTransaction() {
372         if (pm.isClosed() || !tx.isActive()) {
373             invalidate();
374             throw new IllegalStateException JavaDoc(); // RESOLVE Exception text.
375
}
376     }
377
378     /**
379      * Verifies that elements of this Collection are of the expected type.
380      * @param c the Collection to verify.
381      * @throw EJBException of validation fails.
382      */

383     private void assertInstancesOfLocalInterfaceImpl(Collection c) {
384         for (Iterator it = c.iterator(); it.hasNext();)
385             helper.assertInstanceOfLocalInterfaceImpl(it.next());
386     }
387
388     /**
389      * Marks this Set as invalid and releases all references.
390      */

391     public void invalidate() {
392         valid = false;
393         pm = null;
394         tx = null;
395         helper = null;
396         pcSet = null;
397     }
398 }
399
Popular Tags