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