001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2000,2008 Oracle. All rights reserved.
005: *
006: * $Id: StoredSortedEntrySet.java,v 1.24.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.Map;
013: import java.util.SortedSet;
014:
015: /**
016: * The SortedSet returned by Map.entrySet(). This class may not be
017: * instantiated directly. Contrary to what is stated by {@link Map#entrySet}
018: * this class does support the {@link #add} and {@link #addAll} methods.
019: *
020: * <p>The {@link java.util.Map.Entry#setValue} method of the Map.Entry objects
021: * that are returned by this class and its iterators behaves just as the {@link
022: * StoredIterator#set} method does.</p>
023: *
024: * <p>In addition to the standard SortedSet methods, this class provides the
025: * following methods for stored sorted sets only. Note that the use of these
026: * methods is not compatible with the standard Java collections interface.</p>
027: * <ul>
028: * <li>{@link #headSet(Object, boolean)}</li>
029: * <li>{@link #tailSet(Object, boolean)}</li>
030: * <li>{@link #subSet(Object, boolean, Object, boolean)}</li>
031: * </ul>
032: *
033: * @author Mark Hayes
034: */
035: public class StoredSortedEntrySet extends StoredEntrySet implements
036: SortedSet {
037:
038: StoredSortedEntrySet(DataView mapView) {
039:
040: super (mapView);
041: }
042:
043: /**
044: * Returns null since comparators are not supported. The natural ordering
045: * of a stored collection is data byte order, whether the data classes
046: * implement the {@link java.lang.Comparable} interface or not.
047: * This method does not conform to the {@link SortedSet#comparator}
048: * interface.
049: *
050: * @return null.
051: */
052: public Comparator comparator() {
053:
054: return null;
055: }
056:
057: /**
058: * Returns the first (lowest) element currently in this sorted set.
059: * This method conforms to the {@link SortedSet#first} interface.
060: *
061: * @return the first element.
062: *
063: * @throws RuntimeExceptionWrapper if a {@link
064: * com.sleepycat.je.DatabaseException} is thrown.
065: */
066: public Object first() {
067:
068: return getFirstOrLast(true);
069: }
070:
071: /**
072: * Returns the last (highest) element currently in this sorted set.
073: * This method conforms to the {@link SortedSet#last} interface.
074: *
075: * @return the last element.
076: *
077: * @throws RuntimeExceptionWrapper if a {@link
078: * com.sleepycat.je.DatabaseException} is thrown.
079: */
080: public Object last() {
081:
082: return getFirstOrLast(false);
083: }
084:
085: /**
086: * Returns a view of the portion of this sorted set whose elements are
087: * strictly less than toMapEntry.
088: * This method conforms to the {@link SortedSet#headSet} interface.
089: *
090: * <p>Note that the return value is a StoredCollection and must be treated
091: * as such; for example, its iterators must be explicitly closed.</p>
092: *
093: * @param toMapEntry the upper bound.
094: *
095: * @return the subset.
096: *
097: * @throws RuntimeExceptionWrapper if a {@link
098: * com.sleepycat.je.DatabaseException} is thrown.
099: */
100: public SortedSet headSet(Object toMapEntry) {
101:
102: return subSet(null, false, toMapEntry, false);
103: }
104:
105: /**
106: * Returns a view of the portion of this sorted set whose elements are
107: * strictly less than toMapEntry, optionally including toMapEntry.
108: * This method does not exist in the standard {@link SortedSet} interface.
109: *
110: * <p>Note that the return value is a StoredCollection and must be treated
111: * as such; for example, its iterators must be explicitly closed.</p>
112: *
113: * @param toMapEntry is the upper bound.
114: *
115: * @param toInclusive is true to include toMapEntry.
116: *
117: * @return the subset.
118: *
119: * @throws RuntimeExceptionWrapper if a {@link
120: * com.sleepycat.je.DatabaseException} is thrown.
121: */
122: public SortedSet headSet(Object toMapEntry, boolean toInclusive) {
123:
124: return subSet(null, false, toMapEntry, toInclusive);
125: }
126:
127: /**
128: * Returns a view of the portion of this sorted set whose elements are
129: * greater than or equal to fromMapEntry.
130: * This method conforms to the {@link SortedSet#tailSet} interface.
131: *
132: * <p>Note that the return value is a StoredCollection and must be treated
133: * as such; for example, its iterators must be explicitly closed.</p>
134: *
135: * @param fromMapEntry is the lower bound.
136: *
137: * @return the subset.
138: *
139: * @throws RuntimeExceptionWrapper if a {@link
140: * com.sleepycat.je.DatabaseException} is thrown.
141: */
142: public SortedSet tailSet(Object fromMapEntry) {
143:
144: return subSet(fromMapEntry, true, null, false);
145: }
146:
147: /**
148: * Returns a view of the portion of this sorted set whose elements are
149: * strictly greater than fromMapEntry, optionally including fromMapEntry.
150: * This method does not exist in the standard {@link SortedSet} interface.
151: *
152: * <p>Note that the return value is a StoredCollection and must be treated
153: * as such; for example, its iterators must be explicitly closed.</p>
154: *
155: * @param fromMapEntry is the lower bound.
156: *
157: * @param fromInclusive is true to include fromMapEntry.
158: *
159: * @return the subset.
160: *
161: * @throws RuntimeExceptionWrapper if a {@link
162: * com.sleepycat.je.DatabaseException} is thrown.
163: */
164: public SortedSet tailSet(Object fromMapEntry, boolean fromInclusive) {
165:
166: return subSet(fromMapEntry, fromInclusive, null, false);
167: }
168:
169: /**
170: * Returns a view of the portion of this sorted set whose elements range
171: * from fromMapEntry, inclusive, to toMapEntry, exclusive.
172: * This method conforms to the {@link SortedSet#subSet} interface.
173: *
174: * <p>Note that the return value is a StoredCollection and must be treated
175: * as such; for example, its iterators must be explicitly closed.</p>
176: *
177: * @param fromMapEntry is the lower bound.
178: *
179: * @param toMapEntry is the upper bound.
180: *
181: * @return the subset.
182: *
183: * @throws RuntimeExceptionWrapper if a {@link
184: * com.sleepycat.je.DatabaseException} is thrown.
185: */
186: public SortedSet subSet(Object fromMapEntry, Object toMapEntry) {
187:
188: return subSet(fromMapEntry, true, toMapEntry, false);
189: }
190:
191: /**
192: * Returns a view of the portion of this sorted set whose elements are
193: * strictly greater than fromMapEntry and strictly less than toMapEntry,
194: * optionally including fromMapEntry and toMapEntry.
195: * This method does not exist in the standard {@link SortedSet} interface.
196: *
197: * <p>Note that the return value is a StoredCollection and must be treated
198: * as such; for example, its iterators must be explicitly closed.</p>
199: *
200: * @param fromMapEntry is the lower bound.
201: *
202: * @param fromInclusive is true to include fromMapEntry.
203: *
204: * @param toMapEntry is the upper bound.
205: *
206: * @param toInclusive is true to include toMapEntry.
207: *
208: * @return the subset.
209: *
210: * @throws RuntimeExceptionWrapper if a {@link
211: * com.sleepycat.je.DatabaseException} is thrown.
212: */
213: public SortedSet subSet(Object fromMapEntry, boolean fromInclusive,
214: Object toMapEntry, boolean toInclusive) {
215:
216: Object fromKey = (fromMapEntry != null) ? ((Map.Entry) fromMapEntry)
217: .getKey()
218: : null;
219: Object toKey = (toMapEntry != null) ? ((Map.Entry) toMapEntry)
220: .getKey() : null;
221: try {
222: return new StoredSortedEntrySet(view.subView(fromKey,
223: fromInclusive, toKey, toInclusive, null));
224: } catch (Exception e) {
225: throw StoredContainer.convertException(e);
226: }
227: }
228: }
|