rt/emul/compact/src/main/java/java/util/Set.java
changeset 772 d382dacfd73f
parent 557 5be31d9fa455
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/util/Set.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,385 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.util;
    1.30 +
    1.31 +/**
    1.32 + * A collection that contains no duplicate elements.  More formally, sets
    1.33 + * contain no pair of elements <code>e1</code> and <code>e2</code> such that
    1.34 + * <code>e1.equals(e2)</code>, and at most one null element.  As implied by
    1.35 + * its name, this interface models the mathematical <i>set</i> abstraction.
    1.36 + *
    1.37 + * <p>The <tt>Set</tt> interface places additional stipulations, beyond those
    1.38 + * inherited from the <tt>Collection</tt> interface, on the contracts of all
    1.39 + * constructors and on the contracts of the <tt>add</tt>, <tt>equals</tt> and
    1.40 + * <tt>hashCode</tt> methods.  Declarations for other inherited methods are
    1.41 + * also included here for convenience.  (The specifications accompanying these
    1.42 + * declarations have been tailored to the <tt>Set</tt> interface, but they do
    1.43 + * not contain any additional stipulations.)
    1.44 + *
    1.45 + * <p>The additional stipulation on constructors is, not surprisingly,
    1.46 + * that all constructors must create a set that contains no duplicate elements
    1.47 + * (as defined above).
    1.48 + *
    1.49 + * <p>Note: Great care must be exercised if mutable objects are used as set
    1.50 + * elements.  The behavior of a set is not specified if the value of an object
    1.51 + * is changed in a manner that affects <tt>equals</tt> comparisons while the
    1.52 + * object is an element in the set.  A special case of this prohibition is
    1.53 + * that it is not permissible for a set to contain itself as an element.
    1.54 + *
    1.55 + * <p>Some set implementations have restrictions on the elements that
    1.56 + * they may contain.  For example, some implementations prohibit null elements,
    1.57 + * and some have restrictions on the types of their elements.  Attempting to
    1.58 + * add an ineligible element throws an unchecked exception, typically
    1.59 + * <tt>NullPointerException</tt> or <tt>ClassCastException</tt>.  Attempting
    1.60 + * to query the presence of an ineligible element may throw an exception,
    1.61 + * or it may simply return false; some implementations will exhibit the former
    1.62 + * behavior and some will exhibit the latter.  More generally, attempting an
    1.63 + * operation on an ineligible element whose completion would not result in
    1.64 + * the insertion of an ineligible element into the set may throw an
    1.65 + * exception or it may succeed, at the option of the implementation.
    1.66 + * Such exceptions are marked as "optional" in the specification for this
    1.67 + * interface.
    1.68 + *
    1.69 + * <p>This interface is a member of the
    1.70 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
    1.71 + * Java Collections Framework</a>.
    1.72 + *
    1.73 + * @param <E> the type of elements maintained by this set
    1.74 + *
    1.75 + * @author  Josh Bloch
    1.76 + * @author  Neal Gafter
    1.77 + * @see Collection
    1.78 + * @see List
    1.79 + * @see SortedSet
    1.80 + * @see HashSet
    1.81 + * @see TreeSet
    1.82 + * @see AbstractSet
    1.83 + * @see Collections#singleton(java.lang.Object)
    1.84 + * @see Collections#EMPTY_SET
    1.85 + * @since 1.2
    1.86 + */
    1.87 +
    1.88 +public interface Set<E> extends Collection<E> {
    1.89 +    // Query Operations
    1.90 +
    1.91 +    /**
    1.92 +     * Returns the number of elements in this set (its cardinality).  If this
    1.93 +     * set contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
    1.94 +     * <tt>Integer.MAX_VALUE</tt>.
    1.95 +     *
    1.96 +     * @return the number of elements in this set (its cardinality)
    1.97 +     */
    1.98 +    int size();
    1.99 +
   1.100 +    /**
   1.101 +     * Returns <tt>true</tt> if this set contains no elements.
   1.102 +     *
   1.103 +     * @return <tt>true</tt> if this set contains no elements
   1.104 +     */
   1.105 +    boolean isEmpty();
   1.106 +
   1.107 +    /**
   1.108 +     * Returns <tt>true</tt> if this set contains the specified element.
   1.109 +     * More formally, returns <tt>true</tt> if and only if this set
   1.110 +     * contains an element <tt>e</tt> such that
   1.111 +     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
   1.112 +     *
   1.113 +     * @param o element whose presence in this set is to be tested
   1.114 +     * @return <tt>true</tt> if this set contains the specified element
   1.115 +     * @throws ClassCastException if the type of the specified element
   1.116 +     *         is incompatible with this set
   1.117 +     * (<a href="Collection.html#optional-restrictions">optional</a>)
   1.118 +     * @throws NullPointerException if the specified element is null and this
   1.119 +     *         set does not permit null elements
   1.120 +     * (<a href="Collection.html#optional-restrictions">optional</a>)
   1.121 +     */
   1.122 +    boolean contains(Object o);
   1.123 +
   1.124 +    /**
   1.125 +     * Returns an iterator over the elements in this set.  The elements are
   1.126 +     * returned in no particular order (unless this set is an instance of some
   1.127 +     * class that provides a guarantee).
   1.128 +     *
   1.129 +     * @return an iterator over the elements in this set
   1.130 +     */
   1.131 +    Iterator<E> iterator();
   1.132 +
   1.133 +    /**
   1.134 +     * Returns an array containing all of the elements in this set.
   1.135 +     * If this set makes any guarantees as to what order its elements
   1.136 +     * are returned by its iterator, this method must return the
   1.137 +     * elements in the same order.
   1.138 +     *
   1.139 +     * <p>The returned array will be "safe" in that no references to it
   1.140 +     * are maintained by this set.  (In other words, this method must
   1.141 +     * allocate a new array even if this set is backed by an array).
   1.142 +     * The caller is thus free to modify the returned array.
   1.143 +     *
   1.144 +     * <p>This method acts as bridge between array-based and collection-based
   1.145 +     * APIs.
   1.146 +     *
   1.147 +     * @return an array containing all the elements in this set
   1.148 +     */
   1.149 +    Object[] toArray();
   1.150 +
   1.151 +    /**
   1.152 +     * Returns an array containing all of the elements in this set; the
   1.153 +     * runtime type of the returned array is that of the specified array.
   1.154 +     * If the set fits in the specified array, it is returned therein.
   1.155 +     * Otherwise, a new array is allocated with the runtime type of the
   1.156 +     * specified array and the size of this set.
   1.157 +     *
   1.158 +     * <p>If this set fits in the specified array with room to spare
   1.159 +     * (i.e., the array has more elements than this set), the element in
   1.160 +     * the array immediately following the end of the set is set to
   1.161 +     * <tt>null</tt>.  (This is useful in determining the length of this
   1.162 +     * set <i>only</i> if the caller knows that this set does not contain
   1.163 +     * any null elements.)
   1.164 +     *
   1.165 +     * <p>If this set makes any guarantees as to what order its elements
   1.166 +     * are returned by its iterator, this method must return the elements
   1.167 +     * in the same order.
   1.168 +     *
   1.169 +     * <p>Like the {@link #toArray()} method, this method acts as bridge between
   1.170 +     * array-based and collection-based APIs.  Further, this method allows
   1.171 +     * precise control over the runtime type of the output array, and may,
   1.172 +     * under certain circumstances, be used to save allocation costs.
   1.173 +     *
   1.174 +     * <p>Suppose <tt>x</tt> is a set known to contain only strings.
   1.175 +     * The following code can be used to dump the set into a newly allocated
   1.176 +     * array of <tt>String</tt>:
   1.177 +     *
   1.178 +     * <pre>
   1.179 +     *     String[] y = x.toArray(new String[0]);</pre>
   1.180 +     *
   1.181 +     * Note that <tt>toArray(new Object[0])</tt> is identical in function to
   1.182 +     * <tt>toArray()</tt>.
   1.183 +     *
   1.184 +     * @param a the array into which the elements of this set are to be
   1.185 +     *        stored, if it is big enough; otherwise, a new array of the same
   1.186 +     *        runtime type is allocated for this purpose.
   1.187 +     * @return an array containing all the elements in this set
   1.188 +     * @throws ArrayStoreException if the runtime type of the specified array
   1.189 +     *         is not a supertype of the runtime type of every element in this
   1.190 +     *         set
   1.191 +     * @throws NullPointerException if the specified array is null
   1.192 +     */
   1.193 +    <T> T[] toArray(T[] a);
   1.194 +
   1.195 +
   1.196 +    // Modification Operations
   1.197 +
   1.198 +    /**
   1.199 +     * Adds the specified element to this set if it is not already present
   1.200 +     * (optional operation).  More formally, adds the specified element
   1.201 +     * <tt>e</tt> to this set if the set contains no element <tt>e2</tt>
   1.202 +     * such that
   1.203 +     * <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>.
   1.204 +     * If this set already contains the element, the call leaves the set
   1.205 +     * unchanged and returns <tt>false</tt>.  In combination with the
   1.206 +     * restriction on constructors, this ensures that sets never contain
   1.207 +     * duplicate elements.
   1.208 +     *
   1.209 +     * <p>The stipulation above does not imply that sets must accept all
   1.210 +     * elements; sets may refuse to add any particular element, including
   1.211 +     * <tt>null</tt>, and throw an exception, as described in the
   1.212 +     * specification for {@link Collection#add Collection.add}.
   1.213 +     * Individual set implementations should clearly document any
   1.214 +     * restrictions on the elements that they may contain.
   1.215 +     *
   1.216 +     * @param e element to be added to this set
   1.217 +     * @return <tt>true</tt> if this set did not already contain the specified
   1.218 +     *         element
   1.219 +     * @throws UnsupportedOperationException if the <tt>add</tt> operation
   1.220 +     *         is not supported by this set
   1.221 +     * @throws ClassCastException if the class of the specified element
   1.222 +     *         prevents it from being added to this set
   1.223 +     * @throws NullPointerException if the specified element is null and this
   1.224 +     *         set does not permit null elements
   1.225 +     * @throws IllegalArgumentException if some property of the specified element
   1.226 +     *         prevents it from being added to this set
   1.227 +     */
   1.228 +    boolean add(E e);
   1.229 +
   1.230 +
   1.231 +    /**
   1.232 +     * Removes the specified element from this set if it is present
   1.233 +     * (optional operation).  More formally, removes an element <tt>e</tt>
   1.234 +     * such that
   1.235 +     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>, if
   1.236 +     * this set contains such an element.  Returns <tt>true</tt> if this set
   1.237 +     * contained the element (or equivalently, if this set changed as a
   1.238 +     * result of the call).  (This set will not contain the element once the
   1.239 +     * call returns.)
   1.240 +     *
   1.241 +     * @param o object to be removed from this set, if present
   1.242 +     * @return <tt>true</tt> if this set contained the specified element
   1.243 +     * @throws ClassCastException if the type of the specified element
   1.244 +     *         is incompatible with this set
   1.245 +     * (<a href="Collection.html#optional-restrictions">optional</a>)
   1.246 +     * @throws NullPointerException if the specified element is null and this
   1.247 +     *         set does not permit null elements
   1.248 +     * (<a href="Collection.html#optional-restrictions">optional</a>)
   1.249 +     * @throws UnsupportedOperationException if the <tt>remove</tt> operation
   1.250 +     *         is not supported by this set
   1.251 +     */
   1.252 +    boolean remove(Object o);
   1.253 +
   1.254 +
   1.255 +    // Bulk Operations
   1.256 +
   1.257 +    /**
   1.258 +     * Returns <tt>true</tt> if this set contains all of the elements of the
   1.259 +     * specified collection.  If the specified collection is also a set, this
   1.260 +     * method returns <tt>true</tt> if it is a <i>subset</i> of this set.
   1.261 +     *
   1.262 +     * @param  c collection to be checked for containment in this set
   1.263 +     * @return <tt>true</tt> if this set contains all of the elements of the
   1.264 +     *         specified collection
   1.265 +     * @throws ClassCastException if the types of one or more elements
   1.266 +     *         in the specified collection are incompatible with this
   1.267 +     *         set
   1.268 +     * (<a href="Collection.html#optional-restrictions">optional</a>)
   1.269 +     * @throws NullPointerException if the specified collection contains one
   1.270 +     *         or more null elements and this set does not permit null
   1.271 +     *         elements
   1.272 +     * (<a href="Collection.html#optional-restrictions">optional</a>),
   1.273 +     *         or if the specified collection is null
   1.274 +     * @see    #contains(Object)
   1.275 +     */
   1.276 +    boolean containsAll(Collection<?> c);
   1.277 +
   1.278 +    /**
   1.279 +     * Adds all of the elements in the specified collection to this set if
   1.280 +     * they're not already present (optional operation).  If the specified
   1.281 +     * collection is also a set, the <tt>addAll</tt> operation effectively
   1.282 +     * modifies this set so that its value is the <i>union</i> of the two
   1.283 +     * sets.  The behavior of this operation is undefined if the specified
   1.284 +     * collection is modified while the operation is in progress.
   1.285 +     *
   1.286 +     * @param  c collection containing elements to be added to this set
   1.287 +     * @return <tt>true</tt> if this set changed as a result of the call
   1.288 +     *
   1.289 +     * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
   1.290 +     *         is not supported by this set
   1.291 +     * @throws ClassCastException if the class of an element of the
   1.292 +     *         specified collection prevents it from being added to this set
   1.293 +     * @throws NullPointerException if the specified collection contains one
   1.294 +     *         or more null elements and this set does not permit null
   1.295 +     *         elements, or if the specified collection is null
   1.296 +     * @throws IllegalArgumentException if some property of an element of the
   1.297 +     *         specified collection prevents it from being added to this set
   1.298 +     * @see #add(Object)
   1.299 +     */
   1.300 +    boolean addAll(Collection<? extends E> c);
   1.301 +
   1.302 +    /**
   1.303 +     * Retains only the elements in this set that are contained in the
   1.304 +     * specified collection (optional operation).  In other words, removes
   1.305 +     * from this set all of its elements that are not contained in the
   1.306 +     * specified collection.  If the specified collection is also a set, this
   1.307 +     * operation effectively modifies this set so that its value is the
   1.308 +     * <i>intersection</i> of the two sets.
   1.309 +     *
   1.310 +     * @param  c collection containing elements to be retained in this set
   1.311 +     * @return <tt>true</tt> if this set changed as a result of the call
   1.312 +     * @throws UnsupportedOperationException if the <tt>retainAll</tt> operation
   1.313 +     *         is not supported by this set
   1.314 +     * @throws ClassCastException if the class of an element of this set
   1.315 +     *         is incompatible with the specified collection
   1.316 +     * (<a href="Collection.html#optional-restrictions">optional</a>)
   1.317 +     * @throws NullPointerException if this set contains a null element and the
   1.318 +     *         specified collection does not permit null elements
   1.319 +     *         (<a href="Collection.html#optional-restrictions">optional</a>),
   1.320 +     *         or if the specified collection is null
   1.321 +     * @see #remove(Object)
   1.322 +     */
   1.323 +    boolean retainAll(Collection<?> c);
   1.324 +
   1.325 +    /**
   1.326 +     * Removes from this set all of its elements that are contained in the
   1.327 +     * specified collection (optional operation).  If the specified
   1.328 +     * collection is also a set, this operation effectively modifies this
   1.329 +     * set so that its value is the <i>asymmetric set difference</i> of
   1.330 +     * the two sets.
   1.331 +     *
   1.332 +     * @param  c collection containing elements to be removed from this set
   1.333 +     * @return <tt>true</tt> if this set changed as a result of the call
   1.334 +     * @throws UnsupportedOperationException if the <tt>removeAll</tt> operation
   1.335 +     *         is not supported by this set
   1.336 +     * @throws ClassCastException if the class of an element of this set
   1.337 +     *         is incompatible with the specified collection
   1.338 +     * (<a href="Collection.html#optional-restrictions">optional</a>)
   1.339 +     * @throws NullPointerException if this set contains a null element and the
   1.340 +     *         specified collection does not permit null elements
   1.341 +     *         (<a href="Collection.html#optional-restrictions">optional</a>),
   1.342 +     *         or if the specified collection is null
   1.343 +     * @see #remove(Object)
   1.344 +     * @see #contains(Object)
   1.345 +     */
   1.346 +    boolean removeAll(Collection<?> c);
   1.347 +
   1.348 +    /**
   1.349 +     * Removes all of the elements from this set (optional operation).
   1.350 +     * The set will be empty after this call returns.
   1.351 +     *
   1.352 +     * @throws UnsupportedOperationException if the <tt>clear</tt> method
   1.353 +     *         is not supported by this set
   1.354 +     */
   1.355 +    void clear();
   1.356 +
   1.357 +
   1.358 +    // Comparison and hashing
   1.359 +
   1.360 +    /**
   1.361 +     * Compares the specified object with this set for equality.  Returns
   1.362 +     * <tt>true</tt> if the specified object is also a set, the two sets
   1.363 +     * have the same size, and every member of the specified set is
   1.364 +     * contained in this set (or equivalently, every member of this set is
   1.365 +     * contained in the specified set).  This definition ensures that the
   1.366 +     * equals method works properly across different implementations of the
   1.367 +     * set interface.
   1.368 +     *
   1.369 +     * @param o object to be compared for equality with this set
   1.370 +     * @return <tt>true</tt> if the specified object is equal to this set
   1.371 +     */
   1.372 +    boolean equals(Object o);
   1.373 +
   1.374 +    /**
   1.375 +     * Returns the hash code value for this set.  The hash code of a set is
   1.376 +     * defined to be the sum of the hash codes of the elements in the set,
   1.377 +     * where the hash code of a <tt>null</tt> element is defined to be zero.
   1.378 +     * This ensures that <tt>s1.equals(s2)</tt> implies that
   1.379 +     * <tt>s1.hashCode()==s2.hashCode()</tt> for any two sets <tt>s1</tt>
   1.380 +     * and <tt>s2</tt>, as required by the general contract of
   1.381 +     * {@link Object#hashCode}.
   1.382 +     *
   1.383 +     * @return the hash code value for this set
   1.384 +     * @see Object#equals(Object)
   1.385 +     * @see Set#equals(Object)
   1.386 +     */
   1.387 +    int hashCode();
   1.388 +}