List.java 17.8 KB
Newer Older
Bryce McKinlay committed
1
/* List.java -- An ordered collection which allows indexed access
Bryce McKinlay committed
2
   Copyright (C) 1998, 2001 Free Software Foundation, Inc.
Warren Levy committed
3

Bryce McKinlay committed
4
This file is part of GNU Classpath.
Warren Levy committed
5

Bryce McKinlay committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */
Bryce McKinlay committed
37 38


Warren Levy committed
39 40 41
package java.util;

/**
Bryce McKinlay committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
 * An ordered collection (also known as a list). This collection allows
 * access to elements by position, as well as control on where elements
 * are inserted. Unlike sets, duplicate elements are permitted by this
 * general contract (if a subclass forbids duplicates, this should be
 * documented).
 * <p>
 *
 * List places additional requirements on <code>iterator</code>,
 * <code>add</code>, <code>remove</code>, <code>equals</code>, and
 * <code>hashCode</code>, in addition to requiring more methods. List
 * indexing is 0-based (like arrays), although some implementations may
 * require time proportional to the index to obtain an arbitrary element.
 * The List interface is incompatible with Set; you cannot implement both
 * simultaneously.
 * <p>
 *
 * Lists also provide a <code>ListIterator</code> which allows bidirectional
 * traversal and other features atop regular iterators. Lists can be
 * searched for arbitrary elements, and allow easy insertion and removal
 * of multiple elements in one method call.
 * <p>
 *
 * Note: While lists may contain themselves as elements, this leads to
 * undefined (usually infinite recursive) behavior for some methods like
 * hashCode or equals.
 *
 * @author Original author unknown
 * @author Eric Blake <ebb9@email.byu.edu>
 * @see Collection
 * @see Set
 * @see ArrayList
 * @see LinkedList
 * @see Vector
 * @see Arrays#asList(Object[])
 * @see Collections#nCopies(int, Object)
 * @see Collections#EMPTY_LIST
 * @see AbstractList
 * @see AbstractSequentialList
 * @since 1.2
 * @status updated to 1.4
Warren Levy committed
82 83 84
 */
public interface List extends Collection
{
Bryce McKinlay committed
85
  /**
Bryce McKinlay committed
86 87 88 89
   * Insert an element into the list at a given position (optional operation).
   * This shifts all existing elements from that position to the end one
   * index to the right. This version of add has no return, since it is
   * assumed to always succeed if there is no exception.
Bryce McKinlay committed
90
   *
Bryce McKinlay committed
91 92 93 94 95 96 97 98 99
   * @param index the location to insert the item
   * @param o the object to insert
   * @throws UnsupportedOperationException if this list does not support the
   *         add operation
   * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
   * @throws ClassCastException if o cannot be added to this list due to its
   *         type
   * @throws IllegalArgumentException if o cannot be added to this list for
   *         some other reason
100 101
   * @throws NullPointerException if o is null and this list doesn't support
   *         the addition of null values.
Bryce McKinlay committed
102 103 104 105
   */
  void add(int index, Object o);

  /**
Bryce McKinlay committed
106 107 108
   * Add an element to the end of the list (optional operation). If the list
   * imposes restraints on what can be inserted, such as no null elements,
   * this should be documented.
Bryce McKinlay committed
109
   *
Bryce McKinlay committed
110 111 112 113 114 115 116 117
   * @param o the object to add
   * @return true, as defined by Collection for a modified list
   * @throws UnsupportedOperationException if this list does not support the
   *         add operation
   * @throws ClassCastException if o cannot be added to this list due to its
   *         type
   * @throws IllegalArgumentException if o cannot be added to this list for
   *         some other reason
118 119
   * @throws NullPointerException if o is null and this list doesn't support
   *         the addition of null values.
Bryce McKinlay committed
120 121 122 123
   */
  boolean add(Object o);

  /**
Bryce McKinlay committed
124 125 126 127 128
   * Insert the contents of a collection into the list at a given position
   * (optional operation). Shift all elements at that position to the right
   * by the number of elements inserted. This operation is undefined if
   * this list is modified during the operation (for example, if you try
   * to insert a list into itself).
Bryce McKinlay committed
129
   *
Bryce McKinlay committed
130 131 132 133 134 135 136 137 138 139 140
   * @param index the location to insert the collection
   * @param c the collection to insert
   * @return true if the list was modified by this action, that is, if c is
   *         non-empty
   * @throws UnsupportedOperationException if this list does not support the
   *         addAll operation
   * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
   * @throws ClassCastException if some element of c cannot be added to this
   *         list due to its type
   * @throws IllegalArgumentException if some element of c cannot be added
   *         to this list for some other reason
141 142
   * @throws NullPointerException if some element of c is null and this list
   *         doesn't support the addition of null values.
Bryce McKinlay committed
143 144
   * @throws NullPointerException if the specified collection is null
   * @see #add(int, Object)
Bryce McKinlay committed
145 146 147 148
   */
  boolean addAll(int index, Collection c);

