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