1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/compact/src/main/java/java/util/Collection.java Mon Feb 25 19:00:08 2013 +0100
1.3 @@ -0,0 +1,456 @@
1.4 +/*
1.5 + * Copyright (c) 1997, 2010, 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 + * The root interface in the <i>collection hierarchy</i>. A collection
1.33 + * represents a group of objects, known as its <i>elements</i>. Some
1.34 + * collections allow duplicate elements and others do not. Some are ordered
1.35 + * and others unordered. The JDK does not provide any <i>direct</i>
1.36 + * implementations of this interface: it provides implementations of more
1.37 + * specific subinterfaces like <tt>Set</tt> and <tt>List</tt>. This interface
1.38 + * is typically used to pass collections around and manipulate them where
1.39 + * maximum generality is desired.
1.40 + *
1.41 + * <p><i>Bags</i> or <i>multisets</i> (unordered collections that may contain
1.42 + * duplicate elements) should implement this interface directly.
1.43 + *
1.44 + * <p>All general-purpose <tt>Collection</tt> implementation classes (which
1.45 + * typically implement <tt>Collection</tt> indirectly through one of its
1.46 + * subinterfaces) should provide two "standard" constructors: a void (no
1.47 + * arguments) constructor, which creates an empty collection, and a
1.48 + * constructor with a single argument of type <tt>Collection</tt>, which
1.49 + * creates a new collection with the same elements as its argument. In
1.50 + * effect, the latter constructor allows the user to copy any collection,
1.51 + * producing an equivalent collection of the desired implementation type.
1.52 + * There is no way to enforce this convention (as interfaces cannot contain
1.53 + * constructors) but all of the general-purpose <tt>Collection</tt>
1.54 + * implementations in the Java platform libraries comply.
1.55 + *
1.56 + * <p>The "destructive" methods contained in this interface, that is, the
1.57 + * methods that modify the collection on which they operate, are specified to
1.58 + * throw <tt>UnsupportedOperationException</tt> if this collection does not
1.59 + * support the operation. If this is the case, these methods may, but are not
1.60 + * required to, throw an <tt>UnsupportedOperationException</tt> if the
1.61 + * invocation would have no effect on the collection. For example, invoking
1.62 + * the {@link #addAll(Collection)} method on an unmodifiable collection may,
1.63 + * but is not required to, throw the exception if the collection to be added
1.64 + * is empty.
1.65 + *
1.66 + * <p><a name="optional-restrictions"/>
1.67 + * Some collection implementations have restrictions on the elements that
1.68 + * they may contain. For example, some implementations prohibit null elements,
1.69 + * and some have restrictions on the types of their elements. Attempting to
1.70 + * add an ineligible element throws an unchecked exception, typically
1.71 + * <tt>NullPointerException</tt> or <tt>ClassCastException</tt>. Attempting
1.72 + * to query the presence of an ineligible element may throw an exception,
1.73 + * or it may simply return false; some implementations will exhibit the former
1.74 + * behavior and some will exhibit the latter. More generally, attempting an
1.75 + * operation on an ineligible element whose completion would not result in
1.76 + * the insertion of an ineligible element into the collection may throw an
1.77 + * exception or it may succeed, at the option of the implementation.
1.78 + * Such exceptions are marked as "optional" in the specification for this
1.79 + * interface.
1.80 + *
1.81 + * <p>It is up to each collection to determine its own synchronization
1.82 + * policy. In the absence of a stronger guarantee by the
1.83 + * implementation, undefined behavior may result from the invocation
1.84 + * of any method on a collection that is being mutated by another
1.85 + * thread; this includes direct invocations, passing the collection to
1.86 + * a method that might perform invocations, and using an existing
1.87 + * iterator to examine the collection.
1.88 + *
1.89 + * <p>Many methods in Collections Framework interfaces are defined in
1.90 + * terms of the {@link Object#equals(Object) equals} method. For example,
1.91 + * the specification for the {@link #contains(Object) contains(Object o)}
1.92 + * method says: "returns <tt>true</tt> if and only if this collection
1.93 + * contains at least one element <tt>e</tt> such that
1.94 + * <tt>(o==null ? e==null : o.equals(e))</tt>." This specification should
1.95 + * <i>not</i> be construed to imply that invoking <tt>Collection.contains</tt>
1.96 + * with a non-null argument <tt>o</tt> will cause <tt>o.equals(e)</tt> to be
1.97 + * invoked for any element <tt>e</tt>. Implementations are free to implement
1.98 + * optimizations whereby the <tt>equals</tt> invocation is avoided, for
1.99 + * example, by first comparing the hash codes of the two elements. (The
1.100 + * {@link Object#hashCode()} specification guarantees that two objects with
1.101 + * unequal hash codes cannot be equal.) More generally, implementations of
1.102 + * the various Collections Framework interfaces are free to take advantage of
1.103 + * the specified behavior of underlying {@link Object} methods wherever the
1.104 + * implementor deems it appropriate.
1.105 + *
1.106 + * <p>This interface is a member of the
1.107 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
1.108 + * Java Collections Framework</a>.
1.109 + *
1.110 + * @param <E> the type of elements in this collection
1.111 + *
1.112 + * @author Josh Bloch
1.113 + * @author Neal Gafter
1.114 + * @see Set
1.115 + * @see List
1.116 + * @see Map
1.117 + * @see SortedSet
1.118 + * @see SortedMap
1.119 + * @see HashSet
1.120 + * @see TreeSet
1.121 + * @see ArrayList
1.122 + * @see LinkedList
1.123 + * @see Vector
1.124 + * @see Collections
1.125 + * @see Arrays
1.126 + * @see AbstractCollection
1.127 + * @since 1.2
1.128 + */
1.129 +
1.130 +public interface Collection<E> extends Iterable<E> {
1.131 + // Query Operations
1.132 +
1.133 + /**
1.134 + * Returns the number of elements in this collection. If this collection
1.135 + * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
1.136 + * <tt>Integer.MAX_VALUE</tt>.
1.137 + *
1.138 + * @return the number of elements in this collection
1.139 + */
1.140 + int size();
1.141 +
1.142 + /**
1.143 + * Returns <tt>true</tt> if this collection contains no elements.
1.144 + *
1.145 + * @return <tt>true</tt> if this collection contains no elements
1.146 + */
1.147 + boolean isEmpty();
1.148 +
1.149 + /**
1.150 + * Returns <tt>true</tt> if this collection contains the specified element.
1.151 + * More formally, returns <tt>true</tt> if and only if this collection
1.152 + * contains at least one element <tt>e</tt> such that
1.153 + * <tt>(o==null ? e==null : o.equals(e))</tt>.
1.154 + *
1.155 + * @param o element whose presence in this collection is to be tested
1.156 + * @return <tt>true</tt> if this collection contains the specified
1.157 + * element
1.158 + * @throws ClassCastException if the type of the specified element
1.159 + * is incompatible with this collection
1.160 + * (<a href="#optional-restrictions">optional</a>)
1.161 + * @throws NullPointerException if the specified element is null and this
1.162 + * collection does not permit null elements
1.163 + * (<a href="#optional-restrictions">optional</a>)
1.164 + */
1.165 + boolean contains(Object o);
1.166 +
1.167 + /**
1.168 + * Returns an iterator over the elements in this collection. There are no
1.169 + * guarantees concerning the order in which the elements are returned
1.170 + * (unless this collection is an instance of some class that provides a
1.171 + * guarantee).
1.172 + *
1.173 + * @return an <tt>Iterator</tt> over the elements in this collection
1.174 + */
1.175 + Iterator<E> iterator();
1.176 +
1.177 + /**
1.178 + * Returns an array containing all of the elements in this collection.
1.179 + * If this collection makes any guarantees as to what order its elements
1.180 + * are returned by its iterator, this method must return the elements in
1.181 + * the same order.
1.182 + *
1.183 + * <p>The returned array will be "safe" in that no references to it are
1.184 + * maintained by this collection. (In other words, this method must
1.185 + * allocate a new array even if this collection is backed by an array).
1.186 + * The caller is thus free to modify the returned array.
1.187 + *
1.188 + * <p>This method acts as bridge between array-based and collection-based
1.189 + * APIs.
1.190 + *
1.191 + * @return an array containing all of the elements in this collection
1.192 + */
1.193 + Object[] toArray();
1.194 +
1.195 + /**
1.196 + * Returns an array containing all of the elements in this collection;
1.197 + * the runtime type of the returned array is that of the specified array.
1.198 + * If the collection fits in the specified array, it is returned therein.
1.199 + * Otherwise, a new array is allocated with the runtime type of the
1.200 + * specified array and the size of this collection.
1.201 + *
1.202 + * <p>If this collection fits in the specified array with room to spare
1.203 + * (i.e., the array has more elements than this collection), the element
1.204 + * in the array immediately following the end of the collection is set to
1.205 + * <tt>null</tt>. (This is useful in determining the length of this
1.206 + * collection <i>only</i> if the caller knows that this collection does
1.207 + * not contain any <tt>null</tt> elements.)
1.208 + *
1.209 + * <p>If this collection makes any guarantees as to what order its elements
1.210 + * are returned by its iterator, this method must return the elements in
1.211 + * the same order.
1.212 + *
1.213 + * <p>Like the {@link #toArray()} method, this method acts as bridge between
1.214 + * array-based and collection-based APIs. Further, this method allows
1.215 + * precise control over the runtime type of the output array, and may,
1.216 + * under certain circumstances, be used to save allocation costs.
1.217 + *
1.218 + * <p>Suppose <tt>x</tt> is a collection known to contain only strings.
1.219 + * The following code can be used to dump the collection into a newly
1.220 + * allocated array of <tt>String</tt>:
1.221 + *
1.222 + * <pre>
1.223 + * String[] y = x.toArray(new String[0]);</pre>
1.224 + *
1.225 + * Note that <tt>toArray(new Object[0])</tt> is identical in function to
1.226 + * <tt>toArray()</tt>.
1.227 + *
1.228 + * @param a the array into which the elements of this collection are to be
1.229 + * stored, if it is big enough; otherwise, a new array of the same
1.230 + * runtime type is allocated for this purpose.
1.231 + * @return an array containing all of the elements in this collection
1.232 + * @throws ArrayStoreException if the runtime type of the specified array
1.233 + * is not a supertype of the runtime type of every element in
1.234 + * this collection
1.235 + * @throws NullPointerException if the specified array is null
1.236 + */
1.237 + <T> T[] toArray(T[] a);
1.238 +
1.239 + // Modification Operations
1.240 +
1.241 + /**
1.242 + * Ensures that this collection contains the specified element (optional
1.243 + * operation). Returns <tt>true</tt> if this collection changed as a
1.244 + * result of the call. (Returns <tt>false</tt> if this collection does
1.245 + * not permit duplicates and already contains the specified element.)<p>
1.246 + *
1.247 + * Collections that support this operation may place limitations on what
1.248 + * elements may be added to this collection. In particular, some
1.249 + * collections will refuse to add <tt>null</tt> elements, and others will
1.250 + * impose restrictions on the type of elements that may be added.
1.251 + * Collection classes should clearly specify in their documentation any
1.252 + * restrictions on what elements may be added.<p>
1.253 + *
1.254 + * If a collection refuses to add a particular element for any reason
1.255 + * other than that it already contains the element, it <i>must</i> throw
1.256 + * an exception (rather than returning <tt>false</tt>). This preserves
1.257 + * the invariant that a collection always contains the specified element
1.258 + * after this call returns.
1.259 + *
1.260 + * @param e element whose presence in this collection is to be ensured
1.261 + * @return <tt>true</tt> if this collection changed as a result of the
1.262 + * call
1.263 + * @throws UnsupportedOperationException if the <tt>add</tt> operation
1.264 + * is not supported by this collection
1.265 + * @throws ClassCastException if the class of the specified element
1.266 + * prevents it from being added to this collection
1.267 + * @throws NullPointerException if the specified element is null and this
1.268 + * collection does not permit null elements
1.269 + * @throws IllegalArgumentException if some property of the element
1.270 + * prevents it from being added to this collection
1.271 + * @throws IllegalStateException if the element cannot be added at this
1.272 + * time due to insertion restrictions
1.273 + */
1.274 + boolean add(E e);
1.275 +
1.276 + /**
1.277 + * Removes a single instance of the specified element from this
1.278 + * collection, if it is present (optional operation). More formally,
1.279 + * removes an element <tt>e</tt> such that
1.280 + * <tt>(o==null ? e==null : o.equals(e))</tt>, if
1.281 + * this collection contains one or more such elements. Returns
1.282 + * <tt>true</tt> if this collection contained the specified element (or
1.283 + * equivalently, if this collection changed as a result of the call).
1.284 + *
1.285 + * @param o element to be removed from this collection, if present
1.286 + * @return <tt>true</tt> if an element was removed as a result of this call
1.287 + * @throws ClassCastException if the type of the specified element
1.288 + * is incompatible with this collection
1.289 + * (<a href="#optional-restrictions">optional</a>)
1.290 + * @throws NullPointerException if the specified element is null and this
1.291 + * collection does not permit null elements
1.292 + * (<a href="#optional-restrictions">optional</a>)
1.293 + * @throws UnsupportedOperationException if the <tt>remove</tt> operation
1.294 + * is not supported by this collection
1.295 + */
1.296 + boolean remove(Object o);
1.297 +
1.298 +
1.299 + // Bulk Operations
1.300 +
1.301 + /**
1.302 + * Returns <tt>true</tt> if this collection contains all of the elements
1.303 + * in the specified collection.
1.304 + *
1.305 + * @param c collection to be checked for containment in this collection
1.306 + * @return <tt>true</tt> if this collection contains all of the elements
1.307 + * in the specified collection
1.308 + * @throws ClassCastException if the types of one or more elements
1.309 + * in the specified collection are incompatible with this
1.310 + * collection
1.311 + * (<a href="#optional-restrictions">optional</a>)
1.312 + * @throws NullPointerException if the specified collection contains one
1.313 + * or more null elements and this collection does not permit null
1.314 + * elements
1.315 + * (<a href="#optional-restrictions">optional</a>),
1.316 + * or if the specified collection is null.
1.317 + * @see #contains(Object)
1.318 + */
1.319 + boolean containsAll(Collection<?> c);
1.320 +
1.321 + /**
1.322 + * Adds all of the elements in the specified collection to this collection
1.323 + * (optional operation). The behavior of this operation is undefined if
1.324 + * the specified collection is modified while the operation is in progress.
1.325 + * (This implies that the behavior of this call is undefined if the
1.326 + * specified collection is this collection, and this collection is
1.327 + * nonempty.)
1.328 + *
1.329 + * @param c collection containing elements to be added to this collection
1.330 + * @return <tt>true</tt> if this collection changed as a result of the call
1.331 + * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
1.332 + * is not supported by this collection
1.333 + * @throws ClassCastException if the class of an element of the specified
1.334 + * collection prevents it from being added to this collection
1.335 + * @throws NullPointerException if the specified collection contains a
1.336 + * null element and this collection does not permit null elements,
1.337 + * or if the specified collection is null
1.338 + * @throws IllegalArgumentException if some property of an element of the
1.339 + * specified collection prevents it from being added to this
1.340 + * collection
1.341 + * @throws IllegalStateException if not all the elements can be added at
1.342 + * this time due to insertion restrictions
1.343 + * @see #add(Object)
1.344 + */
1.345 + boolean addAll(Collection<? extends E> c);
1.346 +
1.347 + /**
1.348 + * Removes all of this collection's elements that are also contained in the
1.349 + * specified collection (optional operation). After this call returns,
1.350 + * this collection will contain no elements in common with the specified
1.351 + * collection.
1.352 + *
1.353 + * @param c collection containing elements to be removed from this collection
1.354 + * @return <tt>true</tt> if this collection changed as a result of the
1.355 + * call
1.356 + * @throws UnsupportedOperationException if the <tt>removeAll</tt> method
1.357 + * is not supported by this collection
1.358 + * @throws ClassCastException if the types of one or more elements
1.359 + * in this collection are incompatible with the specified
1.360 + * collection
1.361 + * (<a href="#optional-restrictions">optional</a>)
1.362 + * @throws NullPointerException if this collection contains one or more
1.363 + * null elements and the specified collection does not support
1.364 + * null elements
1.365 + * (<a href="#optional-restrictions">optional</a>),
1.366 + * or if the specified collection is null
1.367 + * @see #remove(Object)
1.368 + * @see #contains(Object)
1.369 + */
1.370 + boolean removeAll(Collection<?> c);
1.371 +
1.372 + /**
1.373 + * Retains only the elements in this collection that are contained in the
1.374 + * specified collection (optional operation). In other words, removes from
1.375 + * this collection all of its elements that are not contained in the
1.376 + * specified collection.
1.377 + *
1.378 + * @param c collection containing elements to be retained in this collection
1.379 + * @return <tt>true</tt> if this collection changed as a result of the call
1.380 + * @throws UnsupportedOperationException if the <tt>retainAll</tt> operation
1.381 + * is not supported by this collection
1.382 + * @throws ClassCastException if the types of one or more elements
1.383 + * in this collection are incompatible with the specified
1.384 + * collection
1.385 + * (<a href="#optional-restrictions">optional</a>)
1.386 + * @throws NullPointerException if this collection contains one or more
1.387 + * null elements and the specified collection does not permit null
1.388 + * elements
1.389 + * (<a href="#optional-restrictions">optional</a>),
1.390 + * or if the specified collection is null
1.391 + * @see #remove(Object)
1.392 + * @see #contains(Object)
1.393 + */
1.394 + boolean retainAll(Collection<?> c);
1.395 +
1.396 + /**
1.397 + * Removes all of the elements from this collection (optional operation).
1.398 + * The collection will be empty after this method returns.
1.399 + *
1.400 + * @throws UnsupportedOperationException if the <tt>clear</tt> operation
1.401 + * is not supported by this collection
1.402 + */
1.403 + void clear();
1.404 +
1.405 +
1.406 + // Comparison and hashing
1.407 +
1.408 + /**
1.409 + * Compares the specified object with this collection for equality. <p>
1.410 + *
1.411 + * While the <tt>Collection</tt> interface adds no stipulations to the
1.412 + * general contract for the <tt>Object.equals</tt>, programmers who
1.413 + * implement the <tt>Collection</tt> interface "directly" (in other words,
1.414 + * create a class that is a <tt>Collection</tt> but is not a <tt>Set</tt>
1.415 + * or a <tt>List</tt>) must exercise care if they choose to override the
1.416 + * <tt>Object.equals</tt>. It is not necessary to do so, and the simplest
1.417 + * course of action is to rely on <tt>Object</tt>'s implementation, but
1.418 + * the implementor may wish to implement a "value comparison" in place of
1.419 + * the default "reference comparison." (The <tt>List</tt> and
1.420 + * <tt>Set</tt> interfaces mandate such value comparisons.)<p>
1.421 + *
1.422 + * The general contract for the <tt>Object.equals</tt> method states that
1.423 + * equals must be symmetric (in other words, <tt>a.equals(b)</tt> if and
1.424 + * only if <tt>b.equals(a)</tt>). The contracts for <tt>List.equals</tt>
1.425 + * and <tt>Set.equals</tt> state that lists are only equal to other lists,
1.426 + * and sets to other sets. Thus, a custom <tt>equals</tt> method for a
1.427 + * collection class that implements neither the <tt>List</tt> nor
1.428 + * <tt>Set</tt> interface must return <tt>false</tt> when this collection
1.429 + * is compared to any list or set. (By the same logic, it is not possible
1.430 + * to write a class that correctly implements both the <tt>Set</tt> and
1.431 + * <tt>List</tt> interfaces.)
1.432 + *
1.433 + * @param o object to be compared for equality with this collection
1.434 + * @return <tt>true</tt> if the specified object is equal to this
1.435 + * collection
1.436 + *
1.437 + * @see Object#equals(Object)
1.438 + * @see Set#equals(Object)
1.439 + * @see List#equals(Object)
1.440 + */
1.441 + boolean equals(Object o);
1.442 +
1.443 + /**
1.444 + * Returns the hash code value for this collection. While the
1.445 + * <tt>Collection</tt> interface adds no stipulations to the general
1.446 + * contract for the <tt>Object.hashCode</tt> method, programmers should
1.447 + * take note that any class that overrides the <tt>Object.equals</tt>
1.448 + * method must also override the <tt>Object.hashCode</tt> method in order
1.449 + * to satisfy the general contract for the <tt>Object.hashCode</tt> method.
1.450 + * In particular, <tt>c1.equals(c2)</tt> implies that
1.451 + * <tt>c1.hashCode()==c2.hashCode()</tt>.
1.452 + *
1.453 + * @return the hash code value for this collection
1.454 + *
1.455 + * @see Object#hashCode()
1.456 + * @see Object#equals(Object)
1.457 + */
1.458 + int hashCode();
1.459 +}