KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > SortedSet


1 /*
2  * @(#)SortedSet.java 1.24 04/06/28
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.util;
9
10 /**
11  * A set that further guarantees that its iterator will traverse the set in
12  * ascending element order, sorted according to the <i>natural ordering</i> of
13  * its elements (see Comparable), or by a Comparator provided at sorted set
14  * creation time. Several additional operations are provided to take
15  * advantage of the ordering. (This interface is the set analogue of
16  * SortedMap.)<p>
17  *
18  * All elements inserted into an sorted set must implement the Comparable
19  * interface (or be accepted by the specified Comparator). Furthermore, all
20  * such elements must be <i>mutually comparable</i>: <tt>e1.compareTo(e2)</tt>
21  * (or <tt>comparator.compare(e1, e2)</tt>) must not throw a
22  * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and <tt>e2</tt> in
23  * the sorted set. Attempts to violate this restriction will cause the
24  * offending method or constructor invocation to throw a
25  * <tt>ClassCastException</tt>.<p>
26  *
27  * Note that the ordering maintained by a sorted set (whether or not an
28  * explicit comparator is provided) must be <i>consistent with equals</i> if
29  * the sorted set is to correctly implement the <tt>Set</tt> interface. (See
30  * the <tt>Comparable</tt> interface or <tt>Comparator</tt> interface for a
31  * precise definition of <i>consistent with equals</i>.) This is so because
32  * the <tt>Set</tt> interface is defined in terms of the <tt>equals</tt>
33  * operation, but a sorted set performs all element comparisons using its
34  * <tt>compareTo</tt> (or <tt>compare</tt>) method, so two elements that are
35  * deemed equal by this method are, from the standpoint of the sorted set,
36  * equal. The behavior of a sorted set <i>is</i> well-defined even if its
37  * ordering is inconsistent with equals; it just fails to obey the general
38  * contract of the <tt>Set</tt> interface.<p>
39  *
40  * All general-purpose sorted set implementation classes should provide four
41  * "standard" constructors: 1) A void (no arguments) constructor, which
42  * creates an empty sorted set sorted according to the <i>natural order</i> of
43  * its elements. 2) A constructor with a single argument of type
44  * <tt>Comparator</tt>, which creates an empty sorted set sorted according to
45  * the specified comparator. 3) A constructor with a single argument of type
46  * <tt>Collection</tt>, which creates a new sorted set with the same elements
47  * as its argument, sorted according to the elements' natural ordering. 4) A
48  * constructor with a single argument of type <tt>SortedSet</tt>, which
49  * creates a new sorted set with the same elements and the same ordering as
50  * the input sorted set. There is no way to enforce this recommendation (as
51  * interfaces cannot contain constructors) but the JDK implementation (the
52  * <tt>TreeSet</tt> class) complies.<p>
53  *
54  * This interface is a member of the
55  * <a HREF="{@docRoot}/../guide/collections/index.html">
56  * Java Collections Framework</a>.
57  *
58  * @author Josh Bloch
59  * @version 1.24, 06/28/04
60  * @see Set
61  * @see TreeSet
62  * @see SortedMap
63  * @see Collection
64  * @see Comparable
65  * @see Comparator
66  * @see java.lang.ClassCastException
67  * @since 1.2
68  */