  /**
Bryce McKinlay committed
149 150 151 152
   * Add the contents of a collection to the end of the list (optional
   * operation).  This operation is undefined if this list is modified
   * during the operation (for example, if you try to insert a list into
   * itself).
Bryce McKinlay committed
153
   *
Bryce McKinlay committed
154 155 156 157 158 159 160 161 162 163
   * @param c the collection to add
   * @return true if the list was modified by this action, that is, if c is
   *         non-empty
   * @throws UnsupportedOperationException if this list does not support the
   *         addAll operation
   * @throws ClassCastException if some element of c cannot be added to this
   *         list due to its type
   * @throws IllegalArgumentException if some element of c cannot be added
   *         to this list for some other reason
   * @throws NullPointerException if the specified collection is null
164 165
   * @throws NullPointerException if some element of c is null and this list
   *         doesn't support the addition of null values.
Bryce McKinlay committed
166
   * @see #add(Object)
Bryce McKinlay committed
167 168 169 170 171
   */
  boolean addAll(Collection c);

  /**
   * Clear the list, such that a subsequent call to isEmpty() would return
Bryce McKinlay committed
172
   * true (optional operation).
Bryce McKinlay committed
173
   *
Bryce McKinlay committed
174 175
   * @throws UnsupportedOperationException if this list does not support the
   *         clear operation
Bryce McKinlay committed
176 177 178 179 180
   */
  void clear();

  /**
   * Test whether this list contains a given object as one of its elements.
Bryce McKinlay committed
181 182
   * This is defined as the existence of an element e such that
   * <code>o == null ? e == null : o.equals(e)</code>.
Bryce McKinlay committed
183
   *
Bryce McKinlay committed
184 185
   * @param o the element to look for
   * @return true if this list contains the element
186 187 188 189
   * @throws ClassCastException if the type of o is not a valid type
   *         for this list.
   * @throws NullPointerException if o is null and the list doesn't
   *         support null values.
Bryce McKinlay committed
190 191 192 193 194 195
   */
  boolean contains(Object o);

  /**
   * Test whether this list contains every element in a given collection.
   *
Bryce McKinlay committed
196 197 198
   * @param c the collection to test for
   * @return true if for every element o in c, contains(o) would return true
   * @throws NullPointerException if the collection is null
199 200 201 202
   * @throws ClassCastException if the type of any element in c is not a valid
   *         type for this list.
   * @throws NullPointerException if some element of c is null and this
   *         list does not support null values.
Bryce McKinlay committed
203
   * @see #contains(Object)
Bryce McKinlay committed
204 205 206 207 208 209
   */
  boolean containsAll(Collection c);

  /**
   * Test whether this list is equal to another object. A List is defined to be
   * equal to an object if and only if that object is also a List, and the two
Bryce McKinlay committed
210
   * lists have the same sequence. Two lists l1 and l2 are equal if and only
Bryce McKinlay committed
211 212 213 214
   * if <code>l1.size() == l2.size()</code>, and for every integer n between 0
   * and <code>l1.size() - 1</code> inclusive, <code>l1.get(n) == null ?
   * l2.get(n) == null : l1.get(n).equals(l2.get(n))</code>.
   *
Bryce McKinlay committed
215 216 217 218
   * @param o the object to test for equality with this list
   * @return true if o is equal to this list
   * @see Object#equals(Object)
   * @see #hashCode()
Bryce McKinlay committed
219
   */
220
  boolean equals(Object o);
Bryce McKinlay committed
221 222 223 224

  /**
   * Get the element at a given index in this list.
   *
Bryce McKinlay committed
225 226 227
   * @param index the index of the element to be returned
   * @return the element at index index in this list
   * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
Bryce McKinlay committed
228 229 230 231
   */
  Object get(int index);

  /**
232 233 234 235 236 237 238 239 240 241 242 243 244 245
   * Obtains a hash code for this list. In order to obey the general
   * contract of the hashCode method of class Object, this value is
   * calculated as follows:
   * 
<p><pre>hashCode = 1;
Iterator i = list.iterator();
while (i.hasNext())
{
  Object obj = i.next();
  hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
}</pre>
   *
   * <p>This ensures that the general contract of Object.hashCode()
   * is adhered to.
Bryce McKinlay committed
246
   *
Bryce McKinlay committed
247 248 249
   * @return the hash code of this list
   * @see Object#hashCode()
   * @see #equals(Object)
Bryce McKinlay committed
250 251 252 253 254 255 256
   */
  int hashCode();

