• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src/router/db-4.8.30/java/src/com/sleepycat/collections/
1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2000-2009 Oracle.  All rights reserved.
5 *
6 * $Id$
7 */
8
9package com.sleepycat.collections;
10
11import java.util.Map;
12import java.util.Set;
13
14import com.sleepycat.db.DatabaseEntry;
15import com.sleepycat.db.DatabaseException;
16import com.sleepycat.db.OperationStatus;
17import com.sleepycat.util.RuntimeExceptionWrapper;
18
19/**
20 * The Set returned by Map.entrySet().  This class may not be instantiated
21 * directly.  Contrary to what is stated by {@link Map#entrySet} this class
22 * does support the {@link #add} and {@link #addAll} methods.
23 *
24 * <p>The {@link java.util.Map.Entry#setValue} method of the Map.Entry objects
25 * that are returned by this class and its iterators behaves just as the {@link
26 * StoredIterator#set} method does.</p>
27 *
28 * @author Mark Hayes
29 */
30public class StoredEntrySet<K,V>
31    extends StoredCollection<Map.Entry<K,V>>
32    implements Set<Map.Entry<K,V>> {
33
34    StoredEntrySet(DataView mapView) {
35
36        super(mapView);
37    }
38
39    /**
40     * Adds the specified element to this set if it is not already present
41     * (optional operation).
42     * This method conforms to the {@link Set#add} interface.
43     *
44     * @param mapEntry must be a {@link java.util.Map.Entry} instance.
45     *
46     * @return true if the key-value pair was added to the set (and was not
47     * previously present).
48     *
49     * @throws UnsupportedOperationException if the collection is read-only.
50     *
51     * @throws ClassCastException if the mapEntry is not a {@link
52     * java.util.Map.Entry} instance.
53     *
54     * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is thrown.
55     */
56    public boolean add(Map.Entry<K,V> mapEntry) {
57
58        return add(mapEntry.getKey(), mapEntry.getValue());
59    }
60
61    /**
62     * Removes the specified element from this set if it is present (optional
63     * operation).
64     * This method conforms to the {@link Set#remove} interface.
65     *
66     * @param mapEntry is a {@link java.util.Map.Entry} instance to be removed.
67     *
68     * @return true if the key-value pair was removed from the set, or false if
69     * the mapEntry is not a {@link java.util.Map.Entry} instance or is not
70     * present in the set.
71     *
72     * @throws UnsupportedOperationException if the collection is read-only.
73     *
74     * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is thrown.
75     */
76    public boolean remove(Object mapEntry) {
77
78        if (!(mapEntry instanceof Map.Entry)) {
79            return false;
80        }
81        DataCursor cursor = null;
82        boolean doAutoCommit = beginAutoCommit();
83        try {
84            cursor = new DataCursor(view, true);
85            Map.Entry entry = (Map.Entry) mapEntry;
86            OperationStatus status =
87                cursor.findBoth(entry.getKey(), entry.getValue(), true);
88            if (status == OperationStatus.SUCCESS) {
89                cursor.delete();
90            }
91            closeCursor(cursor);
92            commitAutoCommit(doAutoCommit);
93            return (status == OperationStatus.SUCCESS);
94        } catch (Exception e) {
95            closeCursor(cursor);
96            throw handleException(e, doAutoCommit);
97        }
98    }
99
100    /**
101     * Returns true if this set contains the specified element.
102     * This method conforms to the {@link Set#contains} interface.
103     *
104     * @param mapEntry is a {@link java.util.Map.Entry} instance to be checked.
105     *
106     * @return true if the key-value pair is present in the set, or false if
107     * the mapEntry is not a {@link java.util.Map.Entry} instance or is not
108     * present in the set.
109     *
110     * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is thrown.
111     */
112    public boolean contains(Object mapEntry) {
113
114        if (!(mapEntry instanceof Map.Entry)) {
115            return false;
116        }
117        DataCursor cursor = null;
118        try {
119            cursor = new DataCursor(view, false);
120            Map.Entry entry = (Map.Entry) mapEntry;
121            OperationStatus status =
122                cursor.findBoth(entry.getKey(), entry.getValue(), false);
123            return (status == OperationStatus.SUCCESS);
124        } catch (Exception e) {
125            throw StoredContainer.convertException(e);
126        } finally {
127            closeCursor(cursor);
128        }
129    }
130
131    // javadoc is inherited
132    public String toString() {
133	StringBuffer buf = new StringBuffer();
134	buf.append("[");
135	StoredIterator i = storedIterator();
136        try {
137            while (i.hasNext()) {
138                Map.Entry entry = (Map.Entry) i.next();
139                if (buf.length() > 1) buf.append(',');
140                Object key = entry.getKey();
141                Object val = entry.getValue();
142                if (key != null) buf.append(key.toString());
143                buf.append('=');
144                if (val != null) buf.append(val.toString());
145            }
146            buf.append(']');
147            return buf.toString();
148        }
149        finally {
150            i.close();
151        }
152    }
153
154    Map.Entry<K,V> makeIteratorData(BaseIterator iterator,
155                                    DatabaseEntry keyEntry,
156                                    DatabaseEntry priKeyEntry,
157                                    DatabaseEntry valueEntry) {
158
159        return new StoredMapEntry(view.makeKey(keyEntry, priKeyEntry),
160                                  view.makeValue(priKeyEntry, valueEntry),
161                                  this, iterator);
162    }
163
164    boolean hasValues() {
165
166        return true;
167    }
168}
169