001: /*
002: * @(#)SortedSet.java 1.24 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.util;
029:
030: /**
031: * A set that further guarantees that its iterator will traverse the set in
032: * ascending element order, sorted according to the <i>natural ordering</i> of
033: * its elements (see Comparable), or by a Comparator provided at sorted set
034: * creation time. Several additional operations are provided to take
035: * advantage of the ordering. (This interface is the set analogue of
036: * SortedMap.)<p>
037: *
038: * All elements inserted into an sorted set must implement the Comparable
039: * interface (or be accepted by the specified Comparator). Furthermore, all
040: * such elements must be <i>mutually comparable</i>: <tt>e1.compareTo(e2)</tt>
041: * (or <tt>comparator.compare(e1, e2)</tt>) must not throw a
042: * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and <tt>e2</tt> in
043: * the sorted set. Attempts to violate this restriction will cause the
044: * offending method or constructor invocation to throw a
045: * <tt>ClassCastException</tt>.<p>
046: *
047: * Note that the ordering maintained by a sorted set (whether or not an
048: * explicit comparator is provided) must be <i>consistent with equals</i> if
049: * the sorted set is to correctly implement the <tt>Set</tt> interface. (See
050: * the <tt>Comparable</tt> interface or <tt>Comparator</tt> interface for a
051: * precise definition of <i>consistent with equals</i>.) This is so because
052: * the <tt>Set</tt> interface is defined in terms of the <tt>equals</tt>
053: * operation, but a sorted set performs all element comparisons using its
054: * <tt>compareTo</tt> (or <tt>compare</tt>) method, so two elements that are
055: * deemed equal by this method are, from the standpoint of the sorted set,
056: * equal. The behavior of a sorted set <i>is</i> well-defined even if its
057: * ordering is inconsistent with equals; it just fails to obey the general
058: * contract of the <tt>Set</tt> interface.<p>
059: *
060: * All general-purpose sorted set implementation classes should provide four
061: * "standard" constructors: 1) A void (no arguments) constructor, which
062: * creates an empty sorted set sorted according to the <i>natural order</i> of
063: * its elements. 2) A constructor with a single argument of type
064: * <tt>Comparator</tt>, which creates an empty sorted set sorted according to
065: * the specified comparator. 3) A constructor with a single argument of type
066: * <tt>Collection</tt>, which creates a new sorted set with the same elements
067: * as its argument, sorted according to the elements' natural ordering. 4) A
068: * constructor with a single argument of type <tt>SortedSet</tt>, which
069: * creates a new sorted set with the same elements and the same ordering as
070: * the input sorted set. There is no way to enforce this recommendation (as
071: * interfaces cannot contain constructors) but the SDK implementation (the
072: * <tt>TreeSet</tt> class) complies.<p>
073: *
074: * This interface is a member of the
075: * <a href="{@docRoot}/../guide/collections/index.html">
076: * Java Collections Framework</a>.
077: *
078: * @author Josh Bloch
079: * @version 1.15, 02/02/00
080: * @see Set
081: * @see TreeSet
082: * @see SortedMap
083: * @see Collection
084: * @see Comparable
085: * @see Comparator
086: * @see java.lang.ClassCastException
087: * @since 1.2
088: */
089:
090: public interface SortedSet extends Set {
091: /**
092: * Returns the comparator associated with this sorted set, or
093: * <tt>null</tt> if it uses its elements' natural ordering.
094: *
095: * @return the comparator associated with this sorted set, or
096: * <tt>null</tt> if it uses its elements' natural ordering.
097: */
098: Comparator comparator();
099:
100: /**
101: * Returns a view of the portion of this sorted set whose elements range
102: * from <tt>fromElement</tt>, inclusive, to <tt>toElement</tt>, exclusive.
103: * (If <tt>fromElement</tt> and <tt>toElement</tt> are equal, the returned
104: * sorted set is empty.) The returned sorted set is backed by this sorted
105: * set, so changes in the returned sorted set are reflected in this sorted
106: * set, and vice-versa. The returned sorted set supports all optional set
107: * operations that this sorted set supports.<p>
108: *
109: * The sorted set returned by this method will throw an
110: * <tt>IllegalArgumentException</tt> if the user attempts to insert a
111: * element outside the specified range.<p>
112: *
113: * Note: this method always returns a <i>half-open range</i> (which
114: * includes its low endpoint but not its high endpoint). If you need a
115: * <i>closed range</i> (which includes both endpoints), and the element
116: * type allows for calculation of the successor a given value, merely
117: * request the subrange from <tt>lowEndpoint</tt> to
118: * <tt>successor(highEndpoint)</tt>. For example, suppose that <tt>s</tt>
119: * is a sorted set of strings. The following idiom obtains a view
120: * containing all of the strings in <tt>s</tt> from <tt>low</tt> to
121: * <tt>high</tt>, inclusive: <pre>
122: * SortedSet sub = s.subSet(low, high+"\0");
123: * </pre>
124: *
125: * A similar technique can be used to generate an <i>open range</i> (which
126: * contains neither endpoint). The following idiom obtains a view
127: * containing all of the Strings in <tt>s</tt> from <tt>low</tt> to
128: * <tt>high</tt>, exclusive: <pre>
129: * SortedSet sub = s.subSet(low+"\0", high);
130: * </pre>
131: *
132: * @param fromElement low endpoint (inclusive) of the subSet.
133: * @param toElement high endpoint (exclusive) of the subSet.
134: * @return a view of the specified range within this sorted set.
135: *
136: * @throws ClassCastException if <tt>fromElement</tt> and
137: * <tt>toElement</tt> cannot be compared to one another using this
138: * set's comparator (or, if the set has no comparator, using
139: * natural ordering). Implementations may, but are not required
140: * to, throw this exception if <tt>fromElement</tt> or
141: * <tt>toElement</tt> cannot be compared to elements currently in
142: * the set.
143: * @throws IllegalArgumentException if <tt>fromElement</tt> is greater than
144: * <tt>toElement</tt>; or if this set is itself a subSet, headSet,
145: * or tailSet, and <tt>fromElement</tt> or <tt>toElement</tt> are
146: * not within the specified range of the subSet, headSet, or
147: * tailSet.
148: * @throws NullPointerException if <tt>fromElement</tt> or
149: * <tt>toElement</tt> is <tt>null</tt> and this sorted set does
150: * not tolerate <tt>null</tt> elements.
151: */
152: SortedSet subSet(Object fromElement, Object toElement);
153:
154: /**
155: * Returns a view of the portion of this sorted set whose elements are
156: * strictly less than <tt>toElement</tt>. The returned sorted set is
157: * backed by this sorted set, so changes in the returned sorted set are
158: * reflected in this sorted set, and vice-versa. The returned sorted set
159: * supports all optional set operations.<p>
160: *
161: * The sorted set returned by this method will throw an
162: * <tt>IllegalArgumentException</tt> if the user attempts to insert a
163: * element outside the specified range.<p>
164: *
165: * Note: this method always returns a view that does not contain its
166: * (high) endpoint. If you need a view that does contain this endpoint,
167: * and the element type allows for calculation of the successor a given
168: * value, merely request a headSet bounded by
169: * <tt>successor(highEndpoint)</tt>. For example, suppose that <tt>s</tt>
170: * is a sorted set of strings. The following idiom obtains a view
171: * containing all of the strings in <tt>s</tt> that are less than or equal
172: * to <tt>high</tt>:
173: * <pre> SortedSet head = s.headSet(high+"\0");</pre>
174: *
175: * @param toElement high endpoint (exclusive) of the headSet.
176: * @return a view of the specified initial range of this sorted set.
177: * @throws ClassCastException if <tt>toElement</tt> is not compatible
178: * with this set's comparator (or, if the set has no comparator,
179: * if <tt>toElement</tt> does not implement <tt>Comparable</tt>).
180: * Implementations may, but are not required to, throw this
181: * exception if <tt>toElement</tt> cannot be compared to elements
182: * currently in the set.
183: * @throws NullPointerException if <tt>toElement</tt> is <tt>null</tt> and
184: * this sorted set does not tolerate <tt>null</tt> elements.
185: * @throws IllegalArgumentException if this set is itself a subSet,
186: * headSet, or tailSet, and <tt>toElement</tt> is not within the
187: * specified range of the subSet, headSet, or tailSet.
188: */
189: SortedSet headSet(Object toElement);
190:
191: /**
192: * Returns a view of the portion of this sorted set whose elements are
193: * greater than or equal to <tt>fromElement</tt>. The returned sorted set
194: * is backed by this sorted set, so changes in the returned sorted set are
195: * reflected in this sorted set, and vice-versa. The returned sorted set
196: * supports all optional set operations.<p>
197: *
198: * The sorted set returned by this method will throw an
199: * <tt>IllegalArgumentException</tt> if the user attempts to insert a
200: * element outside the specified range.<p>
201: *
202: * Note: this method always returns a view that contains its (low)
203: * endpoint. If you need a view that does not contain this endpoint, and
204: * the element type allows for calculation of the successor a given value,
205: * merely request a tailSet bounded by <tt>successor(lowEndpoint)</tt>.
206: * For example, suppose that <tt>s</tt> is a sorted set of strings. The
207: * following idiom obtains a view containing all of the strings in
208: * <tt>s</tt> that are strictly greater than <tt>low</tt>:
209: *
210: * <pre> SortedSet tail = s.tailSet(low+"\0");</pre>
211: *
212: * @param fromElement low endpoint (inclusive) of the tailSet.
213: * @return a view of the specified final range of this sorted set.
214: * @throws ClassCastException if <tt>fromElement</tt> is not compatible
215: * with this set's comparator (or, if the set has no comparator,
216: * if <tt>fromElement</tt> does not implement <tt>Comparable</tt>).
217: * Implementations may, but are not required to, throw this
218: * exception if <tt>fromElement</tt> cannot be compared to elements
219: * currently in the set.
220: * @throws NullPointerException if <tt>fromElement</tt> is <tt>null</tt>
221: * and this sorted set does not tolerate <tt>null</tt> elements.
222: * @throws IllegalArgumentException if this set is itself a subSet,
223: * headSet, or tailSet, and <tt>fromElement</tt> is not within the
224: * specified range of the subSet, headSet, or tailSet.
225: */
226: SortedSet tailSet(Object fromElement);
227:
228: /**
229: * Returns the first (lowest) element currently in this sorted set.
230: *
231: * @return the first (lowest) element currently in this sorted set.
232: * @throws NoSuchElementException sorted set is empty.
233: */
234: Object first();
235:
236: /**
237: * Returns the last (highest) element currently in this sorted set.
238: *
239: * @return the last (highest) element currently in this sorted set.
240: * @throws NoSuchElementException sorted set is empty.
241: */
242: Object last();
243: }
|