KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > container > jorm > Set


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: Set.java,v 1.17 2004/09/24 15:05:11 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas_ejb.container.jorm;
26
27 import org.objectweb.jorm.api.PClassMapping;
28 import org.objectweb.jorm.api.PException;
29
30 import javax.ejb.EJBException JavaDoc;
31 import javax.ejb.NoSuchObjectLocalException JavaDoc;
32
33 import java.lang.reflect.Array JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36 /**
37  * This class is a basic implementation of the java.util.Set based on the
38  * generic class implementation (GenClassImpl) and the Collection
39  * implementation. This class can be used to represent a relation between bean.
40  *
41  * @author S.Chassande-Barrioz : Initial developer
42  * @author Helene Joanin : fix bugs in the toArray(...) methods
43  */

44 public class Set extends GenClassImpl implements java.util.Set JavaDoc {
45
46     /**
47      * Called from bean template (.vm)
48      */

49     public Set(PClassMapping gcm) {
50         super(gcm);
51     }
52
53     public Set() {
54         super();
55     }
56
57     /**
58      * Before adding the object into the set, it checks that the new element
59      * does not exist.
60      * @param o is the element to add
61      * @return true is the element has been added, and false is the element
62      * already exists in the set.
63      */

64     public boolean add(Object JavaDoc o) {
65         return add(o, true);
66     }
67
68     public boolean add(Object JavaDoc o, boolean callListener) {
69         try {
70             if (!gcContains((PObject) o, null)) {
71                 gcAdd((PObject) o, callListener);
72                 return true;
73             } else {
74                 return false;
75             }
76         } catch (NoSuchObjectLocalException JavaDoc e) {
77             throw new IllegalArgumentException JavaDoc(e.getMessage());
78         } catch (PException e) {
79             e.printStackTrace();
80             return false;
81         }
82     }
83     //------------------------------------//
84

85     public int size() {
86         return size;
87     }
88
89     public boolean isEmpty() {
90         return size == 0;
91     }
92
93     public boolean contains(Object JavaDoc o) {
94         try {
95             return gcContains((PObject) o, null);
96         } catch (PException e) {
97             e.printStackTrace();
98             throw new ArrayStoreException JavaDoc(e.getMessage());
99         }
100     }
101
102     public Iterator JavaDoc iterator() {
103         try {
104             return gcIterator();
105         } catch (PException e) {
106             e.printStackTrace();
107             throw new ArrayStoreException JavaDoc(e.getMessage());
108         }
109     }
110
111     /**
112      * It returns an array of the elements.
113      * @return array of the elements
114      */

115     public Object JavaDoc[] toArray() {
116         return toArray(new Object JavaDoc[size]);
117     }
118
119     /**
120      * It returns an array of the elements.
121      * It is built by an iteration over the existing elements.
122      * @param objects the array into which the elements of this collection are to be stored
123      * @return array of the elements
124      */

125     public Object JavaDoc[] toArray(Object JavaDoc[] objects) {
126         try {
127             int i = 0;
128             for (Iterator JavaDoc it = gcIterator(); it.hasNext();) {
129                 objects[i++] = it.next();
130             }
131         } catch (PException e) {
132             e.printStackTrace();
133             throw new ArrayStoreException JavaDoc(e.getMessage());
134         }
135         return objects;
136     }
137
138     public boolean remove(Object JavaDoc o) {
139         try {
140             return gcRemove(o, true) != null;
141         } catch (PException e) {
142             throw new EJBException JavaDoc(e);
143         }
144
145     }
146
147     public boolean remove(Object JavaDoc o, boolean callListener) {
148         try {
149             return gcRemove(o, callListener) != null;
150         } catch (PException e) {
151             throw new EJBException JavaDoc(e);
152         }
153
154     }
155
156     public boolean containsAll(java.util.Collection JavaDoc collection) {
157         if (collection == null) {
158             return true;
159         }
160         try {
161             boolean res = true;
162             Object JavaDoc conn = gcm.getPMapper().getConnection();
163             for (Iterator JavaDoc it = collection.iterator(); it.hasNext() && res;) {
164                 res = gcContains((PObject) it.next(), conn);
165             }
166             gcm.getPMapper().closeConnection(conn);
167             return res;
168         } catch (PException e) {
169             e.printStackTrace();
170             throw new ArrayStoreException JavaDoc(e.getMessage());
171         }
172     }
173
174     /**
175      * It iterates over the collection parameter to add each element in the
176      * collection.
177      * @param collection the collection of elements to add
178      */

179     public boolean addAll(java.util.Collection JavaDoc collection) {
180         if (collection == null) {
181             return true;
182         }
183         boolean res = true;
184         for (Iterator JavaDoc it = collection.iterator(); it.hasNext();) {
185             res &= add((PObject) it.next());
186         }
187         return res;
188     }
189
190     /**
191      * It iterates over the collection parameter to remove each element in the
192      * collection.
193      * @param collection The collection of elements to remove
194      */

195     public boolean removeAll(java.util.Collection JavaDoc collection) {
196         if (collection == null) {
197             return true;
198         }
199         try {
200             for (Iterator JavaDoc it = collection.iterator(); it.hasNext();) {
201                 gcRemove((PObject) it.next(), true);
202             }
203         } catch (PException e) {
204             throw new EJBException JavaDoc(e);
205         }
206         return true;
207     }
208
209     /**
210      * For each element of the current collection, it checks if it exist into
211      * the collection parameter. If it does not found then it is removed from
212      * the current collection.
213      */

214     public boolean retainAll(java.util.Collection JavaDoc collection) {
215         if (collection == null) {
216             clear();
217             return true;
218         }
219         try {
220             for (Iterator JavaDoc it = iterator(); it.hasNext();) {
221                 PObject o = (PObject) it.next();
222                 if (!collection.contains(o)) {
223                     gcRemove(o, true);
224                 }
225             }
226         } catch (PException e) {
227             throw new EJBException JavaDoc(e);
228         }
229         return true;
230     }
231
232     /**
233      * It removes all elements.
234      */

235     public void clear() {
236         gcClear(false);
237     }
238
239 }
240
Popular Tags