001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2000,2008 Oracle. All rights reserved.
005: *
006: * $Id: StoredSortedKeySet.java,v 1.27.2.2 2008/01/07 15:14:06 cwl Exp $
007: */
008:
009: package com.sleepycat.collections;
010:
011: import java.util.Comparator;
012: import java.util.SortedSet;
013:
014: import com.sleepycat.bind.EntryBinding;
015: import com.sleepycat.je.Database;
016:
017: /**
018: * The SortedSet returned by Map.keySet() and which can also be constructed
019: * directly if a Map is not needed.
020: * Since this collection is a set it only contains one element for each key,
021: * even when duplicates are allowed. Key set iterators are therefore
022: * particularly useful for enumerating the unique keys of a store or index that
023: * allows duplicates.
024: *
025: * <p>In addition to the standard SortedSet methods, this class provides the
026: * following methods for stored sorted sets only. Note that the use of these
027: * methods is not compatible with the standard Java collections interface.</p>
028: * <ul>
029: * <li>{@link #headSet(Object, boolean)}</li>
030: * <li>{@link #tailSet(Object, boolean)}</li>
031: * <li>{@link #subSet(Object, boolean, Object, boolean)}</li>
032: * </ul>
033: *
034: * @author Mark Hayes
035: */
036: public class StoredSortedKeySet extends StoredKeySet implements
037: SortedSet {
038:
039: /**
040: * Creates a sorted key set view of a {@link Database}.
041: *
042: * @param database is the Database underlying the new collection.
043: *
044: * @param keyBinding is the binding used to translate between key buffers
045: * and key objects.
046: *
047: * @param writeAllowed is true to create a read-write collection or false
048: * to create a read-only collection.
049: *
050: * @throws IllegalArgumentException if formats are not consistently
051: * defined or a parameter is invalid.
052: *
053: * @throws RuntimeExceptionWrapper if a {@link
054: * com.sleepycat.je.DatabaseException} is thrown.
055: */
056: public StoredSortedKeySet(Database database,
057: EntryBinding keyBinding, boolean writeAllowed) {
058:
059: super (new DataView(database, keyBinding, null, null,
060: writeAllowed, null));
061: }
062:
063: StoredSortedKeySet(DataView keySetView) {
064:
065: super (keySetView);
066: }
067:
068: /**
069: * Returns null since comparators are not supported. The natural ordering
070: * of a stored collection is data byte order, whether the data classes
071: * implement the {@link java.lang.Comparable} interface or not.
072: * This method does not conform to the {@link SortedSet#comparator}
073: * interface.
074: *
075: * @return null.
076: */
077: public Comparator comparator() {
078:
079: return null;
080: }
081:
082: /**
083: * Returns the first (lowest) element currently in this sorted set.
084: * This method conforms to the {@link SortedSet#first} interface.
085: *
086: * @return the first element.
087: *
088: * @throws RuntimeExceptionWrapper if a {@link
089: * com.sleepycat.je.DatabaseException} is thrown.
090: */
091: public Object first() {
092:
093: return getFirstOrLast(true);
094: }
095:
096: /**
097: * Returns the last (highest) element currently in this sorted set.
098: * This method conforms to the {@link SortedSet#last} interface.
099: *
100: * @return the last element.
101: *
102: * @throws RuntimeExceptionWrapper if a {@link
103: * com.sleepycat.je.DatabaseException} is thrown.
104: */
105: public Object last() {
106:
107: return getFirstOrLast(false);
108: }
109:
110: /**
111: * Returns a view of the portion of this sorted set whose elements are
112: * strictly less than toKey.
113: * This method conforms to the {@link SortedSet#headSet} interface.
114: *
115: * <p>Note that the return value is a StoredCollection and must be treated
116: * as such; for example, its iterators must be explicitly closed.</p>
117: *
118: * @param toKey is the upper bound.
119: *
120: * @return the subset.
121: *
122: * @throws RuntimeExceptionWrapper if a {@link
123: * com.sleepycat.je.DatabaseException} is thrown.
124: */
125: public SortedSet headSet(Object toKey) {
126:
127: return subSet(null, false, toKey, false);
128: }
129:
130: /**
131: * Returns a view of the portion of this sorted set whose elements are
132: * strictly less than toKey, optionally including toKey.
133: * This method does not exist in the standard {@link SortedSet} interface.
134: *
135: * <p>Note that the return value is a StoredCollection and must be treated
136: * as such; for example, its iterators must be explicitly closed.</p>
137: *
138: * @param toKey is the upper bound.
139: *
140: * @param toInclusive is true to include toKey.
141: *
142: * @return the subset.
143: *
144: * @throws RuntimeExceptionWrapper if a {@link
145: * com.sleepycat.je.DatabaseException} is thrown.
146: */
147: public SortedSet headSet(Object toKey, boolean toInclusive) {
148:
149: return subSet(null, false, toKey, toInclusive);
150: }
151:
152: /**
153: * Returns a view of the portion of this sorted set whose elements are
154: * greater than or equal to fromKey.
155: * This method conforms to the {@link SortedSet#tailSet} interface.
156: *
157: * <p>Note that the return value is a StoredCollection and must be treated
158: * as such; for example, its iterators must be explicitly closed.</p>
159: *
160: * @param fromKey is the lower bound.
161: *
162: * @return the subset.
163: *
164: * @throws RuntimeExceptionWrapper if a {@link
165: * com.sleepycat.je.DatabaseException} is thrown.
166: */
167: public SortedSet tailSet(Object fromKey) {
168:
169: return subSet(fromKey, true, null, false);
170: }
171:
172: /**
173: * Returns a view of the portion of this sorted set whose elements are
174: * strictly greater than fromKey, optionally including fromKey.
175: * This method does not exist in the standard {@link SortedSet} interface.
176: *
177: * <p>Note that the return value is a StoredCollection and must be treated
178: * as such; for example, its iterators must be explicitly closed.</p>
179: *
180: * @param fromKey is the lower bound.
181: *
182: * @param fromInclusive is true to include fromKey.
183: *
184: * @return the subset.
185: *
186: * @throws RuntimeExceptionWrapper if a {@link
187: * com.sleepycat.je.DatabaseException} is thrown.
188: */
189: public SortedSet tailSet(Object fromKey, boolean fromInclusive) {
190:
191: return subSet(fromKey, fromInclusive, null, false);
192: }
193:
194: /**
195: * Returns a view of the portion of this sorted set whose elements range
196: * from fromKey, inclusive, to toKey, exclusive.
197: * This method conforms to the {@link SortedSet#subSet} interface.
198: *
199: * <p>Note that the return value is a StoredCollection and must be treated
200: * as such; for example, its iterators must be explicitly closed.</p>
201: *
202: * @param fromKey is the lower bound.
203: *
204: * @param toKey is the upper bound.
205: *
206: * @return the subset.
207: *
208: * @throws RuntimeExceptionWrapper if a {@link
209: * com.sleepycat.je.DatabaseException} is thrown.
210: */
211: public SortedSet subSet(Object fromKey, Object toKey) {
212:
213: return subSet(fromKey, true, toKey, false);
214: }
215:
216: /**
217: * Returns a view of the portion of this sorted set whose elements are
218: * strictly greater than fromKey and strictly less than toKey,
219: * optionally including fromKey and toKey.
220: * This method does not exist in the standard {@link SortedSet} interface.
221: *
222: * <p>Note that the return value is a StoredCollection and must be treated
223: * as such; for example, its iterators must be explicitly closed.</p>
224: *
225: * @param fromKey is the lower bound.
226: *
227: * @param fromInclusive is true to include fromKey.
228: *
229: * @param toKey is the upper bound.
230: *
231: * @param toInclusive is true to include toKey.
232: *
233: * @return the subset.
234: *
235: * @throws RuntimeExceptionWrapper if a {@link
236: * com.sleepycat.je.DatabaseException} is thrown.
237: */
238: public SortedSet subSet(Object fromKey, boolean fromInclusive,
239: Object toKey, boolean toInclusive) {
240:
241: try {
242: return new StoredSortedKeySet(view.subView(fromKey,
243: fromInclusive, toKey, toInclusive, null));
244: } catch (Exception e) {
245: throw StoredContainer.convertException(e);
246: }
247: }
248: }
|