1 47 48 package org.ozoneDB.collections; 49 50 import java.lang.reflect.Array ; 51 import java.util.Collection ; 52 import java.util.Iterator ; 53 import org.ozoneDB.OzoneObject; 54 55 81 public abstract class AbstractOzoneCollection extends OzoneObject implements OzoneCollection { 82 83 private static final long serialVersionUID = 1L; 84 85 protected AbstractOzoneCollection() { 86 } 87 88 95 public abstract Iterator iterator(); 96 97 103 public abstract int size(); 104 105 121 public boolean add(Object o) { 122 throw new UnsupportedOperationException (); 123 } 124 125 146 public boolean addAll(Collection c) { 147 Iterator itr; 148 if (c instanceof OzoneCollection) { 149 itr = ((OzoneCollection) c)._org_ozoneDB_internalIterator(); 150 } else { 151 itr = c.iterator(); 152 } 153 boolean modified = false; 154 int pos = c.size(); 155 while (--pos >= 0) 156 modified |= add(itr.next()); 157 return modified; 158 } 159 160 172 public void clear() { 173 Iterator itr = _org_ozoneDB_internalIterator(); 174 int pos = size(); 175 while (--pos >= 0) { 176 itr.next(); 177 itr.remove(); 178 } 179 } 180 181 192 public boolean contains(Object o) { 193 Iterator itr = _org_ozoneDB_internalIterator(); 194 int pos = size(); 195 while (--pos >= 0) 196 if (equals(o, itr.next())) 197 return true; 198 return false; 199 } 200 201 213 public boolean containsAll(Collection c) { 214 Iterator itr; 215 if (c instanceof OzoneCollection) { 216 itr = ((OzoneCollection) c)._org_ozoneDB_internalIterator(); 217 } else { 218 itr = c.iterator(); 219 } 220 int pos = c.size(); 221 while (--pos >= 0) 222 if (!contains(itr.next())) 223 return false; 224 return true; 225 } 226 227 234 public boolean isEmpty() { 235 return size() == 0; 236 } 237 238 257 public boolean remove(Object o) { 258 Iterator itr = _org_ozoneDB_internalIterator(); 259 int pos = size(); 260 while (--pos >= 0) 261 if (equals(o, itr.next())) { 262 itr.remove(); 263 return true; 264 } 265 return false; 266 } 267 268 282 public boolean removeAll(Collection c) { 283 return removeAllInternal(c); 284 } 285 286 301 boolean removeAllInternal(Collection c) { 302 Iterator itr; 303 if (c instanceof OzoneCollection) { 304 itr = ((OzoneCollection) c)._org_ozoneDB_internalIterator(); 305 } else { 306 itr = c.iterator(); 307 } 308 boolean modified = false; 309 int pos = size(); 310 while (--pos >= 0) 311 if (c.contains(itr.next())) { 312 itr.remove(); 313 modified = true; 314 } 315 return modified; 316 } 317 318 332 public boolean retainAll(Collection c) { 333 return retainAllInternal(c); 334 } 335 336 352 boolean retainAllInternal(Collection c) { 353 Iterator itr; 354 if (c instanceof OzoneCollection) { 355 itr = ((OzoneCollection) c)._org_ozoneDB_internalIterator(); 356 } else { 357 itr = c.iterator(); 358 } 359 boolean modified = false; 360 int pos = size(); 361 while (--pos >= 0) 362 if (!c.contains(itr.next())) { 363 itr.remove(); 364 modified = true; 365 } 366 return modified; 367 } 368 369 378 public Object [] toArray() { 379 Iterator itr = _org_ozoneDB_internalIterator(); 380 int size = size(); 381 Object [] a = new Object [size]; 382 for (int pos = 0; pos < size; pos++) 383 a[pos] = itr.next(); 384 return a; 385 } 386 387 409 public Object [] toArray(Object [] a) { 410 int size = size(); 411 if (a.length < size) 412 a = (Object []) Array.newInstance(a.getClass().getComponentType(), 413 size); 414 else if (a.length > size) 415 a[size] = null; 416 417 Iterator itr = _org_ozoneDB_internalIterator(); 418 for (int pos = 0; pos < size; pos++) 419 a[pos] = itr.next(); 420 421 return a; 422 } 423 424 433 public String toString() { 434 Iterator itr = _org_ozoneDB_internalIterator(); 435 StringBuffer r = new StringBuffer ("["); 436 for (int pos = size(); pos > 0; pos--) { 437 r.append(itr.next()); 438 if (pos > 1) 439 r.append(", "); 440 } 441 r.append("]"); 442 return r.toString(); 443 } 444 445 452 static final boolean equals(Object o1, Object o2) { 455 return o1 == null ? o2 == null : o1.equals(o2); 456 } 457 458 464 static final int hashCode(Object o) { 467 return o == null ? 0 : o.hashCode(); 468 } 469 470 486 public Collection getClientCollection() { 487 throw new UnsupportedOperationException (); 488 } 489 490 } | Popular Tags |