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

StoredEntrySet.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: StoredEntrySet.java,v 12.1 2005/01/31 19:27:32 mark Exp $
00008  */
00009 
00010 package com.sleepycat.collections;
00011 
00012 import java.util.Iterator;
00013 import java.util.Map;
00014 import java.util.Set;
00015 
00016 import com.sleepycat.db.DatabaseException;
00017 import com.sleepycat.db.OperationStatus;
00018 
00041 public class StoredEntrySet extends StoredCollection implements Set {
00042 
00043     StoredEntrySet(DataView mapView) {
00044 
00045         super(mapView);
00046     }
00047 
00065     public boolean add(Object mapEntry) {
00066 
00067         Map.Entry entry = (Map.Entry) mapEntry; // allow ClassCastException
00068         return add(entry.getKey(), entry.getValue());
00069     }
00070 
00086     public boolean remove(Object mapEntry) {
00087 
00088         if (!(mapEntry instanceof Map.Entry)) {
00089             return false;
00090         }
00091         Map.Entry entry = (Map.Entry) mapEntry;
00092         DataCursor cursor = null;
00093         boolean doAutoCommit = beginAutoCommit();
00094         try {
00095             cursor = new DataCursor(view, true);
00096             OperationStatus status =
00097                 cursor.getSearchBoth(entry.getKey(), entry.getValue(), true);
00098             if (status == OperationStatus.SUCCESS) {
00099                 cursor.delete();
00100             }
00101             closeCursor(cursor);
00102             commitAutoCommit(doAutoCommit);
00103             return (status == OperationStatus.SUCCESS);
00104         } catch (Exception e) {
00105             closeCursor(cursor);
00106             throw handleException(e, doAutoCommit);
00107         }
00108     }
00109 
00122     public boolean contains(Object mapEntry) {
00123 
00124         if (!(mapEntry instanceof Map.Entry)) {
00125             return false;
00126         }
00127         Map.Entry entry = (Map.Entry) mapEntry;
00128         DataCursor cursor = null;
00129         try {
00130             cursor = new DataCursor(view, false);
00131             OperationStatus status =
00132                 cursor.getSearchBoth(entry.getKey(), entry.getValue(), false);
00133             return (status == OperationStatus.SUCCESS);
00134         } catch (Exception e) {
00135             throw StoredContainer.convertException(e);
00136         } finally {
00137             closeCursor(cursor);
00138         }
00139     }
00140 
00141     // javadoc is inherited
00142     public String toString() {
00143         StringBuffer buf = new StringBuffer();
00144         buf.append("[");
00145         Iterator i = iterator();
00146         try {
00147             while (i.hasNext()) {
00148                 Map.Entry entry = (Map.Entry) i.next();
00149                 if (buf.length() > 1) buf.append(',');
00150                 Object key = entry.getKey();
00151                 Object val = entry.getValue();
00152                 if (key != null) buf.append(key.toString());
00153                 buf.append('=');
00154                 if (val != null) buf.append(val.toString());
00155             }
00156             buf.append(']');
00157             return buf.toString();
00158         }
00159         finally {
00160             StoredIterator.close(i);
00161         }
00162     }
00163 
00164     Object makeIteratorData(StoredIterator iterator, DataCursor cursor)
00165         throws DatabaseException {
00166 
00167         return new StoredMapEntry(cursor.getCurrentKey(),
00168                                   cursor.getCurrentValue(),
00169                                   this, iterator);
00170     }
00171 
00172     boolean hasValues() {
00173 
00174         return true;
00175     }
00176 }

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