KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > collections > StoredSortedValueSet


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2000,2006 Oracle. All rights reserved.
5  *
6  * $Id: StoredSortedValueSet.java,v 1.29 2006/10/30 21:14:10 bostic Exp $
7  */

8
9 package com.sleepycat.collections;
10
11 import java.util.Comparator JavaDoc;
12 import java.util.SortedSet JavaDoc;
13
14 import com.sleepycat.bind.EntityBinding;
15 import com.sleepycat.je.Database;
16
17 /**
18  * The SortedSet returned by Map.values() and which can also be constructed
19  * directly if a Map is not needed.
20  * Although this collection is a set it may contain duplicate values. Only if
21  * an entity value binding is used are all elements guaranteed to be unique.
22  *
23  * <p>In addition to the standard SortedSet methods, this class provides the
24  * following methods for stored sorted value sets only. Note that the use of
25  * these methods is not compatible with the standard Java collections
26  * interface.</p>
27  * <ul>
28  * <li>{@link #headSet(Object, boolean)}</li>
29  * <li>{@link #tailSet(Object, boolean)}</li>
30  * <li>{@link #subSet(Object, boolean, Object, boolean)}</li>
31  * </ul>
32  *
33  * @author Mark Hayes
34  */

35 public class StoredSortedValueSet extends StoredValueSet implements SortedSet JavaDoc {
36
37     /*
38      * No valueBinding ctor is possible since key cannot be derived.
39      */

40
41     /**
42      * Creates a sorted value set entity view of a {@link Database}.
43      *
44      * @param database is the Database underlying the new collection.
45      *
46      * @param valueEntityBinding is the binding used to translate between
47      * key/value buffers and entity value objects.
48      *
49      * @param writeAllowed is true to create a read-write collection or false
50      * to create a read-only collection.
51      *
52      * @throws IllegalArgumentException if formats are not consistently
53      * defined or a parameter is invalid.
54      *
55      * @throws RuntimeExceptionWrapper if a {@link
56      * com.sleepycat.je.DatabaseException} is thrown.
57      */

58     public StoredSortedValueSet(Database database,
59                                 EntityBinding valueEntityBinding,
60                                 boolean writeAllowed) {
61
62         super(new DataView(database, null, null, valueEntityBinding,
63                            writeAllowed, null));
64         checkKeyDerivation();
65     }
66
67     StoredSortedValueSet(DataView valueSetView) {
68
69         super(valueSetView);
70         checkKeyDerivation();
71     }
72
73     private void checkKeyDerivation() {
74
75         if (!view.canDeriveKeyFromValue()) {
76             throw new IllegalArgumentException JavaDoc("Cannot derive key from value");
77         }
78     }
79
80     /**
81      * Returns null since comparators are not supported. The natural ordering
82      * of a stored collection is data byte order, whether the data classes
83      * implement the {@link java.lang.Comparable} interface or not.
84      * This method does not conform to the {@link SortedSet#comparator}
85      * interface.
86      *
87      * @return null.
88      */

89     public Comparator JavaDoc comparator() {
90
91         return null;
92     }
93
94     /**
95      * Returns the first (lowest) element currently in this sorted set.
96      * This method conforms to the {@link SortedSet#first} interface.
97      *
98      * @return the first element.
99      *
100      * @throws RuntimeExceptionWrapper if a {@link
101      * com.sleepycat.je.DatabaseException} is thrown.
102      */

103     public Object JavaDoc first() {
104
105         return getFirstOrLast(true);
106     }
107
108     /**
109      * Returns the last (highest) element currently in this sorted set.
110      * This method conforms to the {@link SortedSet#last} interface.
111      *
112      * @return the last element.
113      *
114      * @throws RuntimeExceptionWrapper if a {@link
115      * com.sleepycat.je.DatabaseException} is thrown.
116      */

117     public Object JavaDoc last() {
118
119         return getFirstOrLast(false);
120     }
121
122     /**
123      * Returns a view of the portion of this sorted set whose elements are
124      * strictly less than toValue.
125      * This method conforms to the {@link SortedSet#headSet} interface.
126      *
127      * <p>Note that the return value is a StoredCollection and must be treated
128      * as such; for example, its iterators must be explicitly closed.</p>
129      *
130      * @param toValue the upper bound.
131      *
132      * @return the subset.
133      *
134      * @throws RuntimeExceptionWrapper if a {@link
135      * com.sleepycat.je.DatabaseException} is thrown.
136      */

137     public SortedSet JavaDoc headSet(Object JavaDoc toValue) {
138
139         return subSet(null, false, toValue, false);
140     }
141
142     /**
143      * Returns a view of the portion of this sorted set whose elements are
144      * strictly less than toValue, optionally including toValue.
145      * This method does not exist in the standard {@link SortedSet} interface.
146      *
147      * <p>Note that the return value is a StoredCollection and must be treated
148      * as such; for example, its iterators must be explicitly closed.</p>
149      *
150      * @param toValue is the upper bound.
151      *
152      * @param toInclusive is true to include toValue.
153      *
154      * @return the subset.
155      *
156      * @throws RuntimeExceptionWrapper if a {@link
157      * com.sleepycat.je.DatabaseException} is thrown.
158      */

159     public SortedSet JavaDoc headSet(Object JavaDoc toValue, boolean toInclusive) {
160
161         return subSet(null, false, toValue, toInclusive);
162     }
163
164     /**
165      * Returns a view of the portion of this sorted set whose elements are
166      * greater than or equal to fromValue.
167      * This method conforms to the {@link SortedSet#tailSet} interface.
168      *
169      * <p>Note that the return value is a StoredCollection and must be treated
170      * as such; for example, its iterators must be explicitly closed.</p>
171      *
172      * @param fromValue is the lower bound.
173      *
174      * @return the subset.
175      *
176      * @throws RuntimeExceptionWrapper if a {@link
177      * com.sleepycat.je.DatabaseException} is thrown.
178      */

179     public SortedSet JavaDoc tailSet(Object JavaDoc fromValue) {
180
181         return subSet(fromValue, true, null, false);
182     }
183
184     /**
185      * Returns a view of the portion of this sorted set whose elements are
186      * strictly greater than fromValue, optionally including fromValue.
187      * This method does not exist in the standard {@link SortedSet} interface.
188      *
189      * <p>Note that the return value is a StoredCollection and must be treated
190      * as such; for example, its iterators must be explicitly closed.</p>
191      *
192      * @param fromValue is the lower bound.
193      *
194      * @param fromInclusive is true to include fromValue.
195      *
196      * @return the subset.
197      *
198      * @throws RuntimeExceptionWrapper if a {@link
199      * com.sleepycat.je.DatabaseException} is thrown.
200      */

201     public SortedSet JavaDoc tailSet(Object JavaDoc fromValue, boolean fromInclusive) {
202
203         return subSet(fromValue, fromInclusive, null, false);
204     }
205
206     /**
207      * Returns a view of the portion of this sorted set whose elements range
208      * from fromValue, inclusive, to toValue, exclusive.
209      * This method conforms to the {@link SortedSet#subSet} interface.
210      *
211      * <p>Note that the return value is a StoredCollection and must be treated
212      * as such; for example, its iterators must be explicitly closed.</p>
213      *
214      * @param fromValue is the lower bound.
215      *
216      * @param toValue is the upper bound.
217      *
218      * @return the subset.
219      *
220      * @throws RuntimeExceptionWrapper if a {@link
221      * com.sleepycat.je.DatabaseException} is thrown.
222      */

223     public SortedSet JavaDoc subSet(Object JavaDoc fromValue, Object JavaDoc toValue) {
224
225         return subSet(fromValue, true, toValue, false);
226     }
227
228     /**
229      * Returns a view of the portion of this sorted set whose elements are
230      * strictly greater than fromValue and strictly less than toValue,
231      * optionally including fromValue and toValue.
232      * This method does not exist in the standard {@link SortedSet} interface.
233      *
234      * <p>Note that the return value is a StoredCollection and must be treated
235      * as such; for example, its iterators must be explicitly closed.</p>
236      *
237      * @param fromValue is the lower bound.
238      *
239      * @param fromInclusive is true to include fromValue.
240      *
241      * @param toValue is the upper bound.
242      *
243      * @param toInclusive is true to include toValue.
244      *
245      * @return the subset.
246      *
247      * @throws RuntimeExceptionWrapper if a {@link
248      * com.sleepycat.je.DatabaseException} is thrown.
249      */

250     public SortedSet JavaDoc subSet(Object JavaDoc fromValue, boolean fromInclusive,
251                             Object JavaDoc toValue, boolean toInclusive) {
252
253         try {
254             return new StoredSortedValueSet(
255                view.subView(fromValue, fromInclusive, toValue, toInclusive,
256                             null));
257         } catch (Exception JavaDoc e) {
258             throw StoredContainer.convertException(e);
259         }
260     }
261 }
262
Popular Tags