1 8 9 package com.sleepycat.collections; 10 11 import java.util.Map ; 12 import java.util.Set ; 13 14 import com.sleepycat.je.DatabaseEntry; 15 import com.sleepycat.je.DatabaseException; 16 import com.sleepycat.je.OperationStatus; 17 import com.sleepycat.util.RuntimeExceptionWrapper; 18 19 30 public class StoredEntrySet extends StoredCollection implements Set { 31 32 StoredEntrySet(DataView mapView) { 33 34 super(mapView); 35 } 36 37 54 public boolean add(Object mapEntry) { 55 56 Map.Entry entry = (Map.Entry ) mapEntry; return add(entry.getKey(), entry.getValue()); 58 } 59 60 75 public boolean remove(Object mapEntry) { 76 77 if (!(mapEntry instanceof Map.Entry )) { 78 return false; 79 } 80 Map.Entry entry = (Map.Entry ) mapEntry; 81 DataCursor cursor = null; 82 boolean doAutoCommit = beginAutoCommit(); 83 try { 84 cursor = new DataCursor(view, true); 85 OperationStatus status = 86 cursor.findBoth(entry.getKey(), entry.getValue(), true); 87 if (status == OperationStatus.SUCCESS) { 88 cursor.delete(); 89 } 90 closeCursor(cursor); 91 commitAutoCommit(doAutoCommit); 92 return (status == OperationStatus.SUCCESS); 93 } catch (Exception e) { 94 closeCursor(cursor); 95 throw handleException(e, doAutoCommit); 96 } 97 } 98 99 111 public boolean contains(Object mapEntry) { 112 113 if (!(mapEntry instanceof Map.Entry )) { 114 return false; 115 } 116 Map.Entry entry = (Map.Entry ) mapEntry; 117 DataCursor cursor = null; 118 try { 119 cursor = new DataCursor(view, false); 120 OperationStatus status = 121 cursor.findBoth(entry.getKey(), entry.getValue(), false); 122 return (status == OperationStatus.SUCCESS); 123 } catch (Exception e) { 124 throw StoredContainer.convertException(e); 125 } finally { 126 closeCursor(cursor); 127 } 128 } 129 130 public String toString() { 132 StringBuffer buf = new StringBuffer (); 133 buf.append("["); 134 StoredIterator i = storedIterator(); 135 try { 136 while (i.hasNext()) { 137 Map.Entry entry = (Map.Entry ) i.next(); 138 if (buf.length() > 1) buf.append(','); 139 Object key = entry.getKey(); 140 Object val = entry.getValue(); 141 if (key != null) buf.append(key.toString()); 142 buf.append('='); 143 if (val != null) buf.append(val.toString()); 144 } 145 buf.append(']'); 146 return buf.toString(); 147 } 148 finally { 149 i.close(); 150 } 151 } 152 153 Object makeIteratorData(BaseIterator iterator, 154 DatabaseEntry keyEntry, 155 DatabaseEntry priKeyEntry, 156 DatabaseEntry valueEntry) { 157 158 return new StoredMapEntry(view.makeKey(keyEntry, priKeyEntry), 159 view.makeValue(priKeyEntry, valueEntry), 160 this, iterator); 161 } 162 163 boolean hasValues() { 164 165 return true; 166 } 167 } 168 | Popular Tags |