AbstractSequentialList.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* AbstractSequentialList.java -- List implementation for sequential access
  2. Copyright (C) 1998, 1999, 2000, 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. * Abstract superclass to make it easier to implement the List interface when
  34. * backed by a sequential-access store, such as a linked list. For random
  35. * access data, use AbstractList. This class implements the random access
  36. * methods (<code>get</code>, <code>set</code>, <code>add</code>, and
  37. * <code>remove</code>) atop the list iterator, opposite of AbstractList's
  38. * approach of implementing the iterator atop random access.
  39. * <p>
  40. *
  41. * To implement a list, you need an implementation for <code>size()</code>
  42. * and <code>listIterator</code>. With just <code>hasNext</code>,
  43. * <code>next</code>, <code>hasPrevious</code>, <code>previous</code>,
  44. * <code>nextIndex</code>, and <code>previousIndex</code>, you have an
  45. * unmodifiable list. For a modifiable one, add <code>set</code>, and for
  46. * a variable-size list, add <code>add</code> and <code>remove</code>.
  47. * <p>
  48. *
  49. * The programmer should provide a no-argument constructor, and one that
  50. * accepts another Collection, as recommended by the Collection interface.
  51. * Unfortunately, there is no way to enforce this in Java.
  52. *
  53. * @author Original author unknown
  54. * @author Bryce McKinlay
  55. * @author Eric Blake (ebb9@email.byu.edu)
  56. * @see Collection
  57. * @see List
  58. * @see AbstractList
  59. * @see AbstractCollection
  60. * @see ListIterator
  61. * @see LinkedList
  62. * @since 1.2
  63. * @status updated to 1.4
  64. */
  65. public abstract class AbstractSequentialList<E> extends AbstractList<E>
  66. {
  67. /**
  68. * The main constructor, for use by subclasses.
  69. */
  70. protected AbstractSequentialList()
  71. {
  72. }
  73. /**
  74. * Returns a ListIterator over the list, starting from position index.
  75. * Subclasses must provide an implementation of this method.
  76. *
  77. * @param index the starting position of the list
  78. * @return the list iterator
  79. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
  80. */
  81. public abstract ListIterator<E> listIterator(int index);
  82. /**
  83. * Insert an element into the list at a given position (optional operation).
  84. * This shifts all existing elements from that position to the end one
  85. * index to the right. This version of add has no return, since it is
  86. * assumed to always succeed if there is no exception. This iteration
  87. * uses listIterator(index).add(o).
  88. *
  89. * @param index the location to insert the item
  90. * @param o the object to insert
  91. * @throws UnsupportedOperationException if this list does not support the
  92. * add operation
  93. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
  94. * @throws ClassCastException if o cannot be added to this list due to its
  95. * type
  96. * @throws IllegalArgumentException if o cannot be added to this list for
  97. * some other reason.
  98. * @throws NullPointerException if o is null and the list does not permit
  99. * the addition of null values.
  100. */
  101. public void add(int index, E o)
  102. {
  103. listIterator(index).add(o);
  104. }
  105. /**
  106. * Insert the contents of a collection into the list at a given position
  107. * (optional operation). Shift all elements at that position to the right
  108. * by the number of elements inserted. This operation is undefined if
  109. * this list is modified during the operation (for example, if you try
  110. * to insert a list into itself).
  111. * <p>
  112. *
  113. * This implementation grabs listIterator(index), then proceeds to use add
  114. * for each element returned by c's iterator. Sun's online specs are wrong,
  115. * claiming that this also calls next(): listIterator.add() correctly
  116. * skips the added element.
  117. *
  118. * @param index the location to insert the collection
  119. * @param c the collection to insert
  120. * @return true if the list was modified by this action, that is, if c is
  121. * non-empty
  122. * @throws UnsupportedOperationException if this list does not support the
  123. * addAll operation
  124. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
  125. * @throws ClassCastException if some element of c cannot be added to this
  126. * list due to its type
  127. * @throws IllegalArgumentException if some element of c cannot be added
  128. * to this list for some other reason
  129. * @throws NullPointerException if the specified collection is null
  130. * @throws NullPointerException if an object, o, in c is null and the list
  131. * does not permit the addition of null values.
  132. * @see #add(int, Object)
  133. */
  134. public boolean addAll(int index, Collection<? extends E> c)
  135. {
  136. Iterator<? extends E> ci = c.iterator();
  137. int size = c.size();
  138. ListIterator<E> i = listIterator(index);
  139. for (int pos = size; pos > 0; pos--)
  140. i.add(ci.next());
  141. return size > 0;
  142. }
  143. /**
  144. * Get the element at a given index in this list. This implementation
  145. * returns listIterator(index).next().
  146. *
  147. * @param index the index of the element to be returned
  148. * @return the element at index index in this list
  149. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
  150. */
  151. public E get(int index)
  152. {
  153. // This is a legal listIterator position, but an illegal get.
  154. if (index == size())
  155. throw new IndexOutOfBoundsException("Index: " + index + ", Size:"
  156. + size());
  157. return listIterator(index).next();
  158. }
  159. /**
  160. * Obtain an Iterator over this list, whose sequence is the list order. This
  161. * implementation returns listIterator().
  162. *
  163. * @return an Iterator over the elements of this list, in order
  164. */
  165. public Iterator<E> iterator()
  166. {
  167. return listIterator();
  168. }
  169. /**
  170. * Remove the element at a given position in this list (optional operation).
  171. * Shifts all remaining elements to the left to fill the gap. This
  172. * implementation uses listIterator(index) and ListIterator.remove().
  173. *
  174. * @param index the position within the list of the object to remove
  175. * @return the object that was removed
  176. * @throws UnsupportedOperationException if this list does not support the
  177. * remove operation
  178. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
  179. */
  180. public E remove(int index)
  181. {
  182. // This is a legal listIterator position, but an illegal remove.
  183. if (index == size())
  184. throw new IndexOutOfBoundsException("Index: " + index + ", Size:"
  185. + size());
  186. ListIterator<E> i = listIterator(index);
  187. E removed = i.next();
  188. i.remove();
  189. return removed;
  190. }
  191. /**
  192. * Replace an element of this list with another object (optional operation).
  193. * This implementation uses listIterator(index) and ListIterator.set(o).
  194. *
  195. * @param index the position within this list of the element to be replaced
  196. * @param o the object to replace it with
  197. * @return the object that was replaced
  198. * @throws UnsupportedOperationException if this list does not support the
  199. * set operation
  200. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
  201. * @throws ClassCastException if o cannot be added to this list due to its
  202. * type
  203. * @throws IllegalArgumentException if o cannot be added to this list for
  204. * some other reason
  205. * @throws NullPointerException if o is null and the list does not allow
  206. * a value to be set to null.
  207. */
  208. public E set(int index, E o)
  209. {
  210. // This is a legal listIterator position, but an illegal set.
  211. if (index == size())
  212. throw new IndexOutOfBoundsException("Index: " + index + ", Size:"
  213. + size());
  214. ListIterator<E> i = listIterator(index);
  215. E old = i.next();
  216. i.set(o);
  217. return old;
  218. }
  219. }