ListIterator.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* ListIterator.java -- Extended Iterator for iterating over ordered lists
  2. Copyright (C) 1998, 1999, 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. * An extended version of Iterator to support the extra features of Lists. The
  34. * elements may be accessed in forward or reverse order, elements may be
  35. * replaced as well as removed, and new elements may be inserted, during the
  36. * traversal of the list.
  37. * <p>
  38. *
  39. * A list with n elements provides n+1 iterator positions (the front, the end,
  40. * or between two elements). Note that <code>remove</code> and <code>set</code>
  41. * operate on the last element returned, whether it was by <code>next</code>
  42. * or <code>previous</code>.
  43. *
  44. * @author Original author unknown
  45. * @author Eric Blake (ebb9@email.byu.edu)
  46. * @see Collection
  47. * @see List
  48. * @see Iterator
  49. * @see Enumeration
  50. * @since 1.2
  51. * @status updated to 1.4
  52. */
  53. public interface ListIterator<E> extends Iterator<E>
  54. {
  55. /**
  56. * Tests whether there are elements remaining in the list in the forward
  57. * direction. In other words, next() will not fail with a
  58. * NoSuchElementException.
  59. *
  60. * @return true if the list continues in the forward direction
  61. */
  62. boolean hasNext();
  63. /**
  64. * Tests whether there are elements remaining in the list in the reverse
  65. * direction. In other words, previous() will not fail with a
  66. * NoSuchElementException.
  67. *
  68. * @return true if the list continues in the reverse direction
  69. */
  70. boolean hasPrevious();
  71. /**
  72. * Obtain the next element in the list in the forward direction. Repeated
  73. * calls to next may be used to iterate over the entire list, or calls to
  74. * next and previous may be used together to go forwards and backwards.
  75. * Alternating calls to next and previous will return the same element.
  76. *
  77. * @return the next element in the list in the forward direction
  78. * @throws NoSuchElementException if there are no more elements
  79. */
  80. E next();
  81. /**
  82. * Obtain the next element in the list in the reverse direction. Repeated
  83. * calls to previous may be used to iterate backwards over the entire list,
  84. * or calls to next and previous may be used together to go forwards and
  85. * backwards. Alternating calls to next and previous will return the same
  86. * element.
  87. *
  88. * @return the next element in the list in the reverse direction
  89. * @throws NoSuchElementException if there are no more elements
  90. */
  91. E previous();
  92. /**
  93. * Find the index of the element that would be returned by a call to next.
  94. * If hasNext() returns false, this returns the list size.
  95. *
  96. * @return the index of the element that would be returned by next()
  97. */
  98. int nextIndex();
  99. /**
  100. * Find the index of the element that would be returned by a call to
  101. * previous. If hasPrevious() returns false, this returns -1.
  102. *
  103. * @return the index of the element that would be returned by previous()
  104. */
  105. int previousIndex();
  106. /**
  107. * Insert an element into the list at the current position of the iterator
  108. * (optional operation). The element is inserted in between the element that
  109. * would be returned by previous and the element that would be returned by
  110. * next. After the insertion, a subsequent call to next is unaffected, but
  111. * a call to previous returns the item that was added. The values returned
  112. * by nextIndex() and previousIndex() are incremented.
  113. *
  114. * @param o the object to insert into the list
  115. * @throws ClassCastException if the object is of a type which cannot be added
  116. * to this list.
  117. * @throws IllegalArgumentException if some other aspect of the object stops
  118. * it being added to this list.
  119. * @throws UnsupportedOperationException if this ListIterator does not
  120. * support the add operation.
  121. */
  122. void add(E o);
  123. /**
  124. * Remove from the list the element last returned by a call to next or
  125. * previous (optional operation). This method may only be called if neither
  126. * add nor remove have been called since the last call to next or previous.
  127. *
  128. * @throws IllegalStateException if neither next or previous have been
  129. * called, or if add or remove has been called since the last call
  130. * to next or previous
  131. * @throws UnsupportedOperationException if this ListIterator does not
  132. * support the remove operation
  133. */
  134. void remove();
  135. /**
  136. * Replace the element last returned by a call to next or previous with a
  137. * given object (optional operation). This method may only be called if
  138. * neither add nor remove have been called since the last call to next or
  139. * previous.
  140. *
  141. * @param o the object to replace the element with
  142. * @throws ClassCastException the object is of a type which cannot be added
  143. * to this list
  144. * @throws IllegalArgumentException some other aspect of the object stops
  145. * it being added to this list
  146. * @throws IllegalStateException if neither next or previous have been
  147. * called, or if add or remove has been called since the last call
  148. * to next or previous
  149. * @throws UnsupportedOperationException if this ListIterator does not
  150. * support the set operation
  151. */
  152. void set(E o);
  153. }