69
70 public interface SortedSet<E> extends Set JavaDoc<E> {
71     /**
72      * Returns the comparator associated with this sorted set, or
73      * <tt>null</tt> if it uses its elements' natural ordering.
74      *
75      * @return the comparator associated with this sorted set, or
76      * <tt>null</tt> if it uses its elements' natural ordering.
77      */

78     Comparator JavaDoc<? super E> comparator();
79
80     /**
81      * Returns a view of the portion of this sorted set whose elements range
82      * from <tt>fromElement</tt>, inclusive, to <tt>toElement</tt>, exclusive.
83      * (If <tt>fromElement</tt> and <tt>toElement</tt> are equal, the returned
84      * sorted set is empty.) The returned sorted set is backed by this sorted
85      * set, so changes in the returned sorted set are reflected in this sorted
86      * set, and vice-versa. The returned sorted set supports all optional set
87      * operations that this sorted set supports.<p>
88      *
89      * The sorted set returned by this method will throw an
90      * <tt>IllegalArgumentException</tt> if the user attempts to insert a
91      * element outside the specified range.<p>
92      *
93      * Note: this method always returns a <i>half-open range</i> (which
94      * includes its low endpoint but not its high endpoint). If you need a
95      * <i>closed range</i> (which includes both endpoints), and the element
96      * type allows for calculation of the successor a given value, merely
97      * request the subrange from <tt>lowEndpoint</tt> to
98      * <tt>successor(highEndpoint)</tt>. For example, suppose that <tt>s</tt>
99      * is a sorted set of strings. The following idiom obtains a view
100      * containing all of the strings in <tt>s</tt> from <tt>low</tt> to
101      * <tt>high</tt>, inclusive: <pre>
102      * SortedSet sub = s.subSet(low, high+"\0");
103      * </pre>
104      *
105      * A similar technique can be used to generate an <i>open range</i> (which
106      * contains neither endpoint). The following idiom obtains a view
107      * containing all of the Strings in <tt>s</tt> from <tt>low</tt> to
108      * <tt>high</tt>, exclusive: <pre>
109      * SortedSet sub = s.subSet(low+"\0", high);
110      * </pre>
111      *
112      * @param fromElement low endpoint (inclusive) of the subSet.
113      * @param toElement high endpoint (exclusive) of the subSet.
114      * @return a view of the specified range within this sorted set.
115      *
116      * @throws ClassCastException if <tt>fromElement</tt> and
117      * <tt>toElement</tt> cannot be compared to one another using this
118      * set's comparator (or, if the set has no comparator, using
119      * natural ordering). Implementations may, but are not required
120      * to, throw this exception if <tt>fromElement</tt> or
121      * <tt>toElement</tt> cannot be compared to elements currently in
122      * the set.
123      * @throws IllegalArgumentException if <tt>fromElement</tt> is greater than
124      * <tt>toElement</tt>; or if this set is itself a subSet, headSet,
125      * or tailSet, and <tt>fromElement</tt> or <tt>toElement</tt> are
126      * not within the specified range of the subSet, headSet, or
127      * tailSet.
128      * @throws NullPointerException if <tt>fromElement</tt> or
129      * <tt>toElement</tt> is <tt>null</tt> and this sorted set does
130      * not tolerate <tt>null</tt> elements.
131      */

132     SortedSet JavaDoc<E> subSet(E fromElement, E toElement);
133
134     /**
135      * Returns a view of the portion of this sorted set whose elements are
136      * strictly less than <tt>toElement</tt>. The returned sorted set is
137      * backed by this sorted set, so changes in the returned sorted set are
138      * reflected in this sorted set, and vice-versa. The returned sorted set
139      * supports all optional set operations.<p>
140      *
141      * The sorted set returned by this method will throw an
142      * <tt>IllegalArgumentException</tt> if the user attempts to insert a
143      * element outside the specified range.<p>
144      *
145      * Note: this method always returns a view that does not contain its
146      * (high) endpoint. If you need a view that does contain this endpoint,
147      * and the element type allows for calculation of the successor a given
148      * value, merely request a headSet bounded by
149      * <tt>successor(highEndpoint)</tt>. For example, suppose that <tt>s</tt>
150      * is a sorted set of strings. The following idiom obtains a view
151      * containing all of the strings in <tt>s</tt> that are less than or equal
152      * to <tt>high</tt>:
153      * <pre> SortedSet head = s.headSet(high+"\0");</pre>
154      *
155      * @param toElement high endpoint (exclusive) of the headSet.
156      * @return a view of the specified initial range of this sorted set.
157      * @throws ClassCastException if <tt>toElement</tt> is not compatible
158      * with this set's comparator (or, if the set has no comparator,
159      * if <tt>toElement</tt> does not implement <tt>Comparable</tt>).
160      * Implementations may, but are not required to, throw this
161      * exception if <tt>toElement</tt> cannot be compared to elements
162      * currently in the set.
163      * @throws NullPointerException if <tt>toElement</tt> is <tt>null</tt> and
164      * this sorted set does not tolerate <tt>null</tt> elements.
165      * @throws IllegalArgumentException if this set is itself a subSet,
166      * headSet, or tailSet, and <tt>toElement</tt> is not within the
167      * specified range of the subSet, headSet, or tailSet.
168      */

169     SortedSet JavaDoc<E> headSet(E toElement);
170
171     /**
172      * Returns a view of the portion of this sorted set whose elements are
173      * greater than or equal to <tt>fromElement</tt>. The returned sorted set
174      * is backed by this sorted set, so changes in the returned sorted set are
175      * reflected in this sorted set, and vice-versa. The returned sorted set
176      * supports all optional set operations.<p>
177      *
178      * The sorted set returned by this method will throw an
179      * <tt>IllegalArgumentException</tt> if the user attempts to insert a
180      * element outside the specified range.<p>
181      *
182      * Note: this method always returns a view that contains its (low)
183      * endpoint. If you need a view that does not contain this endpoint, and
184      * the element type allows for calculation of the successor a given value,
185      * merely request a tailSet bounded by <tt>successor(lowEndpoint)</tt>.
186      * For example, suppose that <tt>s</tt> is a sorted set of strings. The
187      * following idiom obtains a view containing all of the strings in
188      * <tt>s</tt> that are strictly greater than <tt>low</tt>:
189      *
190      * <pre> SortedSet tail = s.tailSet(low+"\0");</pre>
191      *
192      * @param fromElement low endpoint (inclusive) of the tailSet.
193      * @return a view of the specified final range of this sorted set.
194      * @throws ClassCastException if <tt>fromElement</tt> is not compatible
195      * with this set's comparator (or, if the set has no comparator,
196      * if <tt>fromElement</tt> does not implement <tt>Comparable</tt>).
197      * Implementations may, but are not required to, throw this
198      * exception if <tt>fromElement</tt> cannot be compared to elements
199      * currently in the set.
200      * @throws NullPointerException if <tt>fromElement</tt> is <tt>null</tt>
201      * and this sorted set does not tolerate <tt>null</tt> elements.
202      * @throws IllegalArgumentException if this set is itself a subSet,
203      * headSet, or tailSet, and <tt>fromElement</tt> is not within the
204      * specified range of the subSet, headSet, or tailSet.
205      */

206     SortedSet JavaDoc<E> tailSet(E fromElement);
207
208     /**
209      * Returns the first (lowest) element currently in this sorted set.
210      *
211      * @return the first (lowest) element currently in this sorted set.
212      * @throws NoSuchElementException sorted set is empty.
213      */

214     E first();
215
216     /**
217      * Returns the last (highest) element currently in this sorted set.
218      *
219      * @return the last (highest) element currently in this sorted set.
220      * @throws NoSuchElementException sorted set is empty.
221      */

222     E last();
223 }
224
Popular Tags