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