KickJava   Java API By Example, From Geeks To Geeks.

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


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: Collection.java,v 1.13 2004/09/24 15:05:11 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

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

43 public class Collection extends GenClassImpl implements java.util.Collection JavaDoc {
44
45     int nextIndex = -1;
46
47     /**
48      * Called from bean template (.vm)
49      */

50     public Collection(PClassMapping gcm) {
51         super(gcm);
52     }
53
54     public Collection() {
55         super();
56     }
57
58     /**
59      *
60      */

61     public PIndexedElem createPIndexedElem() {
62         if (pIndexedElems.size() == 0) {
63             nextIndex = 0;
64         }
65         return new CollectionElement(this, nextIndex++);
66     }
67
68     // IMPLEMENT THE Collection INTERFACE //
69
//------------------------------------//
70

71     public int size() {
72         return size;
73     }
74
75     public boolean isEmpty() {
76         return size == 0;
77     }
78
79     public boolean contains(Object JavaDoc o) {
80         try {
81             return gcContains((PObject) o, null);
82         } catch (PException e) {
83             e.printStackTrace();
84             throw new ArrayStoreException JavaDoc(e.getMessage());
85         }
86     }
87
88     public Iterator JavaDoc iterator() {
89         try {
90             return gcIterator();
91         } catch (PException e) {
92             e.printStackTrace();
93             throw new ArrayStoreException JavaDoc(e.getMessage());
94         }
95     }
96
97     /**
98      * It returns an array of the elements.
99      * @return array of the elements
100      */

101     public Object JavaDoc[] toArray() {
102         return toArray(new Object JavaDoc[size]);
103     }
104
105     /**
106      * It returns an array of the elements.
107      * It is built by an iteration over the existing elements.
108      * @param objects the array into which the elements of this collection are to be stored
109      * @return array of the elements
110      */

111     public Object JavaDoc[] toArray(Object JavaDoc[] objects) {
112         try {
113             int i = 0;
114             for (Iterator JavaDoc it = gcIterator(); it.hasNext();) {
115                 objects[i++] = it.next();
116             }
117         } catch (PException e) {
118             e.printStackTrace();
119             throw new ArrayStoreException JavaDoc(e.getMessage());
120         }
121         return objects;
122     }
123
124     /**
125      *
126      */

127     public boolean add(Object JavaDoc o) {
128         gcAdd((PObject) o, true);
129         return true;
130     }
131
132     /**
133      *
134      */

135     public boolean remove(Object JavaDoc o) {
136         try {
137             return gcRemove(o, true) != null;
138         } catch (PException e) {
139             throw new EJBException JavaDoc(e);
140         }
141     }
142
143     /**
144      *
145      */

146     public boolean remove(Object JavaDoc o, boolean callListener) {
147         try {
148             return gcRemove(o, callListener) != null;
149         } catch (PException e) {
150             throw new EJBException JavaDoc(e);
151         }
152     }
153
154     /**
155      *
156      */

157     public boolean containsAll(java.util.Collection JavaDoc collection) {
158         if (collection == null) {
159             return true;
160         }
161         try {
162             boolean res = true;
163             Object JavaDoc conn = gcm.getPMapper().getConnection();
164             for (Iterator JavaDoc it = collection.iterator(); it.hasNext() && res;) {
165                 res = gcContains((PObject) it.next(), conn);
166             }
167             gcm.getPMapper().closeConnection(conn);
168             return res;
169         } catch (PException e) {
170             e.printStackTrace();
171             throw new ArrayStoreException JavaDoc(e.getMessage());
172         }
173     }
174
175     /**
176      * It iterates over the collection parameter to add each element in the
177      * collection.
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      */

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

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

233     public void clear() {
234         gcClear(false);
235     }
236 }
237
Popular Tags