001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2000,2008 Oracle. All rights reserved.
005: *
006: * $Id: StoredEntrySet.java,v 1.30.2.2 2008/01/07 15:14:06 cwl Exp $
007: */
008:
009: package com.sleepycat.collections;
010:
011: import java.util.Map;
012: import java.util.Set;
013:
014: import com.sleepycat.je.DatabaseEntry;
015: import com.sleepycat.je.DatabaseException;
016: import com.sleepycat.je.OperationStatus;
017: import com.sleepycat.util.RuntimeExceptionWrapper;
018:
019: /**
020: * The Set returned by Map.entrySet(). This class may not be instantiated
021: * directly. Contrary to what is stated by {@link Map#entrySet} this class
022: * does support the {@link #add} and {@link #addAll} methods.
023: *
024: * <p>The {@link java.util.Map.Entry#setValue} method of the Map.Entry objects
025: * that are returned by this class and its iterators behaves just as the {@link
026: * StoredIterator#set} method does.</p>
027: *
028: * @author Mark Hayes
029: */
030: public class StoredEntrySet extends StoredCollection implements Set {
031:
032: StoredEntrySet(DataView mapView) {
033:
034: super (mapView);
035: }
036:
037: /**
038: * Adds the specified element to this set if it is not already present
039: * (optional operation).
040: * This method conforms to the {@link Set#add} interface.
041: *
042: * @param mapEntry must be a {@link java.util.Map.Entry} instance.
043: *
044: * @return true if the key-value pair was added to the set (and was not
045: * previously present).
046: *
047: * @throws UnsupportedOperationException if the collection is read-only.
048: *
049: * @throws ClassCastException if the mapEntry is not a {@link
050: * java.util.Map.Entry} instance.
051: *
052: * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is thrown.
053: */
054: public boolean add(Object mapEntry) {
055:
056: Map.Entry entry = (Map.Entry) mapEntry; // allow ClassCastException
057: return add(entry.getKey(), entry.getValue());
058: }
059:
060: /**
061: * Removes the specified element from this set if it is present (optional
062: * operation).
063: * This method conforms to the {@link Set#remove} interface.
064: *
065: * @param mapEntry is a {@link java.util.Map.Entry} instance to be removed.
066: *
067: * @return true if the key-value pair was removed from the set, or false if
068: * the mapEntry is not a {@link java.util.Map.Entry} instance or is not
069: * present in the set.
070: *
071: * @throws UnsupportedOperationException if the collection is read-only.
072: *
073: * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is thrown.
074: */
075: public boolean remove(Object mapEntry) {
076:
077: if (!(mapEntry instanceof Map.Entry)) {
078: return false;
079: }
080: Map.Entry entry = (Map.Entry) mapEntry;
081: DataCursor cursor = null;
082: boolean doAutoCommit = beginAutoCommit();
083: try {
084: cursor = new DataCursor(view, true);
085: OperationStatus status = cursor.findBoth(entry.getKey(),
086: entry.getValue(), true);
087: if (status == OperationStatus.SUCCESS) {
088: cursor.delete();
089: }
090: closeCursor(cursor);
091: commitAutoCommit(doAutoCommit);
092: return (status == OperationStatus.SUCCESS);
093: } catch (Exception e) {
094: closeCursor(cursor);
095: throw handleException(e, doAutoCommit);
096: }
097: }
098:
099: /**
100: * Returns true if this set contains the specified element.
101: * This method conforms to the {@link Set#contains} interface.
102: *
103: * @param mapEntry is a {@link java.util.Map.Entry} instance to be checked.
104: *
105: * @return true if the key-value pair is present in the set, or false if
106: * the mapEntry is not a {@link java.util.Map.Entry} instance or is not
107: * present in the set.
108: *
109: * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is thrown.
110: */
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 = cursor.findBoth(entry.getKey(),
121: 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: // javadoc is inherited
131: 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)
139: buf.append(',');
140: Object key = entry.getKey();
141: Object val = entry.getValue();
142: if (key != null)
143: buf.append(key.toString());
144: buf.append('=');
145: if (val != null)
146: buf.append(val.toString());
147: }
148: buf.append(']');
149: return buf.toString();
150: } finally {
151: i.close();
152: }
153: }
154:
155: Object makeIteratorData(BaseIterator iterator,
156: DatabaseEntry keyEntry, DatabaseEntry priKeyEntry,
157: DatabaseEntry valueEntry) {
158:
159: return new StoredMapEntry(view.makeKey(keyEntry, priKeyEntry),
160: view.makeValue(priKeyEntry, valueEntry), this , iterator);
161: }
162:
163: boolean hasValues() {
164:
165: return true;
166: }
167: }
|