jaroslav@597: /* jaroslav@597: * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved. jaroslav@597: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@597: * jaroslav@597: * This code is free software; you can redistribute it and/or modify it jaroslav@597: * under the terms of the GNU General Public License version 2 only, as jaroslav@597: * published by the Free Software Foundation. Oracle designates this jaroslav@597: * particular file as subject to the "Classpath" exception as provided jaroslav@597: * by Oracle in the LICENSE file that accompanied this code. jaroslav@597: * jaroslav@597: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@597: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@597: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@597: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@597: * accompanied this code). jaroslav@597: * jaroslav@597: * You should have received a copy of the GNU General Public License version jaroslav@597: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@597: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@597: * jaroslav@597: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@597: * or visit www.oracle.com if you need additional information or have any jaroslav@597: * questions. jaroslav@597: */ jaroslav@597: jaroslav@597: package java.util; jaroslav@597: jaroslav@597: /** jaroslav@597: * A {@link Set} that further provides a total ordering on its elements. jaroslav@597: * The elements are ordered using their {@linkplain Comparable natural jaroslav@597: * ordering}, or by a {@link Comparator} typically provided at sorted jaroslav@597: * set creation time. The set's iterator will traverse the set in jaroslav@597: * ascending element order. Several additional operations are provided jaroslav@597: * to take advantage of the ordering. (This interface is the set jaroslav@597: * analogue of {@link SortedMap}.) jaroslav@597: * jaroslav@597: *

All elements inserted into a sorted set must implement the Comparable jaroslav@597: * interface (or be accepted by the specified comparator). Furthermore, all jaroslav@597: * such elements must be mutually comparable: e1.compareTo(e2) jaroslav@597: * (or comparator.compare(e1, e2)) must not throw a jaroslav@597: * ClassCastException for any elements e1 and e2 in jaroslav@597: * the sorted set. Attempts to violate this restriction will cause the jaroslav@597: * offending method or constructor invocation to throw a jaroslav@597: * ClassCastException. jaroslav@597: * jaroslav@597: *

Note that the ordering maintained by a sorted set (whether or not an jaroslav@597: * explicit comparator is provided) must be consistent with equals if jaroslav@597: * the sorted set is to correctly implement the Set interface. (See jaroslav@597: * the Comparable interface or Comparator interface for a jaroslav@597: * precise definition of consistent with equals.) This is so because jaroslav@597: * the Set interface is defined in terms of the equals jaroslav@597: * operation, but a sorted set performs all element comparisons using its jaroslav@597: * compareTo (or compare) method, so two elements that are jaroslav@597: * deemed equal by this method are, from the standpoint of the sorted set, jaroslav@597: * equal. The behavior of a sorted set is well-defined even if its jaroslav@597: * ordering is inconsistent with equals; it just fails to obey the general jaroslav@597: * contract of the Set interface. jaroslav@597: * jaroslav@597: *

All general-purpose sorted set implementation classes should jaroslav@597: * provide four "standard" constructors: 1) A void (no arguments) jaroslav@597: * constructor, which creates an empty sorted set sorted according to jaroslav@597: * the natural ordering of its elements. 2) A constructor with a jaroslav@597: * single argument of type Comparator, which creates an empty jaroslav@597: * sorted set sorted according to the specified comparator. 3) A jaroslav@597: * constructor with a single argument of type Collection, jaroslav@597: * which creates a new sorted set with the same elements as its jaroslav@597: * argument, sorted according to the natural ordering of the elements. jaroslav@597: * 4) A constructor with a single argument of type SortedSet, jaroslav@597: * which creates a new sorted set with the same elements and the same jaroslav@597: * ordering as the input sorted set. There is no way to enforce this jaroslav@597: * recommendation, as interfaces cannot contain constructors. jaroslav@597: * jaroslav@597: *

Note: several methods return subsets with restricted ranges. jaroslav@597: * Such ranges are half-open, that is, they include their low jaroslav@597: * endpoint but not their high endpoint (where applicable). jaroslav@597: * If you need a closed range (which includes both endpoints), and jaroslav@597: * the element type allows for calculation of the successor of a given jaroslav@597: * value, merely request the subrange from lowEndpoint to jaroslav@597: * successor(highEndpoint). For example, suppose that s jaroslav@597: * is a sorted set of strings. The following idiom obtains a view jaroslav@597: * containing all of the strings in s from low to jaroslav@597: * high, inclusive:

jaroslav@597:  *   SortedSet<String> sub = s.subSet(low, high+"\0");
jaroslav@597: * jaroslav@597: * A similar technique can be used to generate an open range (which jaroslav@597: * contains neither endpoint). The following idiom obtains a view jaroslav@597: * containing all of the Strings in s from low to jaroslav@597: * high, exclusive:
jaroslav@597:  *   SortedSet<String> sub = s.subSet(low+"\0", high);
jaroslav@597: * jaroslav@597: *

This interface is a member of the jaroslav@597: * jaroslav@597: * Java Collections Framework. jaroslav@597: * jaroslav@597: * @param the type of elements maintained by this set jaroslav@597: * jaroslav@597: * @author Josh Bloch jaroslav@597: * @see Set jaroslav@597: * @see TreeSet jaroslav@597: * @see SortedMap jaroslav@597: * @see Collection jaroslav@597: * @see Comparable jaroslav@597: * @see Comparator jaroslav@597: * @see ClassCastException jaroslav@597: * @since 1.2 jaroslav@597: */ jaroslav@597: jaroslav@597: public interface SortedSet extends Set { jaroslav@597: /** jaroslav@597: * Returns the comparator used to order the elements in this set, jaroslav@597: * or null if this set uses the {@linkplain Comparable jaroslav@597: * natural ordering} of its elements. jaroslav@597: * jaroslav@597: * @return the comparator used to order the elements in this set, jaroslav@597: * or null if this set uses the natural ordering jaroslav@597: * of its elements jaroslav@597: */ jaroslav@597: Comparator comparator(); jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns a view of the portion of this set whose elements range jaroslav@597: * from fromElement, inclusive, to toElement, jaroslav@597: * exclusive. (If fromElement and toElement are jaroslav@597: * equal, the returned set is empty.) The returned set is backed jaroslav@597: * by this set, so changes in the returned set are reflected in jaroslav@597: * this set, and vice-versa. The returned set supports all jaroslav@597: * optional set operations that this set supports. jaroslav@597: * jaroslav@597: *

The returned set will throw an IllegalArgumentException jaroslav@597: * on an attempt to insert an element outside its range. jaroslav@597: * jaroslav@597: * @param fromElement low endpoint (inclusive) of the returned set jaroslav@597: * @param toElement high endpoint (exclusive) of the returned set jaroslav@597: * @return a view of the portion of this set whose elements range from jaroslav@597: * fromElement, inclusive, to toElement, exclusive jaroslav@597: * @throws ClassCastException if fromElement and jaroslav@597: * toElement cannot be compared to one another using this jaroslav@597: * set's comparator (or, if the set has no comparator, using jaroslav@597: * natural ordering). Implementations may, but are not required jaroslav@597: * to, throw this exception if fromElement or jaroslav@597: * toElement cannot be compared to elements currently in jaroslav@597: * the set. jaroslav@597: * @throws NullPointerException if fromElement or jaroslav@597: * toElement is null and this set does not permit null jaroslav@597: * elements jaroslav@597: * @throws IllegalArgumentException if fromElement is jaroslav@597: * greater than toElement; or if this set itself jaroslav@597: * has a restricted range, and fromElement or jaroslav@597: * toElement lies outside the bounds of the range jaroslav@597: */ jaroslav@597: SortedSet subSet(E fromElement, E toElement); jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns a view of the portion of this set whose elements are jaroslav@597: * strictly less than toElement. The returned set is jaroslav@597: * backed by this set, so changes in the returned set are jaroslav@597: * reflected in this set, and vice-versa. The returned set jaroslav@597: * supports all optional set operations that this set supports. jaroslav@597: * jaroslav@597: *

The returned set will throw an IllegalArgumentException jaroslav@597: * on an attempt to insert an element outside its range. jaroslav@597: * jaroslav@597: * @param toElement high endpoint (exclusive) of the returned set jaroslav@597: * @return a view of the portion of this set whose elements are strictly jaroslav@597: * less than toElement jaroslav@597: * @throws ClassCastException if toElement is not compatible jaroslav@597: * with this set's comparator (or, if the set has no comparator, jaroslav@597: * if toElement does not implement {@link Comparable}). jaroslav@597: * Implementations may, but are not required to, throw this jaroslav@597: * exception if toElement cannot be compared to elements jaroslav@597: * currently in the set. jaroslav@597: * @throws NullPointerException if toElement is null and jaroslav@597: * this set does not permit null elements jaroslav@597: * @throws IllegalArgumentException if this set itself has a jaroslav@597: * restricted range, and toElement lies outside the jaroslav@597: * bounds of the range jaroslav@597: */ jaroslav@597: SortedSet headSet(E toElement); jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns a view of the portion of this set whose elements are jaroslav@597: * greater than or equal to fromElement. The returned jaroslav@597: * set is backed by this set, so changes in the returned set are jaroslav@597: * reflected in this set, and vice-versa. The returned set jaroslav@597: * supports all optional set operations that this set supports. jaroslav@597: * jaroslav@597: *

The returned set will throw an IllegalArgumentException jaroslav@597: * on an attempt to insert an element outside its range. jaroslav@597: * jaroslav@597: * @param fromElement low endpoint (inclusive) of the returned set jaroslav@597: * @return a view of the portion of this set whose elements are greater jaroslav@597: * than or equal to fromElement jaroslav@597: * @throws ClassCastException if fromElement is not compatible jaroslav@597: * with this set's comparator (or, if the set has no comparator, jaroslav@597: * if fromElement does not implement {@link Comparable}). jaroslav@597: * Implementations may, but are not required to, throw this jaroslav@597: * exception if fromElement cannot be compared to elements jaroslav@597: * currently in the set. jaroslav@597: * @throws NullPointerException if fromElement is null jaroslav@597: * and this set does not permit null elements jaroslav@597: * @throws IllegalArgumentException if this set itself has a jaroslav@597: * restricted range, and fromElement lies outside the jaroslav@597: * bounds of the range jaroslav@597: */ jaroslav@597: SortedSet tailSet(E fromElement); jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns the first (lowest) element currently in this set. jaroslav@597: * jaroslav@597: * @return the first (lowest) element currently in this set jaroslav@597: * @throws NoSuchElementException if this set is empty jaroslav@597: */ jaroslav@597: E first(); jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns the last (highest) element currently in this set. jaroslav@597: * jaroslav@597: * @return the last (highest) element currently in this set jaroslav@597: * @throws NoSuchElementException if this set is empty jaroslav@597: */ jaroslav@597: E last(); jaroslav@597: }