AbstractSequentialList.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* AbstractSequentialList.java -- List implementation for sequential access
  2. Copyright (C) 1998, 1999, 2000, 2001 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., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 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 extends AbstractList
  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 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. */
  99. public void add(int index, Object o)
  100. {
  101. listIterator(index).add(o);
  102. }
  103. /**
  104. * Insert the contents of a collection into the list at a given position
  105. * (optional operation). Shift all elements at that position to the right
  106. * by the number of elements inserted. This operation is undefined if
  107. * this list is modified during the operation (for example, if you try
  108. * to insert a list into itself).
  109. * <p>
  110. *
  111. * This implementation grabs listIterator(index), then proceeds to use add
  112. * for each element returned by c's iterator. Sun's online specs are wrong,
  113. * claiming that this also calls next(): listIterator.add() correctly
  114. * skips the added element.
  115. *
  116. * @param index the location to insert the collection
  117. * @param c the collection to insert
  118. * @return true if the list was modified by this action, that is, if c is
  119. * non-empty
  120. * @throws UnsupportedOperationException if this list does not support the
  121. * addAll operation
  122. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
  123. * @throws ClassCastException if some element of c cannot be added to this
  124. * list due to its type
  125. * @throws IllegalArgumentException if some element of c cannot be added
  126. * to this list for some other reason
  127. * @throws NullPointerException if the specified collection is null
  128. * @see #add(int, Object)
  129. */
  130. public boolean addAll(int index, Collection c)
  131. {
  132. Iterator ci = c.iterator();
  133. int size = c.size();
  134. ListIterator i = listIterator(index);
  135. for (int pos = size; pos > 0; pos--)
  136. i.add(ci.next());
  137. return size > 0;
  138. }
  139. /**
  140. * Get the element at a given index in this list. This implementation
  141. * returns listIterator(index).next().
  142. *
  143. * @param index the index of the element to be returned
  144. * @return the element at index index in this list
  145. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
  146. */
  147. public Object get(int index)
  148. {
  149. // This is a legal listIterator position, but an illegal get.
  150. if (index == size())
  151. throw new IndexOutOfBoundsException("Index: " + index + ", Size:"
  152. + size());
  153. return listIterator(index).next();
  154. }
  155. /**
  156. * Obtain an Iterator over this list, whose sequence is the list order. This
  157. * implementation returns listIterator().
  158. *
  159. * @return an Iterator over the elements of this list, in order
  160. */
  161. public Iterator iterator()
  162. {
  163. return listIterator();
  164. }
  165. /**
  166. * Remove the element at a given position in this list (optional operation).
  167. * Shifts all remaining elements to the left to fill the gap. This
  168. * implementation uses listIterator(index) and ListIterator.remove().
  169. *
  170. * @param index the position within the list of the object to remove
  171. * @return the object that was removed
  172. * @throws UnsupportedOperationException if this list does not support the
  173. * remove operation
  174. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
  175. */
  176. public Object remove(int index)
  177. {
  178. // This is a legal listIterator position, but an illegal remove.
  179. if (index == size())
  180. throw new IndexOutOfBoundsException("Index: " + index + ", Size:"
  181. + size());
  182. ListIterator i = listIterator(index);
  183. Object removed = i.next();
  184. i.remove();
  185. return removed;
  186. }
  187. /**
  188. * Replace an element of this list with another object (optional operation).
  189. * This implementation uses listIterator(index) and ListIterator.set(o).
  190. *
  191. * @param index the position within this list of the element to be replaced
  192. * @param o the object to replace it with
  193. * @return the object that was replaced
  194. * @throws UnsupportedOperationException if this list does not support the
  195. * set operation
  196. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
  197. * @throws ClassCastException if o cannot be added to this list due to its
  198. * type
  199. * @throws IllegalArgumentException if o cannot be added to this list for
  200. * some other reason
  201. */
  202. public Object set(int index, Object o)
  203. {
  204. // This is a legal listIterator position, but an illegal set.
  205. if (index == size())
  206. throw new IndexOutOfBoundsException("Index: " + index + ", Size:"
  207. + size());
  208. ListIterator i = listIterator(index);
  209. Object old = i.next();
  210. i.set(o);
  211. return old;
  212. }
  213. }