00001
00026 package org.objectweb.jonas_ejb.container.jorm;
00027
00028 import java.util.Iterator;
00029 import java.lang.reflect.Array;
00030 import javax.ejb.EJBException;
00031 import org.objectweb.jorm.api.PIndexedElem;
00032 import org.objectweb.jorm.api.PClassMapping;
00033 import org.objectweb.jorm.api.PException;
00034
00043 public class Collection extends GenClassImpl implements java.util.Collection {
00044
00045 int nextIndex = -1;
00046
00050 public Collection(PClassMapping gcm) {
00051 super(gcm);
00052 }
00053
00054 public Collection() {
00055 super();
00056 }
00057
00061 public PIndexedElem createPIndexedElem() {
00062 if (pIndexedElems.size() == 0) {
00063 nextIndex = 0;
00064 }
00065 return new CollectionElement(this, nextIndex++);
00066 }
00067
00068
00069
00070
00071 public int size() {
00072 return size;
00073 }
00074
00075 public boolean isEmpty() {
00076 return size == 0;
00077 }
00078
00079 public boolean contains(Object o) {
00080 try {
00081 return gcContains((PObject) o, null);
00082 } catch (PException e) {
00083 e.printStackTrace();
00084 throw new ArrayStoreException(e.getMessage());
00085 }
00086 }
00087
00088 public Iterator iterator() {
00089 try {
00090 return gcIterator();
00091 } catch (PException e) {
00092 e.printStackTrace();
00093 throw new ArrayStoreException(e.getMessage());
00094 }
00095 }
00096
00101 public Object[] toArray() {
00102 return toArray(new Object[size]);
00103 }
00104
00111 public Object[] toArray(Object[] objects) {
00112 try {
00113 int i = 0;
00114 for (Iterator it = gcIterator(); it.hasNext();) {
00115 objects[i++] = it.next();
00116 }
00117 } catch (PException e) {
00118 e.printStackTrace();
00119 throw new ArrayStoreException(e.getMessage());
00120 }
00121 return objects;
00122 }
00123
00127 public boolean add(Object o) {
00128 gcAdd((PObject) o, true);
00129 return true;
00130 }
00131
00135 public boolean remove(Object o) {
00136 try {
00137 return gcRemove(o, true) != null;
00138 } catch (PException e) {
00139 throw new EJBException(e);
00140 }
00141 }
00142
00146 public boolean remove(Object o, boolean callListener) {
00147 try {
00148 return gcRemove(o, callListener) != null;
00149 } catch (PException e) {
00150 throw new EJBException(e);
00151 }
00152 }
00153
00157 public boolean containsAll(java.util.Collection collection) {
00158 if (collection == null) {
00159 return true;
00160 }
00161 try {
00162 boolean res = true;
00163 Object conn = gcm.getPMapper().getConnection();
00164 for (Iterator it = collection.iterator(); it.hasNext() && res;) {
00165 res = gcContains((PObject) it.next(), conn);
00166 }
00167 gcm.getPMapper().closeConnection(conn);
00168 return res;
00169 } catch (PException e) {
00170 e.printStackTrace();
00171 throw new ArrayStoreException(e.getMessage());
00172 }
00173 }
00174
00179 public boolean addAll(java.util.Collection collection) {
00180 if (collection == null) {
00181 return true;
00182 }
00183 boolean res = true;
00184 for (Iterator it = collection.iterator(); it.hasNext();) {
00185 res &= add((PObject) it.next());
00186 }
00187 return res;
00188 }
00189
00194 public boolean removeAll(java.util.Collection collection) {
00195 if (collection == null) {
00196 return true;
00197 }
00198 try {
00199 for (Iterator it = collection.iterator(); it.hasNext();) {
00200 gcRemove((PObject) it.next(), true);
00201 }
00202 } catch (PException e) {
00203 throw new EJBException(e);
00204 }
00205 return true;
00206 }
00207
00213 public boolean retainAll(java.util.Collection collection) {
00214 if (collection == null) {
00215 clear();
00216 return true;
00217 }
00218 try {
00219 for (Iterator it = iterator(); it.hasNext();) {
00220 PObject o = (PObject) it.next();
00221 if (!collection.contains(o))
00222 gcRemove(o, true);
00223 }
00224 } catch (PException e) {
00225 throw new EJBException(e);
00226 }
00227 return true;
00228 }
00229
00233 public void clear() {
00234 gcClear(false);
00235 }
00236 }