1. /*
  2. * @(#)AbstractSequentialList.java 1.15 01/11/29
  3. *
  4. * Copyright 2002 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.
  36. *
  37. * @author Josh Bloch
  38. * @version 1.15 11/29/01
  39. * @see Collection
  40. * @see List
  41. * @see AbstractList
  42. * @see AbstractCollection
  43. * @since JDK1.2
  44. */
  45. public abstract class AbstractSequentialList extends AbstractList {
  46. /**
  47. * Sole constructor. (For invocation by subclass constructors, typically
  48. * implicit.)
  49. */
  50. protected AbstractSequentialList() {
  51. }
  52. /**
  53. * Returns the element at the specified position in this list. <p>
  54. *
  55. * This implementation first gets a list iterator pointing to the indexed
  56. * element (with <tt>listIterator(index)</tt>). Then, it gets the element
  57. * using <tt>ListIterator.next</tt> and returns it.
  58. *
  59. * @return the element at the specified position in this list. * @param
  60. * index index of element to return. * @throws
  61. * IndexOutOfBoundsException if the specified index is out of
  62. * range (<tt>index < 0 || index >= size()</tt>).
  63. */
  64. public Object get(int index) {
  65. ListIterator e = listIterator(index);
  66. try {
  67. return(e.next());
  68. } catch(NoSuchElementException exc) {
  69. throw(new IndexOutOfBoundsException("Index: "+index));
  70. }
  71. }
  72. /**
  73. * Replaces the element at the specified position in this list with the
  74. * specified element. <p>
  75. *
  76. * This implementation first gets a list iterator pointing to the
  77. * indexed element (with <tt>listIterator(index)</tt>). Then, it gets
  78. * the current element using <tt>ListIterator.next</tt> and replaces it
  79. * with <tt>ListIterator.set</tt>.<p>
  80. *
  81. * Note that this implementation will throw an
  82. * UnsupportedOperationException if list iterator does not implement
  83. * the set operation.
  84. *
  85. * @param index index of element to replace.
  86. * @param element element to be stored at the specified position.
  87. * @return the element previously at the specified position.
  88. * @throws UnsupportedOperationException set is not supported
  89. * by this list.
  90. * @throws NullPointerException this list does not permit null
  91. * elements and one of the elements of <code>c</code> is null.
  92. * @throws ClassCastException class of the specified element
  93. * prevents it from being added to this list.
  94. * @throws IllegalArgumentException some aspect of the specified
  95. * element prevents it from being added to this list.
  96. * @throws IndexOutOfBoundsException index out of range
  97. * <tt>(index < 0 || index >= size()</tt>).
  98. * @throws IllegalArgumentException fromIndex > toIndex.
  99. */
  100. public Object set(int index, Object element) {
  101. ListIterator e = listIterator(index);
  102. try {
  103. Object oldVal = e.next();
  104. e.set(element);
  105. return oldVal;
  106. } catch(NoSuchElementException exc) {
  107. throw(new IndexOutOfBoundsException("Index: "+index));
  108. }
  109. }
  110. /**
  111. * Inserts the specified element at the specified position in this list.
  112. * Shifts the element currently at that position (if any) and any
  113. * subsequent elements to the right (adds one to their indices).<p>
  114. *
  115. * This implementation first gets a list iterator pointing to the
  116. * indexed element (with <tt>listIterator(index)</tt>). Then, it inserts
  117. * the specified element with <tt>ListIterator.add</tt>.<p>
  118. *
  119. * Note that this implementation will throw an
  120. * <tt>UnsupportedOperationException</tt> if list iterator does not
  121. * implement the <tt>add</tt> operation.
  122. *
  123. * @param index index at which the specified element is to be inserted.
  124. * @param element element to be inserted.
  125. * @throws UnsupportedOperationException if the <tt>add</tt> operation is
  126. * not supported by this list.
  127. * @throws NullPointerException this list does not permit <tt>null</tt>
  128. * elements and one of the elements of <code>c</code> is
  129. * <tt>null</tt>.
  130. * @throws ClassCastException if the class of the specified element
  131. * prevents it from being added to this list.
  132. * @throws IllegalArgumentException if some aspect of the specified
  133. * element prevents it from being added to this list.
  134. * @throws IndexOutOfBoundsException if the specified index is out of
  135. * range (<tt>index < 0 || index > size()</tt>).
  136. */
  137. public void add(int index, Object element) {
  138. ListIterator e = listIterator(index);
  139. e.add(element);
  140. }
  141. /**
  142. * Removes the element at the specified position in this list. Shifts any
  143. * subsequent elements to the left (subtracts one from their indices).<p>
  144. *
  145. * This implementation first gets a list iterator pointing to the
  146. * indexed element (with <tt>listIterator(index)</tt>). Then, it removes
  147. * the element with <tt>ListIterator.remove</tt>.<p>
  148. *
  149. * Note that this implementation will throw an
  150. * <tt>UnsupportedOperationException</tt> if list iterator does not
  151. * implement the <tt>remove</tt> operation.
  152. *
  153. * @param the index of the element to be removed from the List.
  154. * @return the element that was removed from the list.
  155. * @throws UnsupportedOperationException if the <tt>remove</tt> operation
  156. * is not supported by this list.
  157. * @throws IndexOutOfBoundsException if the specified index is out of
  158. * range (index < 0 || index >= size()).
  159. */
  160. public Object remove(int index) {
  161. ListIterator e = listIterator(index);
  162. Object outCast;
  163. try {
  164. outCast = e.next();
  165. } catch(NoSuchElementException exc) {
  166. throw(new IndexOutOfBoundsException("Index: "+index));
  167. }
  168. e.remove();
  169. return(outCast);
  170. }
  171. // Bulk Operations
  172. /**
  173. * Inserts all of the elements in in the specified collection into this
  174. * list at the specified position. Shifts the element currently at that
  175. * position (if any) and any subsequent elements to the right (increases
  176. * their indices). The new elements will appear in the list in the order
  177. * that they are returned by the specified collection's iterator. The
  178. * behavior of this operation is unspecified if the specified collection
  179. * is modified while the operation is in progress. (Note that this will
  180. * occur if the specified collection is this list, and it's nonempty.)
  181. * Optional operation.<p>
  182. *
  183. * This implementation gets an iterator over the specified collection and
  184. * a list iterator over this list pointing to the indexed element (with
  185. * <tt>listIterator(index)</tt>). Then, it iterates over the specified
  186. * collection, inserting the elements obtained from the iterator into this
  187. * list, one at a time, using <tt>ListIterator.add</tt> followed by
  188. * <tt>ListIterator.next</tt> (to skip over the added element).<p>
  189. *
  190. * Note that this implementation will throw an
  191. * <tt>UnsupportedOperationException</tt> if the list iterator returned by
  192. * the <tt>listIterator</tt> method does not implement the <tt>add</tt>
  193. * operation.
  194. *
  195. * @return <tt>true</tt> if this list changed as a result of the call.
  196. * @param index index at which to insert first element from the specified
  197. * collection.
  198. * @param c elements to be inserted into this list.
  199. * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
  200. * is not supported by this list.
  201. * @throws NullPointerException this list does not permit <tt>null</tt>
  202. * elements and one of the elements of the specified collection
  203. * is <tt>null</tt>.
  204. * @throws ClassCastException if the class of the specified element
  205. * prevents it from being added to this list.
  206. * @throws IllegalArgumentException if some aspect of the specified
  207. * element prevents it from being added to this list.
  208. * @throws IndexOutOfBoundsException if the specified index is out of
  209. * range (<tt>index < 0 || index > size()</tt>).
  210. */
  211. public boolean addAll(int index, Collection c) {
  212. boolean modified = false;
  213. ListIterator e1 = listIterator(index);
  214. Iterator e2 = c.iterator();
  215. while (e2.hasNext()) {
  216. e1.add(e2.next());
  217. e1.next(); // Skip over the element that we just added
  218. modified = true;
  219. }
  220. return modified;
  221. }
  222. // Iterators
  223. /**
  224. * Returns an iterator over the elements in this list (in proper
  225. * sequence).<p>
  226. *
  227. * This implementation merely returns a list iterator over the list.
  228. *
  229. * @return an iterator over the elements in this list (in proper sequence).
  230. */
  231. public Iterator iterator() {
  232. return listIterator();
  233. }
  234. /**
  235. * Returns a list iterator over the elements in this list (in proper
  236. * sequence).
  237. *
  238. * @return a list iterator over the elements in this list (in proper
  239. * sequence).
  240. */
  241. public abstract ListIterator listIterator(int index);
  242. }