  /**
   * Obtain the first index at which a given object is to be found in this
   * list.
   *
Bryce McKinlay committed
257 258
   * @param o the object to search for
   * @return the least integer n such that <code>o == null ? get(n) == null :
259 260 261 262 263
   *         o.equals(get(n))</code>, or -1 if there is no such index.
   * @throws ClassCastException if the type of o is not a valid
   *         type for this list.
   * @throws NullPointerException if o is null and this
   *         list does not support null values.
Bryce McKinlay committed
264 265 266 267 268 269
   */
  int indexOf(Object o);

  /**
   * Test whether this list is empty, that is, if size() == 0.
   *
Bryce McKinlay committed
270
   * @return true if this list contains no elements
Bryce McKinlay committed
271 272 273 274
   */
  boolean isEmpty();

  /**
Bryce McKinlay committed
275
   * Obtain an Iterator over this list, whose sequence is the list order.
Bryce McKinlay committed
276
   *
Bryce McKinlay committed
277
   * @return an Iterator over the elements of this list, in order
Bryce McKinlay committed
278 279 280 281 282 283 284
   */
  Iterator iterator();

  /**
   * Obtain the last index at which a given object is to be found in this
   * list.
   *
Bryce McKinlay committed
285
   * @return the greatest integer n such that <code>o == null ? get(n) == null
286 287 288 289 290
   *         : o.equals(get(n))</code>, or -1 if there is no such index.
   * @throws ClassCastException if the type of o is not a valid
   *         type for this list.
   * @throws NullPointerException if o is null and this
   *         list does not support null values.
Bryce McKinlay committed
291 292 293 294 295 296
   */
  int lastIndexOf(Object o);

  /**
   * Obtain a ListIterator over this list, starting at the beginning.
   *
Bryce McKinlay committed
297 298
   * @return a ListIterator over the elements of this list, in order, starting
   *         at the beginning
Bryce McKinlay committed
299 300 301 302 303
   */
  ListIterator listIterator();

  /**
   * Obtain a ListIterator over this list, starting at a given position.
Bryce McKinlay committed
304 305
   * A first call to next() would return the same as get(index), and a
   * first call to previous() would return the same as get(index - 1).
Bryce McKinlay committed
306 307
   *
   * @param index the position, between 0 and size() inclusive, to begin the
Bryce McKinlay committed
308 309 310 311
   *        iteration from
   * @return a ListIterator over the elements of this list, in order, starting
   *         at index
   * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
Bryce McKinlay committed
312 313 314 315
   */
  ListIterator listIterator(int index);

  /**
Bryce McKinlay committed
316 317
   * Remove the element at a given position in this list (optional operation).
   * Shifts all remaining elements to the left to fill the gap.
Bryce McKinlay committed
318
   *
Bryce McKinlay committed
319 320 321 322 323
   * @param index the position within the list of the object to remove
   * @return the object that was removed
   * @throws UnsupportedOperationException if this list does not support the
   *         remove operation
   * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
Bryce McKinlay committed
324 325 326 327
   */
  Object remove(int index);

  /**
328
   * Remove the first occurence of an object from this list (optional
Bryce McKinlay committed
329 330
   * operation). That is, remove the first element e such that
   * <code>o == null ? e == null : o.equals(e)</code>.
Bryce McKinlay committed
331
   *
Bryce McKinlay committed
332 333 334 335 336
   * @param o the object to remove
   * @return true if the list changed as a result of this call, that is, if
   *         the list contained at least one occurrence of o
   * @throws UnsupportedOperationException if this list does not support the
   *         remove operation
337 338 339 340
   * @throws ClassCastException if the type of o is not a valid
   *         type for this list.
   * @throws NullPointerException if o is null and this
   *         list does not support removing null values.
Bryce McKinlay committed
341 342 343 344
   */
  boolean remove(Object o);

  /**
Bryce McKinlay committed
345 346
   * Remove all elements of a given collection from this list (optional
   * operation). That is, remove every element e such that c.contains(e).
Bryce McKinlay committed
347
   *
Bryce McKinlay committed
348 349 350 351 352
   * @param c the collection to filter out
   * @return true if this list was modified as a result of this call
   * @throws UnsupportedOperationException if this list does not support the
   *         removeAll operation
   * @throws NullPointerException if the collection is null
353 354 355 356
   * @throws ClassCastException if the type of any element in c is not a valid
   *         type for this list.
   * @throws NullPointerException if some element of c is null and this
   *         list does not support removing null values.
Bryce McKinlay committed
357 358
   * @see #remove(Object)
   * @see #contains(Object)
Bryce McKinlay committed
359 360 361 362 363
   */
  boolean removeAll(Collection c);

