001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: Collection.java 7415 2005-09-26 09:40:22Z durieuxp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ejb.container.jorm;
025:
026: import java.util.Iterator;
027: import java.lang.reflect.Array;
028: import javax.ejb.EJBException;
029: import org.objectweb.jorm.api.PIndexedElem;
030: import org.objectweb.jorm.api.PClassMapping;
031: import org.objectweb.jorm.api.PException;
032:
033: /**
034: * This class is a basic implementation of the java.util.Collection based on the
035: * generic class implementation (GenClassImpl). This class can be used to
036: * represent a relation between bean.
037: *
038: * @author S.Chassande-Barrioz : Initial developer
039: * @author Helene Joanin : fix bugs in the toArray(...) methods
040: */
041: public class Collection extends GenClassImpl implements
042: java.util.Collection {
043:
044: int nextIndex = -1;
045:
046: public Collection() {
047: super ();
048: }
049:
050: /**
051: *
052: */
053: public PIndexedElem createPIndexedElem() {
054: if (pIndexedElems.size() == 0) {
055: nextIndex = 0;
056: }
057: return new CollectionElement(this , nextIndex++);
058: }
059:
060: // IMPLEMENT THE Collection INTERFACE //
061: //------------------------------------//
062:
063: public int size() {
064: return size;
065: }
066:
067: public boolean isEmpty() {
068: return size == 0;
069: }
070:
071: public boolean contains(Object o) {
072: try {
073: return gcContains((PObject) o, null);
074: } catch (PException e) {
075: e.printStackTrace();
076: throw new ArrayStoreException(e.getMessage());
077: }
078: }
079:
080: public Iterator iterator() {
081: try {
082: return gcIterator();
083: } catch (PException e) {
084: e.printStackTrace();
085: throw new ArrayStoreException(e.getMessage());
086: }
087: }
088:
089: /**
090: * It returns an array of the elements.
091: * @return array of the elements
092: */
093: public Object[] toArray() {
094: return toArray(new Object[size]);
095: }
096:
097: /**
098: * It returns an array of the elements.
099: * It is built by an iteration over the existing elements.
100: * @param objects the array into which the elements of this collection are to be stored
101: * @return array of the elements
102: */
103: public Object[] toArray(Object[] objects) {
104: try {
105: int i = 0;
106: for (Iterator it = gcIterator(); it.hasNext();) {
107: objects[i++] = it.next();
108: }
109: } catch (PException e) {
110: e.printStackTrace();
111: throw new ArrayStoreException(e.getMessage());
112: }
113: return objects;
114: }
115:
116: /**
117: *
118: */
119: public boolean add(Object o) {
120: gcAdd((PObject) o, true);
121: return true;
122: }
123:
124: /**
125: *
126: */
127: public boolean remove(Object o) {
128: try {
129: return gcRemove(o, true) != null;
130: } catch (PException e) {
131: throw new EJBException(e);
132: }
133: }
134:
135: /**
136: *
137: */
138: public boolean remove(Object o, boolean callListener) {
139: try {
140: return gcRemove(o, callListener) != null;
141: } catch (PException e) {
142: throw new EJBException(e);
143: }
144: }
145:
146: /**
147: *
148: */
149: public boolean containsAll(java.util.Collection collection) {
150: if (collection == null) {
151: return true;
152: }
153: try {
154: boolean res = true;
155: Object conn = gcm.getPMapper().getConnection();
156: for (Iterator it = collection.iterator(); it.hasNext()
157: && res;) {
158: res = gcContains((PObject) it.next(), conn);
159: }
160: gcm.getPMapper().closeConnection(conn);
161: return res;
162: } catch (PException e) {
163: e.printStackTrace();
164: throw new ArrayStoreException(e.getMessage());
165: }
166: }
167:
168: /**
169: * It iterates over the collection parameter to add each element in the
170: * collection.
171: */
172: public boolean addAll(java.util.Collection collection) {
173: if (collection == null) {
174: return true;
175: }
176: boolean res = true;
177: for (Iterator it = collection.iterator(); it.hasNext();) {
178: res &= add((PObject) it.next());
179: }
180: return res;
181: }
182:
183: /**
184: * It iterates over the collection parameter to remove each element in the
185: * collection.
186: */
187: public boolean removeAll(java.util.Collection collection) {
188: if (collection == null) {
189: return true;
190: }
191: try {
192: for (Iterator it = collection.iterator(); it.hasNext();) {
193: gcRemove((PObject) it.next(), true);
194: }
195: } catch (PException e) {
196: throw new EJBException(e);
197: }
198: return true;
199: }
200:
201: /**
202: * For each element of the current collection, it checks if it exist into
203: * the collection parameter. If it does not found then it is removed from
204: * the current collection.
205: */
206: public boolean retainAll(java.util.Collection collection) {
207: if (collection == null) {
208: clear();
209: return true;
210: }
211: try {
212: for (Iterator it = iterator(); it.hasNext();) {
213: PObject o = (PObject) it.next();
214: if (!collection.contains(o))
215: gcRemove(o, true);
216: }
217: } catch (PException e) {
218: throw new EJBException(e);
219: }
220: return true;
221: }
222:
223: /**
224: * It removes all elements.
225: */
226: public void clear() {
227: gcClear(false);
228: }
229: }
|