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: Set.java 10145 2007-04-05 06:50:20Z durieuxp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ejb.container.jorm;
025:
026: import org.objectweb.jorm.api.PClassMapping;
027: import org.objectweb.jorm.api.PException;
028:
029: import javax.ejb.EJBException;
030: import javax.ejb.NoSuchObjectLocalException;
031:
032: import java.lang.reflect.Array;
033: import java.util.Iterator;
034:
035: import org.objectweb.jonas_ejb.container.TraceEjb;
036: import org.objectweb.util.monolog.api.BasicLevel;
037:
038: /**
039: * This class is a basic implementation of the java.util.Set based on the
040: * generic class implementation (GenClassImpl) and the Collection
041: * implementation. This class can be used to represent a relation between bean.
042: *
043: * @author S.Chassande-Barrioz : Initial developer
044: * @author Helene Joanin : fix bugs in the toArray(...) methods
045: */
046: public class Set extends GenClassImpl implements java.util.Set {
047:
048: public Set() {
049: super ();
050: }
051:
052: /**
053: * Before adding the object into the set, it checks that the new element
054: * does not exist.
055: * @param o is the element to add
056: * @return true is the element has been added, and false is the element
057: * already exists in the set.
058: */
059: public boolean add(Object o) {
060: return add(o, true);
061: }
062:
063: public boolean add(Object o, boolean callListener) {
064: TraceEjb.genclass.log(BasicLevel.DEBUG, "");
065: try {
066: if (!gcContains((PObject) o, null)) {
067: gcAdd((PObject) o, callListener);
068: return true;
069: } else {
070: return false;
071: }
072: } catch (NoSuchObjectLocalException e) {
073: throw new IllegalArgumentException(e.getMessage());
074: } catch (PException e) {
075: e.printStackTrace();
076: return false;
077: }
078: }
079:
080: //------------------------------------//
081:
082: public int size() {
083: return size;
084: }
085:
086: public boolean isEmpty() {
087: return size == 0;
088: }
089:
090: public boolean contains(Object o) {
091: TraceEjb.genclass.log(BasicLevel.DEBUG, "");
092: try {
093: return gcContains((PObject) o, null);
094: } catch (PException e) {
095: e.printStackTrace();
096: throw new ArrayStoreException(e.getMessage());
097: }
098: }
099:
100: public Iterator iterator() {
101: try {
102: return gcIterator();
103: } catch (PException e) {
104: e.printStackTrace();
105: throw new ArrayStoreException(e.getMessage());
106: }
107: }
108:
109: /**
110: * It returns an array of the elements.
111: * @return array of the elements
112: */
113: public Object[] toArray() {
114: return toArray(new Object[size]);
115: }
116:
117: /**
118: * It returns an array of the elements.
119: * It is built by an iteration over the existing elements.
120: * @param objects the array into which the elements of this collection are to be stored
121: * @return array of the elements
122: */
123: public Object[] toArray(Object[] objects) {
124: try {
125: int i = 0;
126: for (Iterator it = gcIterator(); it.hasNext();) {
127: objects[i++] = it.next();
128: }
129: } catch (PException e) {
130: e.printStackTrace();
131: throw new ArrayStoreException(e.getMessage());
132: }
133: return objects;
134: }
135:
136: public boolean remove(Object o) {
137: try {
138: return gcRemove(o, true) != null;
139: } catch (PException e) {
140: throw new EJBException(e);
141: }
142:
143: }
144:
145: public boolean remove(Object o, boolean callListener) {
146: TraceEjb.genclass.log(BasicLevel.DEBUG, "");
147: try {
148: return gcRemove(o, callListener) != null;
149: } catch (PException e) {
150: throw new EJBException(e);
151: }
152:
153: }
154:
155: public boolean containsAll(java.util.Collection collection) {
156: TraceEjb.genclass.log(BasicLevel.DEBUG, "");
157: if (collection == null) {
158: return true;
159: }
160: try {
161: boolean res = true;
162: Object conn = gcm.getPMapper().getConnection();
163: for (Iterator it = collection.iterator(); it.hasNext()
164: && 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(e.getMessage());
172: }
173: }
174:
175: /**
176: * It iterates over the collection parameter to add each element in the
177: * collection.
178: * @param collection the collection of elements to add
179: */
180: public boolean addAll(java.util.Collection collection) {
181: TraceEjb.genclass.log(BasicLevel.DEBUG, "");
182: if (collection == null) {
183: return true;
184: }
185: boolean res = true;
186: for (Iterator it = collection.iterator(); it.hasNext();) {
187: res &= add((PObject) it.next());
188: }
189: return res;
190: }
191:
192: /**
193: * It iterates over the collection parameter to remove each element in the
194: * collection.
195: * @param collection The collection of elements to remove
196: */
197: public boolean removeAll(java.util.Collection collection) {
198: TraceEjb.genclass.log(BasicLevel.DEBUG, "");
199: if (collection == null) {
200: return true;
201: }
202: try {
203: for (Iterator it = collection.iterator(); it.hasNext();) {
204: gcRemove((PObject) it.next(), true);
205: }
206: } catch (PException e) {
207: throw new EJBException(e);
208: }
209: return true;
210: }
211:
212: /**
213: * For each element of the current collection, it checks if it exist into
214: * the collection parameter. If it does not found then it is removed from
215: * the current collection.
216: */
217: public boolean retainAll(java.util.Collection collection) {
218: TraceEjb.genclass.log(BasicLevel.DEBUG, "");
219: if (collection == null) {
220: clear();
221: return true;
222: }
223: try {
224: for (Iterator it = iterator(); it.hasNext();) {
225: PObject o = (PObject) it.next();
226: if (!collection.contains(o)) {
227: gcRemove(o, true);
228: }
229: }
230: } catch (PException e) {
231: throw new EJBException(e);
232: }
233: return true;
234: }
235:
236: /**
237: * It removes all elements.
238: */
239: public void clear() {
240: TraceEjb.genclass.log(BasicLevel.DEBUG, "");
241: gcClear(false);
242: }
243:
244: }
|