1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.List ; 19 import java.util.ListIterator ; 20 21 import org.apache.commons.collections.ResettableListIterator; 22 23 40 public class ReverseListIterator implements ResettableListIterator { 41 42 43 private final List list; 44 45 private ListIterator iterator; 46 47 private boolean validForUpdate = true; 48 49 55 public ReverseListIterator(List list) { 56 super(); 57 this.list = list; 58 iterator = list.listIterator(list.size()); 59 } 60 61 67 public boolean hasNext() { 68 return iterator.hasPrevious(); 69 } 70 71 77 public Object next() { 78 Object obj = iterator.previous(); 79 validForUpdate = true; 80 return obj; 81 } 82 83 88 public int nextIndex() { 89 return iterator.previousIndex(); 90 } 91 92 97 public boolean hasPrevious() { 98 return iterator.hasNext(); 99 } 100 101 107 public Object previous() { 108 Object obj = iterator.next(); 109 validForUpdate = true; 110 return obj; 111 } 112 113 118 public int previousIndex() { 119 return iterator.nextIndex(); 120 } 121 122 128 public void remove() { 129 if (validForUpdate == false) { 130 throw new IllegalStateException ("Cannot remove from list until next() or previous() called"); 131 } 132 iterator.remove(); 133 } 134 135 142 public void set(Object obj) { 143 if (validForUpdate == false) { 144 throw new IllegalStateException ("Cannot set to list until next() or previous() called"); 145 } 146 iterator.set(obj); 147 } 148 149 156 public void add(Object obj) { 157 if (validForUpdate == false) { 160 throw new IllegalStateException ("Cannot add to list until next() or previous() called"); 161 } 162 validForUpdate = false; 163 iterator.add(obj); 164 iterator.previous(); 165 } 166 167 171 public void reset() { 172 iterator = list.listIterator(list.size()); 173 } 174 175 } 176 | Popular Tags |