1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections.iterators;
  17. import java.util.Iterator;
  18. import java.util.LinkedList;
  19. import java.util.ListIterator;
  20. import java.util.NoSuchElementException;
  21. /**
  22. * As the wrapped Iterator is traversed, ListIteratorWrapper
  23. * builds a LinkedList of its values, permitting all required
  24. * operations of ListIterator.
  25. *
  26. * @since Commons Collections 2.1
  27. * @version $Revision: 1.7 $ $Date: 2004/02/18 00:59:50 $
  28. *
  29. * @author Morgan Delagrange
  30. * @author Stephen Colebourne
  31. */
  32. public class ListIteratorWrapper implements ListIterator {
  33. /** Holds value of property "iterator" */
  34. private final Iterator iterator;
  35. private final LinkedList list = new LinkedList();
  36. // position of this iterator
  37. private int currentIndex = 0;
  38. // position of the wrapped iterator
  39. // this Iterator should only be used to populate the list
  40. private int wrappedIteratorIndex = 0;
  41. private static final String UNSUPPORTED_OPERATION_MESSAGE =
  42. "ListIteratorWrapper does not support optional operations of ListIterator.";
  43. // Constructor
  44. //-------------------------------------------------------------------------
  45. /**
  46. * Constructs a new <code>ListIteratorWrapper</code> that will wrap
  47. * the given iterator.
  48. *
  49. * @param iterator the iterator to wrap
  50. * @throws NullPointerException if the iterator is null
  51. */
  52. public ListIteratorWrapper(Iterator iterator) {
  53. super();
  54. if (iterator == null) {
  55. throw new NullPointerException("Iterator must not be null");
  56. }
  57. this.iterator = iterator;
  58. }
  59. // ListIterator interface
  60. //-------------------------------------------------------------------------
  61. /**
  62. * Throws {@link UnsupportedOperationException}.
  63. *
  64. * @param o ignored
  65. * @throws UnsupportedOperationException always
  66. */
  67. public void add(Object o) throws UnsupportedOperationException {
  68. throw new UnsupportedOperationException(UNSUPPORTED_OPERATION_MESSAGE);
  69. }
  70. /**
  71. * Returns true if there are more elements in the iterator.
  72. *
  73. * @return true if there are more elements
  74. */
  75. public boolean hasNext() {
  76. if (currentIndex == wrappedIteratorIndex) {
  77. return iterator.hasNext();
  78. }
  79. return true;
  80. }
  81. /**
  82. * Returns true if there are previous elements in the iterator.
  83. *
  84. * @return true if there are previous elements
  85. */
  86. public boolean hasPrevious() {
  87. if (currentIndex == 0) {
  88. return false;
  89. }
  90. return true;
  91. }
  92. /**
  93. * Returns the next element from the iterator.
  94. *
  95. * @return the next element from the iterator
  96. * @throws NoSuchElementException if there are no more elements
  97. */
  98. public Object next() throws NoSuchElementException {
  99. if (currentIndex < wrappedIteratorIndex) {
  100. ++currentIndex;
  101. return list.get(currentIndex - 1);
  102. }
  103. Object retval = iterator.next();
  104. list.add(retval);
  105. ++currentIndex;
  106. ++wrappedIteratorIndex;
  107. return retval;
  108. }
  109. /**
  110. * Returns in the index of the next element.
  111. *
  112. * @return the index of the next element
  113. */
  114. public int nextIndex() {
  115. return currentIndex;
  116. }
  117. /**
  118. * Returns the the previous element.
  119. *
  120. * @return the previous element
  121. * @throws NoSuchElementException if there are no previous elements
  122. */
  123. public Object previous() throws NoSuchElementException {
  124. if (currentIndex == 0) {
  125. throw new NoSuchElementException();
  126. }
  127. --currentIndex;
  128. return list.get(currentIndex);
  129. }
  130. /**
  131. * Returns the index of the previous element.
  132. *
  133. * @return the index of the previous element
  134. */
  135. public int previousIndex() {
  136. return currentIndex - 1;
  137. }
  138. /**
  139. * Throws {@link UnsupportedOperationException}.
  140. *
  141. * @throws UnsupportedOperationException always
  142. */
  143. public void remove() throws UnsupportedOperationException {
  144. throw new UnsupportedOperationException(UNSUPPORTED_OPERATION_MESSAGE);
  145. }
  146. /**
  147. * Throws {@link UnsupportedOperationException}.
  148. *
  149. * @param o ignored
  150. * @throws UnsupportedOperationException always
  151. */
  152. public void set(Object o) throws UnsupportedOperationException {
  153. throw new UnsupportedOperationException(UNSUPPORTED_OPERATION_MESSAGE);
  154. }
  155. }