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 @Override 026 public void add(Object o) { 027 list.add(++index,o); 028 } 029 030 public void remove() { 031 if(current==UNDEFINED)throw new IllegalStateException(); 032 list.remove(current); 033 current=UNDEFINED; 034 } 035 036 public void set(Object o) { 037 if(current==UNDEFINED) throw new IllegalStateException(); 038 list.set(current, o); 039 } 040 041 ///////////// 042 043 044 public boolean hasNext() { 045 return list.size()>index+1; 046 } 047 048 public boolean hasPrevious() { 049 return index>-1; 050 } 051 052 public int previousIndex() { 053 return index; 054 } 055 056 public int nextIndex() { 057 return index+1; 058 } 059 060 public Object previous() { 061 if(!hasPrevious()) 062 throw new NoSuchElementException(); 063 current=index; 064 return list.get(index--); 065 } 066 067 public Object next() { 068 if(!hasNext()) 069 throw new NoSuchElementException(); 070 return list.get(current=++index); 071 } 072 073 }