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