001 package railo.runtime.type.util; 002 003 import java.util.List; 004 import java.util.ListIterator; 005 import java.util.NoSuchElementException; 006 007 008 public class ListIteratorImpl implements ListIterator { 009 010 private static final int UNDEFINED = Integer.MIN_VALUE; 011 private List list; 012 private int index=-1; 013 private int current=UNDEFINED; 014 015 /** 016 * Constructor of the class 017 * @param arr 018 * @param index 019 */ 020 public ListIteratorImpl(List list, int index){ 021 this.list=list; 022 this.index=index-1; 023 } 024 025 /** 026 * @see java.util.ListIterator#addEntry(E) 027 */ 028 public void add(Object o) { 029 list.add(++index,o); 030 } 031 032 public void remove() { 033 if(current==UNDEFINED)throw new IllegalStateException(); 034 list.remove(current); 035 current=UNDEFINED; 036 } 037 038 public void set(Object o) { 039 if(current==UNDEFINED) throw new IllegalStateException(); 040 list.set(current, o); 041 } 042 043 ///////////// 044 045 046 public boolean hasNext() { 047 return list.size()>index+1; 048 } 049 050 public boolean hasPrevious() { 051 return index>-1; 052 } 053 054 public int previousIndex() { 055 return index; 056 } 057 058 public int nextIndex() { 059 return index+1; 060 } 061 062 public Object previous() { 063 if(!hasPrevious()) 064 throw new NoSuchElementException(); 065 current=index; 066 return list.get(index--); 067 } 068 069 public Object next() { 070 if(!hasNext()) 071 throw new NoSuchElementException(); 072 return list.get(current=++index); 073 } 074 075 }