Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

StoredCollection.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2000-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: StoredCollection.java,v 12.2 2005/10/05 20:40:10 mark Exp $
00008  */
00009 
00010 package com.sleepycat.collections;
00011 
00012 import java.util.ArrayList;
00013 import java.util.Arrays;
00014 import java.util.Collection;
00015 import java.util.Iterator;
00016 import java.util.List;
00017 
00018 import com.sleepycat.db.DatabaseException;
00019 import com.sleepycat.db.JoinConfig;
00020 import com.sleepycat.db.OperationStatus;
00021 
00051 public abstract class StoredCollection extends StoredContainer
00052     implements Collection {
00053 
00054     StoredCollection(DataView view) {
00055 
00056         super(view);
00057     }
00058 
00059     final boolean add(Object key, Object value) {
00060 
00061         DataCursor cursor = null;
00062         boolean doAutoCommit = beginAutoCommit();
00063         try {
00064             cursor = new DataCursor(view, true);
00065             OperationStatus status =
00066                 cursor.putNoDupData(key, value, null, false);
00067             closeCursor(cursor);
00068             commitAutoCommit(doAutoCommit);
00069             return (status == OperationStatus.SUCCESS);
00070         } catch (Exception e) {
00071             closeCursor(cursor);
00072             throw handleException(e, doAutoCommit);
00073         }
00074     }
00075 
00088     public Iterator iterator() {
00089 
00090         return iterator(isWriteAllowed());
00091     }
00092 
00112     public StoredIterator iterator(boolean writeAllowed) {
00113 
00114         try {
00115             return new StoredIterator(this, writeAllowed && isWriteAllowed(),
00116                                       null);
00117         } catch (Exception e) {
00118             throw StoredContainer.convertException(e);
00119         }
00120     }
00121 
00129     public Object[] toArray() {
00130 
00131         ArrayList list = new ArrayList();
00132         Iterator i = iterator();
00133         try {
00134             while (i.hasNext()) {
00135                 list.add(i.next());
00136             }
00137         } finally {
00138             StoredIterator.close(i);
00139         }
00140         return list.toArray();
00141     }
00142 
00152     public Object[] toArray(Object[] a) {
00153 
00154         int j = 0;
00155         Iterator i = iterator();
00156         try {
00157             while (j < a.length && i.hasNext()) {
00158                 a[j++] = i.next();
00159             }
00160             if (j < a.length) {
00161                 a[j] = null;
00162             } else if (i.hasNext()) {
00163                 ArrayList list = new ArrayList(Arrays.asList(a));
00164                 while (i.hasNext()) {
00165                     list.add(i.next());
00166                 }
00167                 a = list.toArray(a);
00168             }
00169         } finally {
00170             StoredIterator.close(i);
00171         }
00172         return a;
00173     }
00174 
00183     public boolean containsAll(Collection coll) {
00184         Iterator i = coll.iterator();
00185         try {
00186             while (i.hasNext()) {
00187                 if (!contains(i.next())) {
00188                     return false;
00189                 }
00190             }
00191         } finally {
00192             StoredIterator.close(i);
00193         }
00194         return true;
00195     }
00196 
00211     public boolean addAll(Collection coll) {
00212         Iterator i = null;
00213         boolean doAutoCommit = beginAutoCommit();
00214         try {
00215             i = coll.iterator();
00216             boolean changed = false;
00217             while (i.hasNext()) {
00218                 if (add(i.next())) {
00219                     changed = true;
00220                 }
00221             }
00222             StoredIterator.close(i);
00223             commitAutoCommit(doAutoCommit);
00224             return changed;
00225         } catch (Exception e) {
00226             StoredIterator.close(i);
00227             throw handleException(e, doAutoCommit);
00228         }
00229     }
00230 
00241     public boolean removeAll(Collection coll) {
00242 
00243         return removeAll(coll, true);
00244     }
00245 
00256     public boolean retainAll(Collection coll) {
00257 
00258         return removeAll(coll, false);
00259     }
00260 
00261     private boolean removeAll(Collection coll, boolean ifExistsInColl) {
00262         Iterator i = null;
00263         boolean doAutoCommit = beginAutoCommit();
00264         try {
00265             boolean changed = false;
00266             i = iterator();
00267             while (i.hasNext()) {
00268                 if (ifExistsInColl == coll.contains(i.next())) {
00269                     i.remove();
00270                     changed = true;
00271                 }
00272             }
00273             StoredIterator.close(i);
00274             commitAutoCommit(doAutoCommit);
00275             return changed;
00276         } catch (Exception e) {
00277             StoredIterator.close(i);
00278             throw handleException(e, doAutoCommit);
00279         }
00280     }
00281 
00291     public boolean equals(Object other) {
00292 
00293         if (other instanceof Collection) {
00294             Collection otherColl = StoredCollection.copyCollection(other);
00295             Iterator i = iterator();
00296             try {
00297                 while (i.hasNext()) {
00298                     if (!otherColl.remove(i.next())) {
00299                         return false;
00300                     }
00301                 }
00302                 return otherColl.isEmpty();
00303             } finally {
00304                 StoredIterator.close(i);
00305             }
00306         } else {
00307             return false;
00308         }
00309     }
00310 
00311     /*
00312      * Add this in to keep FindBugs from whining at us about implementing
00313      * equals(), but not hashCode().
00314      */
00315     public int hashCode() {
00316         return super.hashCode();
00317     }
00318 
00329     public List toList() {
00330 
00331         ArrayList list = new ArrayList();
00332         Iterator i = iterator();
00333         try {
00334             while (i.hasNext()) list.add(i.next());
00335             return list;
00336         } finally {
00337             StoredIterator.close(i);
00338         }
00339     }
00340 
00350     public String toString() {
00351         StringBuffer buf = new StringBuffer();
00352         buf.append("[");
00353         Iterator i = iterator();
00354         try {
00355             while (i.hasNext()) {
00356                 if (buf.length() > 1) buf.append(',');
00357                 buf.append(i.next().toString());
00358             }
00359             buf.append(']');
00360             return buf.toString();
00361         } finally {
00362             StoredIterator.close(i);
00363         }
00364     }
00365 
00392     public StoredIterator join(StoredContainer[] indices, Object[] indexKeys,
00393                                JoinConfig joinConfig) {
00394 
00395         try {
00396             DataView[] indexViews = new DataView[indices.length];
00397             for (int i = 0; i < indices.length; i += 1) {
00398                 indexViews[i] = indices[i].view;
00399             }
00400             DataCursor cursor = view.join(indexViews, indexKeys, joinConfig);
00401             return new StoredIterator(this, false, cursor);
00402         } catch (Exception e) {
00403             throw StoredContainer.convertException(e);
00404         }
00405     }
00406 
00407     final Object getFirstOrLast(boolean doGetFirst) {
00408 
00409         DataCursor cursor = null;
00410         try {
00411             cursor = new DataCursor(view, false);
00412             OperationStatus status;
00413             if (doGetFirst) {
00414                 status = cursor.getFirst(false);
00415             } else {
00416                 status = cursor.getLast(false);
00417             }
00418             return (status == OperationStatus.SUCCESS) ?
00419                     makeIteratorData(null, cursor) : null;
00420         } catch (Exception e) {
00421             throw StoredContainer.convertException(e);
00422         } finally {
00423             closeCursor(cursor);
00424         }
00425     }
00426 
00427     abstract Object makeIteratorData(StoredIterator iterator,
00428                                      DataCursor cursor)
00429         throws DatabaseException;
00430 
00431     abstract boolean hasValues();
00432 
00433     boolean iterateDuplicates() {
00434 
00435         return true;
00436     }
00437 
00438     void checkIterAddAllowed()
00439         throws UnsupportedOperationException {
00440 
00441         if (!areDuplicatesAllowed()) {
00442             throw new UnsupportedOperationException("duplicates required");
00443         }
00444     }
00445 
00446     int getIndexOffset() {
00447 
00448         return 0;
00449     }
00450 
00451     private static Collection copyCollection(Object other) {
00452 
00453         if (other instanceof StoredCollection) {
00454             return ((StoredCollection) other).toList();
00455         } else {
00456             return new ArrayList((Collection) other);
00457         }
00458     }
00459 }

Generated on Sun Dec 25 12:14:31 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2