001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.type.it;
020
021import java.util.ListIterator;
022import java.util.NoSuchElementException;
023
024import lucee.runtime.type.Array;
025
026
027public class ArrayListIteratorImpl implements ListIterator {
028        
029        private static final int UNDEFINED = Integer.MIN_VALUE;
030        private Array array;
031        private int index=-1;
032        private int current=UNDEFINED;
033
034        /**
035         * Constructor of the class
036         * @param arr
037         * @param index 
038         */
039        public ArrayListIteratorImpl(Array array, int index){
040                this.array=array;
041                this.index=index-1;
042        }
043
044        @Override
045        public void add(Object o) {
046                array.setEL((++index)+1,o);
047        }
048
049        public void remove() {
050                if(current==UNDEFINED)throw new IllegalStateException();
051                array.removeEL(current+1);
052                current=UNDEFINED;
053        }
054
055        public void set(Object o) {
056                if(current==UNDEFINED) throw new IllegalStateException();
057                array.setEL(current+1, o);
058        }
059        
060/////////////   
061        
062
063        public boolean hasNext() {
064                return array.size()>index+1;
065        }
066
067        public boolean hasPrevious() {
068                return index>-1;
069        }
070
071        public int previousIndex() {
072                return index;
073        }
074
075        public int nextIndex() {
076                return index+1;
077        }
078
079        public Object previous() {
080                if(!hasPrevious())
081                        throw new NoSuchElementException();
082                current=index;
083                return array.get((index--)+1,null);
084        }
085
086        public Object next() {
087                if(!hasNext())
088                        throw new NoSuchElementException();
089                return array.get((current=++index)+1,null);
090        }
091
092}