001: /*
002: * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package java.util;
027:
028: /**
029: * The root interface in the <i>collection hierarchy</i>. A collection
030: * represents a group of objects, known as its <i>elements</i>. Some
031: * collections allow duplicate elements and others do not. Some are ordered
032: * and others unordered. The JDK does not provide any <i>direct</i>
033: * implementations of this interface: it provides implementations of more
034: * specific subinterfaces like <tt>Set</tt> and <tt>List</tt>. This interface
035: * is typically used to pass collections around and manipulate them where
036: * maximum generality is desired.
037: *
038: * <p><i>Bags</i> or <i>multisets</i> (unordered collections that may contain
039: * duplicate elements) should implement this interface directly.
040: *
041: * <p>All general-purpose <tt>Collection</tt> implementation classes (which
042: * typically implement <tt>Collection</tt> indirectly through one of its
043: * subinterfaces) should provide two "standard" constructors: a void (no
044: * arguments) constructor, which creates an empty collection, and a
045: * constructor with a single argument of type <tt>Collection</tt>, which
046: * creates a new collection with the same elements as its argument. In
047: * effect, the latter constructor allows the user to copy any collection,
048: * producing an equivalent collection of the desired implementation type.
049: * There is no way to enforce this convention (as interfaces cannot contain
050: * constructors) but all of the general-purpose <tt>Collection</tt>
051: * implementations in the Java platform libraries comply.
052: *
053: * <p>The "destructive" methods contained in this interface, that is, the
054: * methods that modify the collection on which they operate, are specified to
055: * throw <tt>UnsupportedOperationException</tt> if this collection does not
056: * support the operation. If this is the case, these methods may, but are not
057: * required to, throw an <tt>UnsupportedOperationException</tt> if the
058: * invocation would have no effect on the collection. For example, invoking
059: * the {@link #addAll(Collection)} method on an unmodifiable collection may,
060: * but is not required to, throw the exception if the collection to be added
061: * is empty.
062: *
063: * <p>Some collection implementations have restrictions on the elements that
064: * they may contain. For example, some implementations prohibit null elements,
065: * and some have restrictions on the types of their elements. Attempting to
066: * add an ineligible element throws an unchecked exception, typically
067: * <tt>NullPointerException</tt> or <tt>ClassCastException</tt>. Attempting
068: * to query the presence of an ineligible element may throw an exception,
069: * or it may simply return false; some implementations will exhibit the former
070: * behavior and some will exhibit the latter. More generally, attempting an
071: * operation on an ineligible element whose completion would not result in
072: * the insertion of an ineligible element into the collection may throw an
073: * exception or it may succeed, at the option of the implementation.
074: * Such exceptions are marked as "optional" in the specification for this
075: * interface.
076: *
077: * <p>It is up to each collection to determine its own synchronization
078: * policy. In the absence of a stronger guarantee by the
079: * implementation, undefined behavior may result from the invocation
080: * of any method on a collection that is being mutated by another
081: * thread; this includes direct invocations, passing the collection to
082: * a method that might perform invocations, and using an existing
083: * iterator to examine the collection.
084: *
085: * <p>Many methods in Collections Framework interfaces are defined in
086: * terms of the {@link Object#equals(Object) equals} method. For example,
087: * the specification for the {@link #contains(Object) contains(Object o)}
088: * method says: "returns <tt>true</tt> if and only if this collection
089: * contains at least one element <tt>e</tt> such that
090: * <tt>(o==null ? e==null : o.equals(e))</tt>." This specification should
091: * <i>not</i> be construed to imply that invoking <tt>Collection.contains</tt>
092: * with a non-null argument <tt>o</tt> will cause <tt>o.equals(e)</tt> to be
093: * invoked for any element <tt>e</tt>. Implementations are free to implement
094: * optimizations whereby the <tt>equals</tt> invocation is avoided, for
095: * example, by first comparing the hash codes of the two elements. (The
096: * {@link Object#hashCode()} specification guarantees that two objects with
097: * unequal hash codes cannot be equal.) More generally, implementations of
098: * the various Collections Framework interfaces are free to take advantage of
099: * the specified behavior of underlying {@link Object} methods wherever the
100: * implementor deems it appropriate.
101: *
102: * <p>This interface is a member of the
103: * <a href="{@docRoot}/../technotes/guides/collections/index.html">
104: * Java Collections Framework</a>.
105: *
106: * @author Josh Bloch
107: * @author Neal Gafter
108: * @version 1.61, 05/05/07
109: * @see Set
110: * @see List
111: * @see Map
112: * @see SortedSet
113: * @see SortedMap
114: * @see HashSet
115: * @see TreeSet
116: * @see ArrayList
117: * @see LinkedList
118: * @see Vector
119: * @see Collections
120: * @see Arrays
121: * @see AbstractCollection
122: * @since 1.2
123: */
124:
125: public interface Collection<E> extends Iterable<E> {
126: // Query Operations
127:
128: /**
129: * Returns the number of elements in this collection. If this collection
130: * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
131: * <tt>Integer.MAX_VALUE</tt>.
132: *
133: * @return the number of elements in this collection
134: */
135: int size();
136:
137: /**
138: * Returns <tt>true</tt> if this collection contains no elements.
139: *
140: * @return <tt>true</tt> if this collection contains no elements
141: */
142: boolean isEmpty();
143:
144: /**
145: * Returns <tt>true</tt> if this collection contains the specified element.
146: * More formally, returns <tt>true</tt> if and only if this collection
147: * contains at least one element <tt>e</tt> such that
148: * <tt>(o==null ? e==null : o.equals(e))</tt>.
149: *
150: * @param o element whose presence in this collection is to be tested
151: * @return <tt>true</tt> if this collection contains the specified
152: * element
153: * @throws ClassCastException if the type of the specified element
154: * is incompatible with this collection (optional)
155: * @throws NullPointerException if the specified element is null and this
156: * collection does not permit null elements (optional)
157: */
158: boolean contains(Object o);
159:
160: /**
161: * Returns an iterator over the elements in this collection. There are no
162: * guarantees concerning the order in which the elements are returned
163: * (unless this collection is an instance of some class that provides a
164: * guarantee).
165: *
166: * @return an <tt>Iterator</tt> over the elements in this collection
167: */
168: Iterator<E> iterator();
169:
170: /**
171: * Returns an array containing all of the elements in this collection.
172: * If this collection makes any guarantees as to what order its elements
173: * are returned by its iterator, this method must return the elements in
174: * the same order.
175: *
176: * <p>The returned array will be "safe" in that no references to it are
177: * maintained by this collection. (In other words, this method must
178: * allocate a new array even if this collection is backed by an array).
179: * The caller is thus free to modify the returned array.
180: *
181: * <p>This method acts as bridge between array-based and collection-based
182: * APIs.
183: *
184: * @return an array containing all of the elements in this collection
185: */
186: Object[] toArray();
187:
188: /**
189: * Returns an array containing all of the elements in this collection;
190: * the runtime type of the returned array is that of the specified array.
191: * If the collection fits in the specified array, it is returned therein.
192: * Otherwise, a new array is allocated with the runtime type of the
193: * specified array and the size of this collection.
194: *
195: * <p>If this collection fits in the specified array with room to spare
196: * (i.e., the array has more elements than this collection), the element
197: * in the array immediately following the end of the collection is set to
198: * <tt>null</tt>. (This is useful in determining the length of this
199: * collection <i>only</i> if the caller knows that this collection does
200: * not contain any <tt>null</tt> elements.)
201: *
202: * <p>If this collection makes any guarantees as to what order its elements
203: * are returned by its iterator, this method must return the elements in
204: * the same order.
205: *
206: * <p>Like the {@link #toArray()} method, this method acts as bridge between
207: * array-based and collection-based APIs. Further, this method allows
208: * precise control over the runtime type of the output array, and may,
209: * under certain circumstances, be used to save allocation costs.
210: *
211: * <p>Suppose <tt>x</tt> is a collection known to contain only strings.
212: * The following code can be used to dump the collection into a newly
213: * allocated array of <tt>String</tt>:
214: *
215: * <pre>
216: * String[] y = x.toArray(new String[0]);</pre>
217: *
218: * Note that <tt>toArray(new Object[0])</tt> is identical in function to
219: * <tt>toArray()</tt>.
220: *
221: * @param a the array into which the elements of this collection are to be
222: * stored, if it is big enough; otherwise, a new array of the same
223: * runtime type is allocated for this purpose.
224: * @return an array containing all of the elements in this collection
225: * @throws ArrayStoreException if the runtime type of the specified array
226: * is not a supertype of the runtime type of every element in
227: * this collection
228: * @throws NullPointerException if the specified array is null
229: */
230: <T> T[] toArray(T[] a);
231:
232: // Modification Operations
233:
234: /**
235: * Ensures that this collection contains the specified element (optional
236: * operation). Returns <tt>true</tt> if this collection changed as a
237: * result of the call. (Returns <tt>false</tt> if this collection does
238: * not permit duplicates and already contains the specified element.)<p>
239: *
240: * Collections that support this operation may place limitations on what
241: * elements may be added to this collection. In particular, some
242: * collections will refuse to add <tt>null</tt> elements, and others will
243: * impose restrictions on the type of elements that may be added.
244: * Collection classes should clearly specify in their documentation any
245: * restrictions on what elements may be added.<p>
246: *
247: * If a collection refuses to add a particular element for any reason
248: * other than that it already contains the element, it <i>must</i> throw
249: * an exception (rather than returning <tt>false</tt>). This preserves
250: * the invariant that a collection always contains the specified element
251: * after this call returns.
252: *
253: * @param e element whose presence in this collection is to be ensured
254: * @return <tt>true</tt> if this collection changed as a result of the
255: * call
256: * @throws UnsupportedOperationException if the <tt>add</tt> operation
257: * is not supported by this collection
258: * @throws ClassCastException if the class of the specified element
259: * prevents it from being added to this collection
260: * @throws NullPointerException if the specified element is null and this
261: * collection does not permit null elements
262: * @throws IllegalArgumentException if some property of the element
263: * prevents it from being added to this collection
264: * @throws IllegalStateException if the element cannot be added at this
265: * time due to insertion restrictions
266: */
267: boolean add(E e);
268:
269: /**
270: * Removes a single instance of the specified element from this
271: * collection, if it is present (optional operation). More formally,
272: * removes an element <tt>e</tt> such that
273: * <tt>(o==null ? e==null : o.equals(e))</tt>, if
274: * this collection contains one or more such elements. Returns
275: * <tt>true</tt> if this collection contained the specified element (or
276: * equivalently, if this collection changed as a result of the call).
277: *
278: * @param o element to be removed from this collection, if present
279: * @return <tt>true</tt> if an element was removed as a result of this call
280: * @throws ClassCastException if the type of the specified element
281: * is incompatible with this collection (optional)
282: * @throws NullPointerException if the specified element is null and this
283: * collection does not permit null elements (optional)
284: * @throws UnsupportedOperationException if the <tt>remove</tt> operation
285: * is not supported by this collection
286: */
287: boolean remove(Object o);
288:
289: // Bulk Operations
290:
291: /**
292: * Returns <tt>true</tt> if this collection contains all of the elements
293: * in the specified collection.
294: *
295: * @param c collection to be checked for containment in this collection
296: * @return <tt>true</tt> if this collection contains all of the elements
297: * in the specified collection
298: * @throws ClassCastException if the types of one or more elements
299: * in the specified collection are incompatible with this
300: * collection (optional)
301: * @throws NullPointerException if the specified collection contains one
302: * or more null elements and this collection does not permit null
303: * elements (optional), or if the specified collection is null
304: * @see #contains(Object)
305: */
306: boolean containsAll(Collection<?> c);
307:
308: /**
309: * Adds all of the elements in the specified collection to this collection
310: * (optional operation). The behavior of this operation is undefined if
311: * the specified collection is modified while the operation is in progress.
312: * (This implies that the behavior of this call is undefined if the
313: * specified collection is this collection, and this collection is
314: * nonempty.)
315: *
316: * @param c collection containing elements to be added to this collection
317: * @return <tt>true</tt> if this collection changed as a result of the call
318: * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
319: * is not supported by this collection
320: * @throws ClassCastException if the class of an element of the specified
321: * collection prevents it from being added to this collection
322: * @throws NullPointerException if the specified collection contains a
323: * null element and this collection does not permit null elements,
324: * or if the specified collection is null
325: * @throws IllegalArgumentException if some property of an element of the
326: * specified collection prevents it from being added to this
327: * collection
328: * @throws IllegalStateException if not all the elements can be added at
329: * this time due to insertion restrictions
330: * @see #add(Object)
331: */
332: boolean addAll(Collection<? extends E> c);
333:
334: /**
335: * Removes all of this collection's elements that are also contained in the
336: * specified collection (optional operation). After this call returns,
337: * this collection will contain no elements in common with the specified
338: * collection.
339: *
340: * @param c collection containing elements to be removed from this collection
341: * @return <tt>true</tt> if this collection changed as a result of the
342: * call
343: * @throws UnsupportedOperationException if the <tt>removeAll</tt> method
344: * is not supported by this collection
345: * @throws ClassCastException if the types of one or more elements
346: * in this collection are incompatible with the specified
347: * collection (optional)
348: * @throws NullPointerException if this collection contains one or more
349: * null elements and the specified collection does not support
350: * null elements (optional), or if the specified collection is null
351: * @see #remove(Object)
352: * @see #contains(Object)
353: */
354: boolean removeAll(Collection<?> c);
355:
356: /**
357: * Retains only the elements in this collection that are contained in the
358: * specified collection (optional operation). In other words, removes from
359: * this collection all of its elements that are not contained in the
360: * specified collection.
361: *
362: * @param c collection containing elements to be retained in this collection
363: * @return <tt>true</tt> if this collection changed as a result of the call
364: * @throws UnsupportedOperationException if the <tt>retainAll</tt> operation
365: * is not supported by this collection
366: * @throws ClassCastException if the types of one or more elements
367: * in this collection are incompatible with the specified
368: * collection (optional)
369: * @throws NullPointerException if this collection contains one or more
370: * null elements and the specified collection does not permit null
371: * elements (optional), or if the specified collection is null
372: * @see #remove(Object)
373: * @see #contains(Object)
374: */
375: boolean retainAll(Collection<?> c);
376:
377: /**
378: * Removes all of the elements from this collection (optional operation).
379: * The collection will be empty after this method returns.
380: *
381: * @throws UnsupportedOperationException if the <tt>clear</tt> operation
382: * is not supported by this collection
383: */
384: void clear();
385:
386: // Comparison and hashing
387:
388: /**
389: * Compares the specified object with this collection for equality. <p>
390: *
391: * While the <tt>Collection</tt> interface adds no stipulations to the
392: * general contract for the <tt>Object.equals</tt>, programmers who
393: * implement the <tt>Collection</tt> interface "directly" (in other words,
394: * create a class that is a <tt>Collection</tt> but is not a <tt>Set</tt>
395: * or a <tt>List</tt>) must exercise care if they choose to override the
396: * <tt>Object.equals</tt>. It is not necessary to do so, and the simplest
397: * course of action is to rely on <tt>Object</tt>'s implementation, but
398: * the implementor may wish to implement a "value comparison" in place of
399: * the default "reference comparison." (The <tt>List</tt> and
400: * <tt>Set</tt> interfaces mandate such value comparisons.)<p>
401: *
402: * The general contract for the <tt>Object.equals</tt> method states that
403: * equals must be symmetric (in other words, <tt>a.equals(b)</tt> if and
404: * only if <tt>b.equals(a)</tt>). The contracts for <tt>List.equals</tt>
405: * and <tt>Set.equals</tt> state that lists are only equal to other lists,
406: * and sets to other sets. Thus, a custom <tt>equals</tt> method for a
407: * collection class that implements neither the <tt>List</tt> nor
408: * <tt>Set</tt> interface must return <tt>false</tt> when this collection
409: * is compared to any list or set. (By the same logic, it is not possible
410: * to write a class that correctly implements both the <tt>Set</tt> and
411: * <tt>List</tt> interfaces.)
412: *
413: * @param o object to be compared for equality with this collection
414: * @return <tt>true</tt> if the specified object is equal to this
415: * collection
416: *
417: * @see Object#equals(Object)
418: * @see Set#equals(Object)
419: * @see List#equals(Object)
420: */
421: boolean equals(Object o);
422:
423: /**
424: * Returns the hash code value for this collection. While the
425: * <tt>Collection</tt> interface adds no stipulations to the general
426: * contract for the <tt>Object.hashCode</tt> method, programmers should
427: * take note that any class that overrides the <tt>Object.equals</tt>
428: * method must also override the <tt>Object.hashCode</tt> method in order
429: * to satisfy the general contract for the <tt>Object.hashCode</tt>method.
430: * In particular, <tt>c1.equals(c2)</tt> implies that
431: * <tt>c1.hashCode()==c2.hashCode()</tt>.
432: *
433: * @return the hash code value for this collection
434: *
435: * @see Object#hashCode()
436: * @see Object#equals(Object)
437: */
438: int hashCode();
439: }
|