001: /*
002: * @(#)List.java 1.39 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.util;
029:
030: /**
031: * An ordered collection (also known as a <i>sequence</i>). The user of this
032: * interface has precise control over where in the list each element is
033: * inserted. The user can access elements by their integer index (position in
034: * the list), and search for elements in the list.<p>
035: *
036: * Unlike sets, lists typically allow duplicate elements. More formally,
037: * lists typically allow pairs of elements <tt>e1</tt> and <tt>e2</tt>
038: * such that <tt>e1.equals(e2)</tt>, and they typically allow multiple
039: * null elements if they allow null elements at all. It is not inconceivable
040: * that someone might wish to implement a list that prohibits duplicates, by
041: * throwing runtime exceptions when the user attempts to insert them, but we
042: * expect this usage to be rare.<p>
043: *
044: * The <tt>List</tt> interface places additional stipulations, beyond those
045: * specified in the <tt>Collection</tt> interface, on the contracts of the
046: * <tt>iterator</tt>, <tt>add</tt>, <tt>remove</tt>, <tt>equals</tt>, and
047: * <tt>hashCode</tt> methods. Declarations for other inherited methods are
048: * also included here for convenience.<p>
049: *
050: * The <tt>List</tt> interface provides four methods for positional (indexed)
051: * access to list elements. Lists (like Java arrays) are zero based. Note
052: * that these operations may execute in time proportional to the index value
053: * for some implementations (the <tt>LinkedList</tt> class, for
054: * example). Thus, iterating over the elements in a list is typically
055: * preferable to indexing through it if the caller does not know the
056: * implementation.<p>
057: *
058: * The <tt>List</tt> interface provides a special iterator, called a
059: * <tt>ListIterator</tt>, that allows element insertion and replacement, and
060: * bidirectional access in addition to the normal operations that the
061: * <tt>Iterator</tt> interface provides. A method is provided to obtain a
062: * list iterator that starts at a specified position in the list.<p>
063: *
064: * The <tt>List</tt> interface provides two methods to search for a specified
065: * object. From a performance standpoint, these methods should be used with
066: * caution. In many implementations they will perform costly linear
067: * searches.<p>
068: *
069: * The <tt>List</tt> interface provides two methods to efficiently insert and
070: * remove multiple elements at an arbitrary point in the list.<p>
071: *
072: * Note: While it is permissible for lists to contain themselves as elements,
073: * extreme caution is advised: the <tt>equals</tt> and <tt>hashCode</tt>
074: * methods are no longer well defined on a such a list.
075: *
076: * <p>Some list implementations have restrictions on the elements that
077: * they may contain. For example, some implementations prohibit null elements,
078: * and some have restrictions on the types of their elements. Attempting to
079: * add an ineligible element throws an unchecked exception, typically
080: * <tt>NullPointerException</tt> or <tt>ClassCastException</tt>. Attempting
081: * to query the presence of an ineligible element may throw an exception,
082: * or it may simply return false; some implementations will exhibit the former
083: * behavior and some will exhibit the latter. More generally, attempting an
084: * operation on an ineligible element whose completion would not result in
085: * the insertion of an ineligible element into the list may throw an
086: * exception or it may succeed, at the option of the implementation.
087: * Such exceptions are marked as "optional" in the specification for this
088: * interface.
089: *
090: * <p>This interface is a member of the
091: * <a href="{@docRoot}/../guide/collections/index.html">
092: * Java Collections Framework</a>.
093: *
094: * @author Josh Bloch
095: * @version 1.32, 02/02/00
096: * @see Collection
097: * @see Set
098: * @see ArrayList
099: * @see LinkedList
100: * @see Vector
101: * @see Arrays#asList(Object[])
102: * @see Collections#nCopies(int, Object)
103: * @see Collections#EMPTY_LIST
104: * @see AbstractList
105: * @see AbstractSequentialList
106: * @since 1.2
107: */
108:
109: public interface List extends Collection {
110: // Query Operations
111:
112: /**
113: * Returns the number of elements in this list. If this list contains
114: * more than <tt>Integer.MAX_VALUE</tt> elements, returns
115: * <tt>Integer.MAX_VALUE</tt>.
116: *
117: * @return the number of elements in this list.
118: */
119: int size();
120:
121: /**
122: * Returns <tt>true</tt> if this list contains no elements.
123: *
124: * @return <tt>true</tt> if this list contains no elements.
125: */
126: boolean isEmpty();
127:
128: /**
129: *
130: * Returns <tt>true</tt> if this list contains the specified element.
131: * More formally, returns <tt>true</tt> if and only if this list contains
132: * at least one element <tt>e</tt> such that
133: * <tt>(o==null ? e==null : o.equals(e))</tt>.
134: *
135: * @param o element whose presence in this list is to be tested.
136: * @return <tt>true</tt> if this list contains the specified element.
137: * @throws ClassCastException if the type of the specified element
138: * is incompatible with this list (optional).
139: * @throws NullPointerException if the specified element is null and this
140: * list does not support null elements (optional).
141: */
142: boolean contains(Object o);
143:
144: /**
145: * Returns an iterator over the elements in this list in proper sequence.
146: *
147: * @return an iterator over the elements in this list in proper sequence.
148: */
149: Iterator iterator();
150:
151: /**
152: * Returns an array containing all of the elements in this list in proper
153: * sequence. Obeys the general contract of the
154: * <tt>Collection.toArray</tt> method.
155: *
156: * @return an array containing all of the elements in this list in proper
157: * sequence.
158: * @see Arrays#asList(Object[])
159: */
160: Object[] toArray();
161:
162: /**
163: * Returns an array containing all of the elements in this list in proper
164: * sequence; the runtime type of the returned array is that of the
165: * specified array. Obeys the general contract of the
166: * <tt>Collection.toArray(Object[])</tt> method.
167: *
168: * @param a the array into which the elements of this list are to
169: * be stored, if it is big enough; otherwise, a new array of the
170: * same runtime type is allocated for this purpose.
171: * @return an array containing the elements of this list.
172: *
173: * @throws ArrayStoreException if the runtime type of the specified array
174: * is not a supertype of the runtime type of every element in
175: * this list.
176: * @throws NullPointerException if the specified array is <tt>null</tt>.
177: */
178: Object[] toArray(Object a[]);
179:
180: // Modification Operations
181:
182: /**
183: * Appends the specified element to the end of this list (optional
184: * operation). <p>
185: *
186: * Lists that support this operation may place limitations on what
187: * elements may be added to this list. In particular, some
188: * lists will refuse to add null elements, and others will impose
189: * restrictions on the type of elements that may be added. List
190: * classes should clearly specify in their documentation any restrictions
191: * on what elements may be added.
192: *
193: * @param o element to be appended to this list.
194: * @return <tt>true</tt> (as per the general contract of the
195: * <tt>Collection.add</tt> method).
196: *
197: * @throws UnsupportedOperationException if the <tt>add</tt> method is not
198: * supported by this list.
199: * @throws ClassCastException if the class of the specified element
200: * prevents it from being added to this list.
201: * @throws NullPointerException if the specified element is null and this
202: * list does not support null elements.
203: * @throws IllegalArgumentException if some aspect of this element
204: * prevents it from being added to this list.
205: */
206: boolean add(Object o);
207:
208: /**
209: * Removes the first occurrence in this list of the specified element
210: * (optional operation). If this list does not contain the element, it is
211: * unchanged. More formally, removes the element with the lowest index i
212: * such that <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> (if
213: * such an element exists).
214: *
215: * @param o element to be removed from this list, if present.
216: * @return <tt>true</tt> if this list contained the specified element.
217: * @throws ClassCastException if the type of the specified element
218: * is incompatible with this list (optional).
219: * @throws NullPointerException if the specified element is null and this
220: * list does not support null elements (optional).
221: * @throws UnsupportedOperationException if the <tt>remove</tt> method is
222: * not supported by this list.
223: */
224: boolean remove(Object o);
225:
226: // Bulk Modification Operations
227:
228: /**
229: *
230: * Returns <tt>true</tt> if this list contains all of the elements of the
231: * specified collection.
232: *
233: * @param c collection to be checked for containment in this list.
234: * @return <tt>true</tt> if this list contains all of the elements of the
235: * specified collection.
236: * @throws ClassCastException if the types of one or more elements
237: * in the specified collection are incompatible with this
238: * list (optional).
239: * @throws NullPointerException if the specified collection contains one
240: * or more null elements and this list does not support null
241: * elements (optional).
242: * @throws NullPointerException if the specified collection is
243: * <tt>null</tt>.
244: * @see #contains(Object)
245: */
246: boolean containsAll(Collection c);
247:
248: /**
249: * Appends all of the elements in the specified collection to the end of
250: * this list, in the order that they are returned by the specified
251: * collection's iterator (optional operation). The behavior of this
252: * operation is unspecified if the specified collection is modified while
253: * the operation is in progress. (Note that this will occur if the
254: * specified collection is this list, and it's nonempty.)
255: *
256: * @param c collection whose elements are to be added to this list.
257: * @return <tt>true</tt> if this list changed as a result of the call.
258: *
259: * @throws UnsupportedOperationException if the <tt>addAll</tt> method is
260: * not supported by this list.
261: * @throws ClassCastException if the class of an element in the specified
262: * collection prevents it from being added to this list.
263: * @throws NullPointerException if the specified collection contains one
264: * or more null elements and this list does not support null
265: * elements, or if the specified collection is <tt>null</tt>.
266: * @throws IllegalArgumentException if some aspect of an element in the
267: * specified collection prevents it from being added to this
268: * list.
269: * @see #add(Object)
270: */
271: boolean addAll(Collection c);
272:
273: /**
274: * Inserts all of the elements in the specified collection into this
275: * list at the specified position (optional operation). Shifts the
276: * element currently at that position (if any) and any subsequent
277: * elements to the right (increases their indices). The new elements
278: * will appear in this list in the order that they are returned by the
279: * specified collection's iterator. The behavior of this operation is
280: * unspecified if the specified collection is modified while the
281: * operation is in progress. (Note that this will occur if the specified
282: * collection is this list, and it's nonempty.)
283: *
284: * @param index index at which to insert first element from the specified
285: * collection.
286: * @param c elements to be inserted into this list.
287: * @return <tt>true</tt> if this list changed as a result of the call.
288: *
289: * @throws UnsupportedOperationException if the <tt>addAll</tt> method is
290: * not supported by this list.
291: * @throws ClassCastException if the class of one of elements of the
292: * specified collection prevents it from being added to this
293: * list.
294: * @throws NullPointerException if the specified collection contains one
295: * or more null elements and this list does not support null
296: * elements, or if the specified collection is <tt>null</tt>.
297: * @throws IllegalArgumentException if some aspect of one of elements of
298: * the specified collection prevents it from being added to
299: * this list.
300: * @throws IndexOutOfBoundsException if the index is out of range (index
301: * < 0 || index > size()).
302: */
303: boolean addAll(int index, Collection c);
304:
305: /**
306: * Removes from this list all the elements that are contained in the
307: * specified collection (optional operation).
308: *
309: * @param c collection that defines which elements will be removed from
310: * this list.
311: * @return <tt>true</tt> if this list changed as a result of the call.
312: *
313: * @throws UnsupportedOperationException if the <tt>removeAll</tt> method
314: * is not supported by this list.
315: * @throws ClassCastException if the types of one or more elements
316: * in this list are incompatible with the specified
317: * collection (optional).
318: * @throws NullPointerException if this list contains one or more
319: * null elements and the specified collection does not support
320: * null elements (optional).
321: * @throws NullPointerException if the specified collection is
322: * <tt>null</tt>.
323: * @see #remove(Object)
324: * @see #contains(Object)
325: */
326: boolean removeAll(Collection c);
327:
328: /**
329: * Retains only the elements in this list that are contained in the
330: * specified collection (optional operation). In other words, removes
331: * from this list all the elements that are not contained in the specified
332: * collection.
333: *
334: * @param c collection that defines which elements this set will retain.
335: *
336: * @return <tt>true</tt> if this list changed as a result of the call.
337: *
338: * @throws UnsupportedOperationException if the <tt>retainAll</tt> method
339: * is not supported by this list.
340: * @throws ClassCastException if the types of one or more elements
341: * in this list are incompatible with the specified
342: * collection (optional).
343: * @throws NullPointerException if this list contains one or more
344: * null elements and the specified collection does not support
345: * null elements (optional).
346: * @throws NullPointerException if the specified collection is
347: * <tt>null</tt>.
348: * @see #remove(Object)
349: * @see #contains(Object)
350: */
351: boolean retainAll(Collection c);
352:
353: /**
354: * Removes all of the elements from this list (optional operation). This
355: * list will be empty after this call returns (unless it throws an
356: * exception).
357: *
358: * @throws UnsupportedOperationException if the <tt>clear</tt> method is
359: * not supported by this list.
360: */
361: void clear();
362:
363: // Comparison and hashing
364:
365: /**
366: * Compares the specified object with this list for equality. Returns
367: * <tt>true</tt> if and only if the specified object is also a list, both
368: * lists have the same size, and all corresponding pairs of elements in
369: * the two lists are <i>equal</i>. (Two elements <tt>e1</tt> and
370: * <tt>e2</tt> are <i>equal</i> if <tt>(e1==null ? e2==null :
371: * e1.equals(e2))</tt>.) In other words, two lists are defined to be
372: * equal if they contain the same elements in the same order. This
373: * definition ensures that the equals method works properly across
374: * different implementations of the <tt>List</tt> interface.
375: *
376: * @param o the object to be compared for equality with this list.
377: * @return <tt>true</tt> if the specified object is equal to this list.
378: */
379: boolean equals(Object o);
380:
381: /**
382: * Returns the hash code value for this list. The hash code of a list
383: * is defined to be the result of the following calculation:
384: * <pre>
385: * hashCode = 1;
386: * Iterator i = list.iterator();
387: * while (i.hasNext()) {
388: * Object obj = i.next();
389: * hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
390: * }
391: * </pre>
392: * This ensures that <tt>list1.equals(list2)</tt> implies that
393: * <tt>list1.hashCode()==list2.hashCode()</tt> for any two lists,
394: * <tt>list1</tt> and <tt>list2</tt>, as required by the general
395: * contract of <tt>Object.hashCode</tt>.
396: *
397: * @return the hash code value for this list.
398: * @see Object#hashCode()
399: * @see Object#equals(Object)
400: * @see #equals(Object)
401: */
402: int hashCode();
403:
404: // Positional Access Operations
405:
406: /**
407: * Returns the element at the specified position in this list.
408: *
409: * @param index index of element to return.
410: * @return the element at the specified position in this list.
411: *
412: * @throws IndexOutOfBoundsException if the index is out of range (index
413: * < 0 || index >= size()).
414: */
415: Object get(int index);
416:
417: /**
418: * Replaces the element at the specified position in this list with the
419: * specified element (optional operation).
420: *
421: * @param index index of element to replace.
422: * @param element element to be stored at the specified position.
423: * @return the element previously at the specified position.
424: *
425: * @throws UnsupportedOperationException if the <tt>set</tt> method is not
426: * supported by this list.
427: * @throws ClassCastException if the class of the specified element
428: * prevents it from being added to this list.
429: * @throws NullPointerException if the specified element is null and
430: * this list does not support null elements.
431: * @throws IllegalArgumentException if some aspect of the specified
432: * element prevents it from being added to this list.
433: * @throws IndexOutOfBoundsException if the index is out of range
434: * (index < 0 || index >= size()).
435: */
436: Object set(int index, Object element);
437:
438: /**
439: * Inserts the specified element at the specified position in this list
440: * (optional operation). Shifts the element currently at that position
441: * (if any) and any subsequent elements to the right (adds one to their
442: * indices).
443: *
444: * @param index index at which the specified element is to be inserted.
445: * @param element element to be inserted.
446: *
447: * @throws UnsupportedOperationException if the <tt>add</tt> method is not
448: * supported by this list.
449: * @throws ClassCastException if the class of the specified element
450: * prevents it from being added to this list.
451: * @throws NullPointerException if the specified element is null and
452: * this list does not support null elements.
453: * @throws IllegalArgumentException if some aspect of the specified
454: * element prevents it from being added to this list.
455: * @throws IndexOutOfBoundsException if the index is out of range
456: * (index < 0 || index > size()).
457: */
458: void add(int index, Object element);
459:
460: /**
461: * Removes the element at the specified position in this list (optional
462: * operation). Shifts any subsequent elements to the left (subtracts one
463: * from their indices). Returns the element that was removed from the
464: * list.
465: *
466: * @param index the index of the element to removed.
467: * @return the element previously at the specified position.
468: *
469: * @throws UnsupportedOperationException if the <tt>remove</tt> method is
470: * not supported by this list.
471: * @throws IndexOutOfBoundsException if the index is out of range (index
472: * < 0 || index >= size()).
473: */
474: Object remove(int index);
475:
476: // Search Operations
477:
478: /**
479: * Returns the index in this list of the first occurrence of the specified
480: * element, or -1 if this list does not contain this element.
481: * More formally, returns the lowest index <tt>i</tt> such that
482: * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
483: * or -1 if there is no such index.
484: *
485: * @param o element to search for.
486: * @return the index in this list of the first occurrence of the specified
487: * element, or -1 if this list does not contain this element.
488: * @throws ClassCastException if the type of the specified element
489: * is incompatible with this list (optional).
490: * @throws NullPointerException if the specified element is null and this
491: * list does not support null elements (optional).
492: */
493: int indexOf(Object o);
494:
495: /**
496: * Returns the index in this list of the last occurrence of the specified
497: * element, or -1 if this list does not contain this element.
498: * More formally, returns the highest index <tt>i</tt> such that
499: * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
500: * or -1 if there is no such index.
501: *
502: * @param o element to search for.
503: * @return the index in this list of the last occurrence of the specified
504: * element, or -1 if this list does not contain this element.
505: * @throws ClassCastException if the type of the specified element
506: * is incompatible with this list (optional).
507: * @throws NullPointerException if the specified element is null and this
508: * list does not support null elements (optional).
509: */
510: int lastIndexOf(Object o);
511:
512: // List Iterators
513:
514: /**
515: * Returns a list iterator of the elements in this list (in proper
516: * sequence).
517: *
518: * @return a list iterator of the elements in this list (in proper
519: * sequence).
520: */
521: ListIterator listIterator();
522:
523: /**
524: * Returns a list iterator of the elements in this list (in proper
525: * sequence), starting at the specified position in this list. The
526: * specified index indicates the first element that would be returned by
527: * an initial call to the <tt>next</tt> method. An initial call to
528: * the <tt>previous</tt> method would return the element with the
529: * specified index minus one.
530: *
531: * @param index index of first element to be returned from the
532: * list iterator (by a call to the <tt>next</tt> method).
533: * @return a list iterator of the elements in this list (in proper
534: * sequence), starting at the specified position in this list.
535: * @throws IndexOutOfBoundsException if the index is out of range (index
536: * < 0 || index > size()).
537: */
538: ListIterator listIterator(int index);
539:
540: // View
541:
542: /**
543: * Returns a view of the portion of this list between the specified
544: * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive. (If
545: * <tt>fromIndex</tt> and <tt>toIndex</tt> are equal, the returned list is
546: * empty.) The returned list is backed by this list, so non-structural
547: * changes in the returned list are reflected in this list, and vice-versa.
548: * The returned list supports all of the optional list operations supported
549: * by this list.<p>
550: *
551: * This method eliminates the need for explicit range operations (of
552: * the sort that commonly exist for arrays). Any operation that expects
553: * a list can be used as a range operation by passing a subList view
554: * instead of a whole list. For example, the following idiom
555: * removes a range of elements from a list:
556: * <pre>
557: * list.subList(from, to).clear();
558: * </pre>
559: * Similar idioms may be constructed for <tt>indexOf</tt> and
560: * <tt>lastIndexOf</tt>, and all of the algorithms in the
561: * <tt>Collections</tt> class can be applied to a subList.<p>
562: *
563: * The semantics of the list returned by this method become undefined if
564: * the backing list (i.e., this list) is <i>structurally modified</i> in
565: * any way other than via the returned list. (Structural modifications are
566: * those that change the size of this list, or otherwise perturb it in such
567: * a fashion that iterations in progress may yield incorrect results.)
568: *
569: * @param fromIndex low endpoint (inclusive) of the subList.
570: * @param toIndex high endpoint (exclusive) of the subList.
571: * @return a view of the specified range within this list.
572: *
573: * @throws IndexOutOfBoundsException for an illegal endpoint index value
574: * (fromIndex < 0 || toIndex > size || fromIndex > toIndex).
575: */
576: List subList(int fromIndex, int toIndex);
577: }
|