1. /*
  2. * @(#)AbstractSequentialList.java 1.27 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util;
  8. /**
  9. * This class provides a skeletal implementation of the <tt>List</tt>
  10. * interface to minimize the effort required to implement this interface
  11. * backed by a "sequential access" data store (such as a linked list). For
  12. * random access data (such as an array), <tt>AbstractList</tt> should be used
  13. * in preference to this class.<p>
  14. *
  15. * This class is the opposite of the <tt>AbstractList</tt> class in the sense
  16. * that it implements the "random access" methods (<tt>get(int index)</tt>,
  17. * <tt>set(int index, Object element)</tt>, <tt>set(int index, Object
  18. * element)</tt>, <tt>add(int index, Object element)</tt> and <tt>remove(int
  19. * index)</tt>) on top of the list's list iterator, instead of the other way
  20. * around.<p>
  21. *
  22. * To implement a list the programmer needs only to extend this class and
  23. * provide implementations for the <tt>listIterator</tt> and <tt>size</tt>
  24. * methods. For an unmodifiable list, the programmer need only implement the
  25. * list iterator's <tt>hasNext</tt>, <tt>next</tt>, <tt>hasPrevious</tt>,
  26. * <tt>previous</tt> and <tt>index</tt> methods.<p>
  27. *
  28. * For a modifiable list the programmer should additionally implement the list
  29. * iterator's <tt>set</tt> method. For a variable-size list the programmer
  30. * should additionally implement the list iterator's <tt>remove</tt> and
  31. * <tt>add</tt> methods.<p>
  32. *
  33. * The programmer should generally provide a void (no argument) and collection
  34. * constructor, as per the recommendation in the <tt>Collection</tt> interface
  35. * specification.<p>
  36. *
  37. * This class is a member of the
  38. * <a href="{@docRoot}/../guide/collections/index.html">
  39. * Java Collections Framework</a>.
  40. *
  41. * @author Josh Bloch
  42. * @version 1.27, 01/23/03
  43. * @see Collection
  44. * @see List
  45. * @see AbstractList
  46. * @see AbstractCollection
  47. * @since 1.2
  48. */
  49. public abstract class AbstractSequentialList extends AbstractList {
  50. /**
  51. * Sole constructor. (For invocation by subclass constructors, typically
  52. * implicit.)
  53. */
  54. protected AbstractSequentialList() {
  55. }
  56. /**
  57. * Returns the element at the specified position in this list. <p>
  58. *
  59. * This implementation first gets a list iterator pointing to the indexed
  60. * element (with <tt>listIterator(index)</tt>). Then, it gets the element
  61. * using <tt>ListIterator.next</tt> and returns it.
  62. * @param index index of element to return.
  63. *
  64. * @return the element at the specified position in this list.
  65. * @throws IndexOutOfBoundsException if the specified index is out of
  66. * range (<tt>index < 0 || index >= size()</tt>).
  67. */
  68. public Object get(int index) {
  69. ListIterator e = listIterator(index);
  70. try {
  71. return(e.next());
  72. } catch(NoSuchElementException exc) {
  73. throw(new IndexOutOfBoundsException("Index: "+index));
  74. }
  75. }
  76. /**
  77. * Replaces the element at the specified position in this list with the
  78. * specified element. <p>
  79. *
  80. * This implementation first gets a list iterator pointing to the
  81. * indexed element (with <tt>listIterator(index)</tt>). Then, it gets
  82. * the current element using <tt>ListIterator.next</tt> and replaces it
  83. * with <tt>ListIterator.set</tt>.<p>
  84. *
  85. * Note that this implementation will throw an
  86. * UnsupportedOperationException if list iterator does not implement
  87. * the set operation.
  88. *
  89. * @param index index of element to replace.
  90. * @param element element to be stored at the specified position.
  91. * @return the element previously at the specified position.
  92. * @throws UnsupportedOperationException set is not supported
  93. * by this list.
  94. * @throws NullPointerException this list does not permit null
  95. * elements and one of the elements of <code>c</code> is null.
  96. * @throws ClassCastException class of the specified element
  97. * prevents it from being added to this list.
  98. * @throws IllegalArgumentException some aspect of the specified
  99. * element prevents it from being added to this list.
  100. * @throws IndexOutOfBoundsException index out of range
  101. * <tt>(index < 0 || index >= size()</tt>).
  102. * @throws IllegalArgumentException fromIndex > toIndex.
  103. */
  104. public Object set(int index, Object element) {
  105. ListIterator e = listIterator(index);
  106. try {
  107. Object oldVal = e.next();
  108. e.set(element);
  109. return oldVal;
  110. } catch(NoSuchElementException exc) {
  111. throw(new IndexOutOfBoundsException("Index: "+index));
  112. }
  113. }
  114. /**
  115. * Inserts the specified element at the specified position in this list.
  116. * Shifts the element currently at that position (if any) and any
  117. * subsequent elements to the right (adds one to their indices).<p>
  118. *
  119. * This implementation first gets a list iterator pointing to the
  120. * indexed element (with <tt>listIterator(index)</tt>). Then, it inserts
  121. * the specified element with <tt>ListIterator.add</tt>.<p>
  122. *
  123. * Note that this implementation will throw an
  124. * <tt>UnsupportedOperationException</tt> if list iterator does not
  125. * implement the <tt>add</tt> operation.
  126. *
  127. * @param index index at which the specified element is to be inserted.
  128. * @param element element to be inserted.
  129. * @throws UnsupportedOperationException if the <tt>add</tt> operation is
  130. * not supported by this list.
  131. * @throws NullPointerException this list does not permit <tt>null</tt>
  132. * elements and one of the elements of <code>c</code> is
  133. * <tt>null</tt>.
  134. * @throws ClassCastException if the class of the specified element
  135. * prevents it from being added to this list.
  136. * @throws IllegalArgumentException if some aspect of the specified
  137. * element prevents it from being added to this list.
  138. * @throws IndexOutOfBoundsException if the specified index is out of
  139. * range (<tt>index < 0 || index > size()</tt>).
  140. */
  141. public void add(int index, Object element) {
  142. ListIterator e = listIterator(index);
  143. e.add(element);
  144. }
  145. /**
  146. * Removes the element at the specified position in this list. Shifts any
  147. * subsequent elements to the left (subtracts one from their indices).<p>
  148. *
  149. * This implementation first gets a list iterator pointing to the
  150. * indexed element (with <tt>listIterator(index)</tt>). Then, it removes
  151. * the element with <tt>ListIterator.remove</tt>.<p>
  152. *
  153. * Note that this implementation will throw an
  154. * <tt>UnsupportedOperationException</tt> if list iterator does not
  155. * implement the <tt>remove</tt> operation.
  156. *
  157. * @param index index of the element to be removed from the List.
  158. * @return the element that was removed from the list.
  159. * @throws UnsupportedOperationException if the <tt>remove</tt> operation
  160. * is not supported by this list.
  161. * @throws IndexOutOfBoundsException if the specified index is out of
  162. * range (index < 0 || index >= size()).
  163. */
  164. public Object remove(int index) {
  165. ListIterator e = listIterator(index);
  166. Object outCast;
  167. try {
  168. outCast = e.next();
  169. } catch(NoSuchElementException exc) {
  170. throw(new IndexOutOfBoundsException("Index: "+index));
  171. }
  172. e.remove();
  173. return(outCast);
  174. }
  175. // Bulk Operations
  176. /**
  177. * Inserts all of the elements in in the specified collection into this
  178. * list at the specified position. Shifts the element currently at that
  179. * position (if any) and any subsequent elements to the right (increases
  180. * their indices). The new elements will appear in the list in the order
  181. * that they are returned by the specified collection's iterator. The
  182. * behavior of this operation is unspecified if the specified collection
  183. * is modified while the operation is in progress. (Note that this will
  184. * occur if the specified collection is this list, and it's nonempty.)
  185. * Optional operation.<p>
  186. *
  187. * This implementation gets an iterator over the specified collection and
  188. * a list iterator over this list pointing to the indexed element (with
  189. * <tt>listIterator(index)</tt>). Then, it iterates over the specified
  190. * collection, inserting the elements obtained from the iterator into this
  191. * list, one at a time, using <tt>ListIterator.add</tt> followed by
  192. * <tt>ListIterator.next</tt> (to skip over the added element).<p>
  193. *
  194. * Note that this implementation will throw an
  195. * <tt>UnsupportedOperationException</tt> if the list iterator returned by
  196. * the <tt>listIterator</tt> method does not implement the <tt>add</tt>
  197. * operation.
  198. *
  199. * @return <tt>true</tt> if this list changed as a result of the call.
  200. * @param index index at which to insert first element from the specified
  201. * collection.
  202. * @param c elements to be inserted into this list.
  203. * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
  204. * is not supported by this list.
  205. * @throws NullPointerException this list does not permit <tt>null</tt>
  206. * elements and one of the elements of the specified collection
  207. * is <tt>null</tt>.
  208. * @throws ClassCastException if the class of the specified element
  209. * prevents it from being added to this list.
  210. * @throws IllegalArgumentException if some aspect of the specified
  211. * element prevents it from being added to this list.
  212. * @throws IndexOutOfBoundsException if the specified index is out of
  213. * range (<tt>index < 0 || index > size()</tt>).
  214. * @throws NullPointerException if the specified collection is null.
  215. */
  216. public boolean addAll(int index, Collection c) {
  217. boolean modified = false;
  218. ListIterator e1 = listIterator(index);
  219. Iterator e2 = c.iterator();
  220. while (e2.hasNext()) {
  221. e1.add(e2.next());
  222. modified = true;
  223. }
  224. return modified;
  225. }
  226. // Iterators
  227. /**
  228. * Returns an iterator over the elements in this list (in proper
  229. * sequence).<p>
  230. *
  231. * This implementation merely returns a list iterator over the list.
  232. *
  233. * @return an iterator over the elements in this list (in proper sequence).
  234. */
  235. public Iterator iterator() {
  236. return listIterator();
  237. }
  238. /**
  239. * Returns a list iterator over the elements in this list (in proper
  240. * sequence).
  241. *
  242. * @param index index of first element to be returned from the list
  243. * iterator (by a call to the <code>next</code> method)
  244. * @return a list iterator over the elements in this list (in proper
  245. * sequence).
  246. */
  247. public abstract ListIterator listIterator(int index);
  248. }