1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.Iterator ; 19 import java.util.LinkedList ; 20 import java.util.ListIterator ; 21 import java.util.NoSuchElementException ; 22 23 34 public class ListIteratorWrapper implements ListIterator { 35 36 37 private final Iterator iterator; 38 private final LinkedList list = new LinkedList (); 39 40 private int currentIndex = 0; 42 private int wrappedIteratorIndex = 0; 45 46 private static final String UNSUPPORTED_OPERATION_MESSAGE = 47 "ListIteratorWrapper does not support optional operations of ListIterator."; 48 49 52 59 public ListIteratorWrapper(Iterator iterator) { 60 super(); 61 if (iterator == null) { 62 throw new NullPointerException ("Iterator must not be null"); 63 } 64 this.iterator = iterator; 65 } 66 67 70 76 public void add(Object o) throws UnsupportedOperationException { 77 throw new UnsupportedOperationException (UNSUPPORTED_OPERATION_MESSAGE); 78 } 79 80 81 86 public boolean hasNext() { 87 if (currentIndex == wrappedIteratorIndex) { 88 return iterator.hasNext(); 89 } 90 91 return true; 92 } 93 94 99 public boolean hasPrevious() { 100 if (currentIndex == 0) { 101 return false; 102 } 103 104 return true; 105 } 106 107 113 public Object next() throws NoSuchElementException { 114 if (currentIndex < wrappedIteratorIndex) { 115 ++currentIndex; 116 return list.get(currentIndex - 1); 117 } 118 119 Object retval = iterator.next(); 120 list.add(retval); 121 ++currentIndex; 122 ++wrappedIteratorIndex; 123 return retval; 124 } 125 126 131 public int nextIndex() { 132 return currentIndex; 133 } 134 135 141 public Object previous() throws NoSuchElementException { 142 if (currentIndex == 0) { 143 throw new NoSuchElementException (); 144 } 145 146 --currentIndex; 147 return list.get(currentIndex); 148 } 149 150 155 public int previousIndex() { 156 return currentIndex - 1; 157 } 158 159 164 public void remove() throws UnsupportedOperationException { 165 throw new UnsupportedOperationException (UNSUPPORTED_OPERATION_MESSAGE); 166 } 167 168 174 public void set(Object o) throws UnsupportedOperationException { 175 throw new UnsupportedOperationException (UNSUPPORTED_OPERATION_MESSAGE); 176 } 177 178 } 179 180 | Popular Tags |