--- jsr166/src/main/java/util/TreeSet.java 2004/12/28 12:14:07 1.1
+++ jsr166/src/main/java/util/TreeSet.java 2005/05/03 02:52:07 1.10
@@ -1,11 +1,11 @@
/*
* %W% %E%
*
- * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
+ * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
-package java.util;
+package java.util;
/**
* This class implements the Set interface, backed by a
@@ -53,9 +53,9 @@ package java.util;
* throw ConcurrentModificationException on a best-effort basis.
* Therefore, it would be wrong to write a program that depended on this
* exception for its correctness: the fail-fast behavior of iterators
- * should be used only to detect bugs.
+ * should be used only to detect bugs.
*
- * This class is a member of the
+ *
This class is a member of the
*
* Java Collections Framework.
*
@@ -199,7 +199,7 @@ public class TreeSet
*
* @throws ClassCastException if the specified object cannot be compared
* with the elements currently in the set.
- * @throws NullPointerException o is null and this map
+ * @throws NullPointerException if o is null and this map
* uses natural ordering and is non-empty, or its comparator does
* not tolerate null keys.
*/
@@ -210,18 +210,18 @@ public class TreeSet
/**
* Adds the specified element to this set if it is not already present.
*
- * @param o element to be added to this set.
+ * @param e element to be added to this set.
* @return true if the set did not already contain the specified
* element.
*
* @throws ClassCastException if the specified object cannot be compared
* with the elements currently in the set.
- * @throws NullPointerException o is null and this map
+ * @throws NullPointerException if e is null and this map
* uses natural ordering and is non-empty, or its comparator does
* not tolerate null keys.
*/
- public boolean add(E o) {
- return m.put(o, PRESENT)==null;
+ public boolean add(E e) {
+ return m.put(e, PRESENT)==null;
}
/**
@@ -232,7 +232,7 @@ public class TreeSet
*
* @throws ClassCastException if the specified object cannot be compared
* with the elements currently in the set.
- * @throws NullPointerException o is null and this map
+ * @throws NullPointerException if o is null and this map
* uses natural ordering and is non-empty, or its comparator does
* not tolerate null keys.
*/
@@ -255,7 +255,7 @@ public class TreeSet
*
* @throws ClassCastException if the elements provided cannot be compared
* with the elements currently in the set.
- * @throws NullPointerException of the specified collection is
+ * @throws NullPointerException if the specified collection is
* null or if any element is null and this map
* uses natural ordering, or its comparator does not tolerate
* null keys.
@@ -265,9 +265,9 @@ public class TreeSet
if (m.size()==0 && c.size() > 0 &&
c instanceof SortedSet &&
m instanceof TreeMap) {
- SortedSet> set = (SortedSet>) (SortedSet) c;
+ SortedSet extends E> set = (SortedSet extends E>) c;
TreeMap map = (TreeMap) m;
- Comparator super E> cc = (Comparator) set.comparator();
+ Comparator super E> cc = (Comparator super E>) set.comparator();
Comparator super E> mc = map.comparator();
if (cc==mc || (cc != null && cc.equals(mc))) {
map.addAllForTreeSet(set, PRESENT);
@@ -278,39 +278,40 @@ public class TreeSet
}
/**
- * Returns a view of the portion of this set whose elements range from
- * fromElement, inclusive, to toElement, exclusive. (If
- * fromElement and toElement are equal, the returned
- * sorted set is empty.) The returned sorted set is backed by this set,
- * so changes in the returned sorted set are reflected in this set, and
- * vice-versa. The returned sorted set supports all optional Set
- * operations.
+ * Returns a view of the portion of this set whose elements range
+ * from fromElement, inclusive, to toElement,
+ * exclusive. (If fromElement and toElement are
+ * equal, the returned navigable set is empty.) The returned
+ * navigable set is backed by this set, so changes in the returned
+ * navigable set are reflected in this set, and vice-versa. The
+ * returned navigable set supports all optional Set operations.
*
- * The sorted set returned by this method will throw an
+ * The navigable set returned by this method will throw an
* IllegalArgumentException if the user attempts to insert an
* element outside the specified range.
*
- * Note: this method always returns a half-open range (which
- * includes its low endpoint but not its high endpoint). If you need a
- * closed range (which includes both endpoints), and the element
- * type allows for calculation of the successor of a specified value,
- * merely request the subrange from lowEndpoint to
- * successor(highEndpoint). For example, suppose that s
- * is a sorted set of strings. The following idiom obtains a view
- * containing all of the strings in s from low to
- * high, inclusive:
- * NavigableSet sub = s.subSet(low, high+"\0");
+ * Note: this method always returns a half-open range
+ * (which includes its low endpoint but not its high endpoint).
+ * If you need a closed range (which includes both
+ * endpoints), and the element type allows for calculation of the
+ * successor of a specified value, merely request the subrange
+ * from lowEndpoint to successor(highEndpoint).
+ * For example, suppose that s is a navigable set of
+ * strings. The following idiom obtains a view containing all of
+ * the strings in s from low to high,
+ * inclusive:
+ * NavigableSet sub = s.navigableSubSet(low, high+"\0");
*
*
* A similar technique can be used to generate an open range (which
* contains neither endpoint). The following idiom obtains a view
* containing all of the strings in s from low to
* high, exclusive:
- * NavigableSet sub = s.subSet(low+"\0", high);
+ * NavigableSet sub = s.navigableSubSet(low+"\0", high);
*
*
- * @param fromElement low endpoint (inclusive) of the subSet.
- * @param toElement high endpoint (exclusive) of the subSet.
+ * @param fromElement low endpoint (inclusive) of the range.
+ * @param toElement high endpoint (exclusive) of the range.
* @return a view of the portion of this set whose elements range from
* fromElement, inclusive, to toElement,
* exclusive.
@@ -325,67 +326,69 @@ public class TreeSet
* order, or its comparator does not tolerate null
* elements.
*/
- public NavigableSet subSet(E fromElement, E toElement) {
- return new TreeSet(m.subMap(fromElement, toElement));
+ public NavigableSet navigableSubSet(E fromElement, E toElement) {
+ return new TreeSet(m.navigableSubMap(fromElement, toElement));
}
/**
- * Returns a view of the portion of this set whose elements are strictly
- * less than toElement. The returned sorted set is backed by
- * this set, so changes in the returned sorted set are reflected in this
- * set, and vice-versa. The returned sorted set supports all optional set
- * operations.
- *
- * The sorted set returned by this method will throw an
- * IllegalArgumentException if the user attempts to insert an
- * element greater than or equal to toElement.
- *
- * Note: this method always returns a view that does not contain its
- * (high) endpoint. If you need a view that does contain this endpoint,
- * and the element type allows for calculation of the successor of a
- * specified value, merely request a headSet bounded by
- * successor(highEndpoint). For example, suppose that s
- * is a sorted set of strings. The following idiom obtains a view
- * containing all of the strings in s that are less than or equal
- * to high:
NavigableSet head = s.headSet(high+"\0");
+ * Returns a view of the portion of this set whose elements are
+ * strictly less than toElement. The returned navigable
+ * set is backed by this set, so changes in the returned navigable
+ * set are reflected in this set, and vice-versa. The returned
+ * navigable set supports all optional set operations.
+ *
+ * The navigable set returned by this method will throw an
+ * IllegalArgumentException if the user attempts to
+ * insert an element greater than or equal to
+ * toElement.
+ *
+ * Note: this method always returns a view that does not contain
+ * its (high) endpoint. If you need a view that does contain this
+ * endpoint, and the element type allows for calculation of the
+ * successor of a specified value, merely request a headSet
+ * bounded by successor(highEndpoint). For example,
+ * suppose that s is a navigable set of strings. The
+ * following idiom obtains a view containing all of the strings in
+ * s that are less than or equal to high:
+ *
NavigableSet head = s.navigableHeadSet(high+"\0");
*
* @param toElement high endpoint (exclusive) of the headSet.
* @return a view of the portion of this set whose elements are strictly
- * less than toElement.
+ * less than toElement.
* @throws ClassCastException if toElement is not compatible
* with this set's comparator (or, if the set has no comparator,
* if toElement does not implement Comparable).
- * @throws IllegalArgumentException if this set is itself a subSet,
- * headSet, or tailSet, and toElement is not within the
- * specified range of the subSet, headSet, or tailSet.
+ * @throws IllegalArgumentException if this set is itself a subset,
+ * and toElement is not within the
+ * specified range of the subset.
* @throws NullPointerException if toElement is null and
* this set uses natural ordering, or its comparator does
* not tolerate null elements.
*/
- public NavigableSet headSet(E toElement) {
- return new TreeSet(m.headMap(toElement));
+ public NavigableSet navigableHeadSet(E toElement) {
+ return new TreeSet(m.navigableHeadMap(toElement));
}
/**
* Returns a view of the portion of this set whose elements are
- * greater than or equal to fromElement. The returned sorted set
- * is backed by this set, so changes in the returned sorted set are
- * reflected in this set, and vice-versa. The returned sorted set
- * supports all optional set operations.
+ * greater than or equal to fromElement. The returned
+ * navigable set is backed by this set, so changes in the returned
+ * navigable set are reflected in this set, and vice-versa. The
+ * returned navigable set supports all optional set operations.
*
- * The sorted set returned by this method will throw an
+ * The navigable set returned by this method will throw an
* IllegalArgumentException if the user attempts to insert an
* element less than fromElement.
*
* Note: this method always returns a view that contains its (low)
- * endpoint. If you need a view that does not contain this endpoint, and
- * the element type allows for calculation of the successor of a specified
- * value, merely request a tailSet bounded by
- * successor(lowEndpoint). For example, suppose that s
- * is a sorted set of strings. The following idiom obtains a view
- * containing all of the strings in s that are strictly greater
- * than low:
- * NavigableSet tail = s.tailSet(low+"\0");
+ * endpoint. If you need a view that does not contain this
+ * endpoint, and the element type allows for calculation of the
+ * successor of a specified value, merely request a tailSet
+ * bounded by successor(lowEndpoint). For example,
+ * suppose that s is a navigable set of strings. The
+ * following idiom obtains a view containing all of the strings in
+ * s that are strictly greater than low:
+ * NavigableSet tail = s.navigableTailSet(low+"\0");
*
*
* @param fromElement low endpoint (inclusive) of the tailSet.
@@ -394,15 +397,80 @@ public class TreeSet
* @throws ClassCastException if fromElement is not compatible
* with this set's comparator (or, if the set has no comparator,
* if fromElement does not implement Comparable).
- * @throws IllegalArgumentException if this set is itself a subSet,
- * headSet, or tailSet, and fromElement is not within the
- * specified range of the subSet, headSet, or tailSet.
+ * @throws IllegalArgumentException if this set is itself a subset,
+ * and fromElement is not within the
+ * specified range of the subset.
* @throws NullPointerException if fromElement is null
* and this set uses natural ordering, or its comparator does
* not tolerate null elements.
*/
- public NavigableSet tailSet(E fromElement) {
- return new TreeSet(m.tailMap(fromElement));
+ public NavigableSet navigableTailSet(E fromElement) {
+ return new TreeSet(m.navigableTailMap(fromElement));
+ }
+
+
+ /**
+ * Equivalent to navigableSubSet but with a return
+ * type conforming to the SortedSet interface.
+ * @param fromElement low endpoint (inclusive) of the range.
+ * @param toElement high endpoint (exclusive) of the range.
+ * @return a view of the portion of this set whose elements range from
+ * fromElement, inclusive, to toElement,
+ * exclusive.
+ * @throws ClassCastException if fromElement and
+ * toElement cannot be compared to one another using
+ * this set's comparator (or, if the set has no comparator,
+ * using natural ordering).
+ * @throws IllegalArgumentException if fromElement is greater than
+ * toElement.
+ * @throws NullPointerException if fromElement or
+ * toElement is null and this set uses natural
+ * order, or its comparator does not tolerate null
+ * elements.
+ */
+ public SortedSet subSet(E fromElement, E toElement) {
+ return new TreeSet(m.navigableSubMap(fromElement, toElement));
+ }
+
+ /**
+ * Equivalent to navigableHeadSet but with a return
+ * type conforming to the SortedSet interface.
+ *
+ * @param toElement high endpoint (exclusive) of the headSet.
+ * @return a view of the portion of this set whose elements are strictly
+ * less than toElement.
+ * @throws ClassCastException if toElement is not compatible
+ * with this set's comparator (or, if the set has no comparator,
+ * if toElement does not implement Comparable).
+ * @throws IllegalArgumentException if this set is itself a subset,
+ * and toElement is not within the
+ * specified range of the subset.
+ * @throws NullPointerException if toElement is null and
+ * this set uses natural ordering, or its comparator does
+ * not tolerate null elements.
+ */
+ public SortedSet headSet(E toElement) {
+ return new TreeSet(m.navigableHeadMap(toElement));
+ }
+
+ /**
+ * Equivalent to navigableTailSet but with a return
+ * type conforming to the SortedSet interface.
+ * @param fromElement low endpoint (inclusive) of the tailSet.
+ * @return a view of the portion of this set whose elements are
+ * greater than or equal to fromElement.
+ * @throws ClassCastException if fromElement is not compatible
+ * with this set's comparator (or, if the set has no comparator,
+ * if fromElement does not implement Comparable).
+ * @throws IllegalArgumentException if this set is itself a subset,
+ * and fromElement is not within the
+ * specified range of the subset.
+ * @throws NullPointerException if fromElement is null
+ * and this set uses natural ordering, or its comparator does
+ * not tolerate null elements.
+ */
+ public SortedSet tailSet(E fromElement) {
+ return new TreeSet(m.navigableTailMap(fromElement));
}
/**
@@ -442,69 +510,69 @@ public class TreeSet
/**
* Returns an element greater than or equal to the given element, or
* null if there is no such element.
- *
- * @param o the value to match
+ *
+ * @param e the value to match
* @return an element greater than or equal to given element, or
* null if there is no such element.
- * @throws ClassCastException if o cannot be compared with the elements
+ * @throws ClassCastException if e cannot be compared with the elements
* currently in the set.
- * @throws NullPointerException o is null and this map
+ * @throws NullPointerException if e is null and this map
* uses natural ordering and is non-empty, or its comparator does
* not tolerate null keys.
*/
- public E ceiling(E o) {
- return m.ceilingKey(o);
+ public E ceiling(E e) {
+ return m.ceilingKey(e);
}
/**
* Returns an element strictly less than the given element, or
* null if there is no such element.
- *
- * @param o the value to match
+ *
+ * @param e the value to match
* @return the greatest element less than the given element, or
* null if there is no such element.
- * @throws ClassCastException if o cannot be compared with the elements
+ * @throws ClassCastException if e cannot be compared with the elements
* currently in the set.
- * @throws NullPointerException o is null and this map
+ * @throws NullPointerException if e is null and this map
* uses natural ordering and is non-empty, or its comparator does
* not tolerate null keys.
*/
- public E lower(E o) {
- return m.lowerKey(o);
+ public E lower(E e) {
+ return m.lowerKey(e);
}
/**
* Returns an element less than or equal to the given element, or
* null if there is no such element.
- *
- * @param o the value to match
+ *
+ * @param e the value to match
* @return the greatest element less than or equal to given
* element, or null if there is no such element.
- * @throws ClassCastException if o cannot be compared with the elements
+ * @throws ClassCastException if e cannot be compared with the elements
* currently in the set.
- * @throws NullPointerException o is null and this map
+ * @throws NullPointerException if e is null and this map
* uses natural ordering and is non-empty, or its comparator does
* not tolerate null keys.
*/
- public E floor(E o) {
- return m.floorKey(o);
+ public E floor(E e) {
+ return m.floorKey(e);
}
/**
* Returns an element strictly greater than the given element, or
* null if there is no such element.
- *
- * @param o the value to match
+ *
+ * @param e the value to match
* @return the least element greater than the given element, or
* null if there is no such element.
- * @throws ClassCastException if o cannot be compared with the elements
+ * @throws ClassCastException if e cannot be compared with the elements
* currently in the set.
- * @throws NullPointerException o is null and this map
+ * @throws NullPointerException if e is null and this map
* uses natural ordering and is non-empty, or its comparator does
* not tolerate null keys.
*/
- public E higher(E o) {
- return m.higherKey(o);
+ public E higher(E e) {
+ return m.higherKey(e);
}
/**
@@ -584,7 +652,7 @@ public class TreeSet
s.defaultReadObject();
// Read in Comparator
- Comparator c = (Comparator) s.readObject();
+ Comparator super E> c = (Comparator super E>) s.readObject();
// Create backing TreeMap
TreeMap tm;