  /**
   * Remove all elements of this list that are not contained in a given
Bryce McKinlay committed
364 365
   * collection (optional operation). That is, remove every element e such
   * that !c.contains(e).
Bryce McKinlay committed
366
   *
Bryce McKinlay committed
367 368 369 370 371
   * @param c the collection to retain
   * @return true if this list was modified as a result of this call
   * @throws UnsupportedOperationException if this list does not support the
   *         retainAll operation
   * @throws NullPointerException if the collection is null
372 373 374 375
   * @throws ClassCastException if the type of any element in c is not a valid
   *         type for this list.
   * @throws NullPointerException if some element of c is null and this
   *         list does not support retaining null values.
Bryce McKinlay committed
376 377
   * @see #remove(Object)
   * @see #contains(Object)
Bryce McKinlay committed
378 379 380 381
   */
  boolean retainAll(Collection c);

  /**
Bryce McKinlay committed
382
   * Replace an element of this list with another object (optional operation).
Bryce McKinlay committed
383
   *
Bryce McKinlay committed
384 385 386 387 388 389 390 391 392 393
   * @param index the position within this list of the element to be replaced
   * @param o the object to replace it with
   * @return the object that was replaced
   * @throws UnsupportedOperationException if this list does not support the
   *         set operation
   * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
   * @throws ClassCastException if o cannot be added to this list due to its
   *         type
   * @throws IllegalArgumentException if o cannot be added to this list for
   *         some other reason
394 395
   * @throws NullPointerException if o is null and this
   *         list does not support null values.
Bryce McKinlay committed
396 397 398 399
   */
  Object set(int index, Object o);

  /**
Bryce McKinlay committed
400 401
   * Get the number of elements in this list. If the list contains more
   * than Integer.MAX_VALUE elements, return Integer.MAX_VALUE.
Bryce McKinlay committed
402
   *
Bryce McKinlay committed
403
   * @return the number of elements in the list
Bryce McKinlay committed
404 405 406 407 408
   */
  int size();

  /**
   * Obtain a List view of a subsection of this list, from fromIndex
Bryce McKinlay committed
409 410 411 412
   * (inclusive) to toIndex (exclusive). If the two indices are equal, the
   * sublist is empty. The returned list should be modifiable if and only
   * if this list is modifiable. Changes to the returned list should be
   * reflected in this list. If this list is structurally modified in
Bryce McKinlay committed
413 414 415 416
   * any way other than through the returned list, the result of any subsequent
   * operations on the returned list is undefined.
   *
   * @param fromIndex the index that the returned list should start from
Bryce McKinlay committed
417 418 419 420 421
   *        (inclusive)
   * @param toIndex the index that the returned list should go to (exclusive)
   * @return a List backed by a subsection of this list
   * @throws IndexOutOfBoundsException if fromIndex &lt; 0
   *         || toIndex &gt; size() || fromIndex &gt; toIndex
Bryce McKinlay committed
422 423 424 425 426 427
   */
  List subList(int fromIndex, int toIndex);

  /**
   * Copy the current contents of this list into an array.
   *
Bryce McKinlay committed
428 429
   * @return an array of type Object[] and length equal to the length of this
   *         list, containing the elements currently in this list, in order
Bryce McKinlay committed
430 431 432 433 434 435 436 437 438 439 440 441 442
   */
  Object[] toArray();

  /**
   * Copy the current contents of this list into an array. If the array passed
   * as an argument has length less than that of this list, an array of the
   * same run-time type as a, and length equal to the length of this list, is
   * allocated using Reflection. Otherwise, a itself is used. The elements of
   * this list are copied into it, and if there is space in the array, the
   * following element is set to null. The resultant array is returned.
   * Note: The fact that the following element is set to null is only useful
   * if it is known that this list does not contain any null elements.
   *
Bryce McKinlay committed
443 444 445 446 447 448
   * @param a the array to copy this list into
   * @return an array containing the elements currently in this list, in
   *         order
   * @throws ArrayStoreException if the type of any element of the
   *         collection is not a subtype of the element type of a
   * @throws NullPointerException if the specified array is null
Bryce McKinlay committed
449 450
   */
  Object[] toArray(Object[] a);
Warren Levy committed
451
}