List.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /* List.java -- An ordered collection which allows indexed access
  2. Copyright (C) 1998, 2001, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util;
  32. /**
  33. * An ordered collection (also known as a list). This collection allows
  34. * access to elements by position, as well as control on where elements
  35. * are inserted. Unlike sets, duplicate elements are permitted by this
  36. * general contract (if a subclass forbids duplicates, this should be
  37. * documented).
  38. * <p>
  39. *
  40. * List places additional requirements on <code>iterator</code>,
  41. * <code>add</code>, <code>remove</code>, <code>equals</code>, and
  42. * <code>hashCode</code>, in addition to requiring more methods. List
  43. * indexing is 0-based (like arrays), although some implementations may
  44. * require time proportional to the index to obtain an arbitrary element.
  45. * The List interface is incompatible with Set; you cannot implement both
  46. * simultaneously.
  47. * <p>
  48. *
  49. * Lists also provide a <code>ListIterator</code> which allows bidirectional
  50. * traversal and other features atop regular iterators. Lists can be
  51. * searched for arbitrary elements, and allow easy insertion and removal
  52. * of multiple elements in one method call.
  53. * <p>
  54. *
  55. * Note: While lists may contain themselves as elements, this leads to
  56. * undefined (usually infinite recursive) behavior for some methods like
  57. * hashCode or equals.
  58. *
  59. * @author Original author unknown
  60. * @author Eric Blake (ebb9@email.byu.edu)
  61. * @see Collection
  62. * @see Set
  63. * @see ArrayList
  64. * @see LinkedList
  65. * @see Vector
  66. * @see Arrays#asList(Object[])
  67. * @see Collections#nCopies(int, Object)
  68. * @see Collections#EMPTY_LIST
  69. * @see AbstractList
  70. * @see AbstractSequentialList
  71. * @since 1.2
  72. * @status updated to 1.4
  73. */
  74. public interface List<E> extends Collection<E>
  75. {
  76. /**
  77. * Insert an element into the list at a given position (optional operation).
  78. * This shifts all existing elements from that position to the end one
  79. * index to the right. This version of add has no return, since it is
  80. * assumed to always succeed if there is no exception.
  81. *
  82. * @param index the location to insert the item
  83. * @param o the object to insert
  84. * @throws UnsupportedOperationException if this list does not support the
  85. * add operation
  86. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
  87. * @throws ClassCastException if o cannot be added to this list due to its
  88. * type
  89. * @throws IllegalArgumentException if o cannot be added to this list for
  90. * some other reason
  91. * @throws NullPointerException if o is null and this list doesn't support
  92. * the addition of null values.
  93. */
  94. void add(int index, E o);
  95. /**
  96. * Add an element to the end of the list (optional operation). If the list
  97. * imposes restraints on what can be inserted, such as no null elements,
  98. * this should be documented.
  99. *
  100. * @param o the object to add
  101. * @return true, as defined by Collection for a modified list
  102. * @throws UnsupportedOperationException if this list does not support the
  103. * add operation
  104. * @throws ClassCastException if o cannot be added to this list due to its
  105. * type
  106. * @throws IllegalArgumentException if o cannot be added to this list for
  107. * some other reason
  108. * @throws NullPointerException if o is null and this list doesn't support
  109. * the addition of null values.
  110. */
  111. boolean add(E o);
  112. /**
  113. * Insert the contents of a collection into the list at a given position
  114. * (optional operation). Shift all elements at that position to the right
  115. * by the number of elements inserted. This operation is undefined if
  116. * this list is modified during the operation (for example, if you try
  117. * to insert a list into itself).
  118. *
  119. * @param index the location to insert the collection
  120. * @param c the collection to insert
  121. * @return true if the list was modified by this action, that is, if c is
  122. * non-empty
  123. * @throws UnsupportedOperationException if this list does not support the
  124. * addAll operation
  125. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
  126. * @throws ClassCastException if some element of c cannot be added to this
  127. * list due to its type
  128. * @throws IllegalArgumentException if some element of c cannot be added
  129. * to this list for some other reason
  130. * @throws NullPointerException if some element of c is null and this list
  131. * doesn't support the addition of null values.
  132. * @throws NullPointerException if the specified collection is null
  133. * @see #add(int, Object)
  134. */
  135. boolean addAll(int index, Collection<? extends E> c);
  136. /**
  137. * Add the contents of a collection to the end of the list (optional
  138. * operation). This operation is undefined if this list is modified
  139. * during the operation (for example, if you try to insert a list into
  140. * itself).
  141. *
  142. * @param c the collection to add
  143. * @return true if the list was modified by this action, that is, if c is
  144. * non-empty
  145. * @throws UnsupportedOperationException if this list does not support the
  146. * addAll operation
  147. * @throws ClassCastException if some element of c cannot be added to this
  148. * list due to its type
  149. * @throws IllegalArgumentException if some element of c cannot be added
  150. * to this list for some other reason
  151. * @throws NullPointerException if the specified collection is null
  152. * @throws NullPointerException if some element of c is null and this list
  153. * doesn't support the addition of null values.
  154. * @see #add(Object)
  155. */
  156. boolean addAll(Collection<? extends E> c);
  157. /**
  158. * Clear the list, such that a subsequent call to isEmpty() would return
  159. * true (optional operation).
  160. *
  161. * @throws UnsupportedOperationException if this list does not support the
  162. * clear operation
  163. */
  164. void clear();
  165. /**
  166. * Test whether this list contains a given object as one of its elements.
  167. * This is defined as the existence of an element e such that
  168. * <code>o == null ? e == null : o.equals(e)</code>.
  169. *
  170. * @param o the element to look for
  171. * @return true if this list contains the element
  172. * @throws ClassCastException if the type of o is not a valid type
  173. * for this list.
  174. * @throws NullPointerException if o is null and the list doesn't
  175. * support null values.
  176. */
  177. boolean contains(Object o);
  178. /**
  179. * Test whether this list contains every element in a given collection.
  180. *
  181. * @param c the collection to test for
  182. * @return true if for every element o in c, contains(o) would return true
  183. * @throws NullPointerException if the collection is null
  184. * @throws ClassCastException if the type of any element in c is not a valid
  185. * type for this list.
  186. * @throws NullPointerException if some element of c is null and this
  187. * list does not support null values.
  188. * @see #contains(Object)
  189. */
  190. boolean containsAll(Collection<?> c);
  191. /**
  192. * Test whether this list is equal to another object. A List is defined to be
  193. * equal to an object if and only if that object is also a List, and the two
  194. * lists have the same sequence. Two lists l1 and l2 are equal if and only
  195. * if <code>l1.size() == l2.size()</code>, and for every integer n between 0
  196. * and <code>l1.size() - 1</code> inclusive, <code>l1.get(n) == null ?
  197. * l2.get(n) == null : l1.get(n).equals(l2.get(n))</code>.
  198. *
  199. * @param o the object to test for equality with this list
  200. * @return true if o is equal to this list
  201. * @see Object#equals(Object)
  202. * @see #hashCode()
  203. */
  204. boolean equals(Object o);
  205. /**
  206. * Get the element at a given index in this list.
  207. *
  208. * @param index the index of the element to be returned
  209. * @return the element at index index in this list
  210. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
  211. */
  212. E get(int index);
  213. /**
  214. * Obtains a hash code for this list. In order to obey the general
  215. * contract of the hashCode method of class Object, this value is
  216. * calculated as follows:
  217. *
  218. <p><pre>hashCode = 1;
  219. Iterator i = list.iterator();
  220. while (i.hasNext())
  221. {
  222. Object obj = i.next();
  223. hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
  224. }</pre>
  225. *
  226. * <p>This ensures that the general contract of Object.hashCode()
  227. * is adhered to.
  228. *
  229. * @return the hash code of this list
  230. * @see Object#hashCode()
  231. * @see #equals(Object)
  232. */
  233. int hashCode();
  234. /**
  235. * Obtain the first index at which a given object is to be found in this
  236. * list.
  237. *
  238. * @param o the object to search for
  239. * @return the least integer n such that <code>o == null ? get(n) == null :
  240. * o.equals(get(n))</code>, or -1 if there is no such index.
  241. * @throws ClassCastException if the type of o is not a valid
  242. * type for this list.
  243. * @throws NullPointerException if o is null and this
  244. * list does not support null values.
  245. */
  246. int indexOf(Object o);
  247. /**
  248. * Test whether this list is empty, that is, if size() == 0.
  249. *
  250. * @return true if this list contains no elements
  251. */
  252. boolean isEmpty();
  253. /**
  254. * Obtain an Iterator over this list, whose sequence is the list order.
  255. *
  256. * @return an Iterator over the elements of this list, in order
  257. */
  258. Iterator<E> iterator();
  259. /**
  260. * Obtain the last index at which a given object is to be found in this
  261. * list.
  262. *
  263. * @return the greatest integer n such that <code>o == null ? get(n) == null
  264. * : o.equals(get(n))</code>, or -1 if there is no such index.
  265. * @throws ClassCastException if the type of o is not a valid
  266. * type for this list.
  267. * @throws NullPointerException if o is null and this
  268. * list does not support null values.
  269. */
  270. int lastIndexOf(Object o);
  271. /**
  272. * Obtain a ListIterator over this list, starting at the beginning.
  273. *
  274. * @return a ListIterator over the elements of this list, in order, starting
  275. * at the beginning
  276. */
  277. ListIterator<E> listIterator();
  278. /**
  279. * Obtain a ListIterator over this list, starting at a given position.
  280. * A first call to next() would return the same as get(index), and a
  281. * first call to previous() would return the same as get(index - 1).
  282. *
  283. * @param index the position, between 0 and size() inclusive, to begin the
  284. * iteration from
  285. * @return a ListIterator over the elements of this list, in order, starting
  286. * at index
  287. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
  288. */
  289. ListIterator<E> listIterator(int index);
  290. /**
  291. * Remove the element at a given position in this list (optional operation).
  292. * Shifts all remaining elements to the left to fill the gap.
  293. *
  294. * @param index the position within the list of the object to remove
  295. * @return the object that was removed
  296. * @throws UnsupportedOperationException if this list does not support the
  297. * remove operation
  298. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
  299. */
  300. E remove(int index);
  301. /**
  302. * Remove the first occurence of an object from this list (optional
  303. * operation). That is, remove the first element e such that
  304. * <code>o == null ? e == null : o.equals(e)</code>.
  305. *
  306. * @param o the object to remove
  307. * @return true if the list changed as a result of this call, that is, if
  308. * the list contained at least one occurrence of o
  309. * @throws UnsupportedOperationException if this list does not support the
  310. * remove operation
  311. * @throws ClassCastException if the type of o is not a valid
  312. * type for this list.
  313. * @throws NullPointerException if o is null and this
  314. * list does not support removing null values.
  315. */
  316. boolean remove(Object o);
  317. /**
  318. * Remove all elements of a given collection from this list (optional
  319. * operation). That is, remove every element e such that c.contains(e).
  320. *
  321. * @param c the collection to filter out
  322. * @return true if this list was modified as a result of this call
  323. * @throws UnsupportedOperationException if this list does not support the
  324. * removeAll operation
  325. * @throws NullPointerException if the collection is null
  326. * @throws ClassCastException if the type of any element in c is not a valid
  327. * type for this list.
  328. * @throws NullPointerException if some element of c is null and this
  329. * list does not support removing null values.
  330. * @see #remove(Object)
  331. * @see #contains(Object)
  332. */
  333. boolean removeAll(Collection<?> c);
  334. /**
  335. * Remove all elements of this list that are not contained in a given
  336. * collection (optional operation). That is, remove every element e such
  337. * that !c.contains(e).
  338. *
  339. * @param c the collection to retain
  340. * @return true if this list was modified as a result of this call
  341. * @throws UnsupportedOperationException if this list does not support the
  342. * retainAll operation
  343. * @throws NullPointerException if the collection is null
  344. * @throws ClassCastException if the type of any element in c is not a valid
  345. * type for this list.
  346. * @throws NullPointerException if some element of c is null and this
  347. * list does not support retaining null values.
  348. * @see #remove(Object)
  349. * @see #contains(Object)
  350. */
  351. boolean retainAll(Collection<?> c);
  352. /**
  353. * Replace an element of this list with another object (optional operation).
  354. *
  355. * @param index the position within this list of the element to be replaced
  356. * @param o the object to replace it with
  357. * @return the object that was replaced
  358. * @throws UnsupportedOperationException if this list does not support the
  359. * set operation
  360. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
  361. * @throws ClassCastException if o cannot be added to this list due to its
  362. * type
  363. * @throws IllegalArgumentException if o cannot be added to this list for
  364. * some other reason
  365. * @throws NullPointerException if o is null and this
  366. * list does not support null values.
  367. */
  368. E set(int index, E o);
  369. /**
  370. * Get the number of elements in this list. If the list contains more
  371. * than Integer.MAX_VALUE elements, return Integer.MAX_VALUE.
  372. *
  373. * @return the number of elements in the list
  374. */
  375. int size();
  376. /**
  377. * Obtain a List view of a subsection of this list, from fromIndex
  378. * (inclusive) to toIndex (exclusive). If the two indices are equal, the
  379. * sublist is empty. The returned list should be modifiable if and only
  380. * if this list is modifiable. Changes to the returned list should be
  381. * reflected in this list. If this list is structurally modified in
  382. * any way other than through the returned list, the result of any subsequent
  383. * operations on the returned list is undefined.
  384. *
  385. * @param fromIndex the index that the returned list should start from
  386. * (inclusive)
  387. * @param toIndex the index that the returned list should go to (exclusive)
  388. * @return a List backed by a subsection of this list
  389. * @throws IndexOutOfBoundsException if fromIndex &lt; 0
  390. * || toIndex &gt; size() || fromIndex &gt; toIndex
  391. */
  392. List<E> subList(int fromIndex, int toIndex);
  393. /**
  394. * Copy the current contents of this list into an array.
  395. *
  396. * @return an array of type Object[] and length equal to the length of this
  397. * list, containing the elements currently in this list, in order
  398. */
  399. Object[] toArray();
  400. /**
  401. * Copy the current contents of this list into an array. If the array passed
  402. * as an argument has length less than that of this list, an array of the
  403. * same run-time type as a, and length equal to the length of this list, is
  404. * allocated using Reflection. Otherwise, a itself is used. The elements of
  405. * this list are copied into it, and if there is space in the array, the
  406. * following element is set to null. The resultant array is returned.
  407. * Note: The fact that the following element is set to null is only useful
  408. * if it is known that this list does not contain any null elements.
  409. *
  410. * @param a the array to copy this list into
  411. * @return an array containing the elements currently in this list, in
  412. * order
  413. * @throws ArrayStoreException if the type of any element of the
  414. * collection is not a subtype of the element type of a
  415. * @throws NullPointerException if the specified array is null
  416. */
  417. <T> T[] toArray(T[] a);
  418. }