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.util;
020
021import java.util.List;
022import java.util.ListIterator;
023import java.util.NoSuchElementException;
024
025
026public class ListIteratorImpl implements ListIterator {
027        
028        private static final int UNDEFINED = Integer.MIN_VALUE;
029        private List list;
030        private int index=-1;
031        private int current=UNDEFINED;
032
033        /**
034         * Constructor of the class
035         * @param arr
036         * @param index 
037         */
038        public ListIteratorImpl(List list, int index){
039                this.list=list;
040                this.index=index-1;
041        }
042
043        @Override
044        public void add(Object o) {
045                list.add(++index,o);
046        }
047
048        public void remove() {
049                if(current==UNDEFINED)throw new IllegalStateException();
050                list.remove(current);
051                current=UNDEFINED;
052        }
053
054        public void set(Object o) {
055                if(current==UNDEFINED) throw new IllegalStateException();
056                list.set(current, o);
057        }
058        
059/////////////   
060        
061
062        public boolean hasNext() {
063                return list.size()>index+1;
064        }
065
066        public boolean hasPrevious() {
067                return index>-1;
068        }
069
070        public int previousIndex() {
071                return index;
072        }
073
074        public int nextIndex() {
075                return index+1;
076        }
077
078        public Object previous() {
079                if(!hasPrevious())
080                        throw new NoSuchElementException();
081                current=index;
082                return list.get(index--);
083        }
084
085        public Object next() {
086                if(!hasNext())
087                        throw new NoSuchElementException();
088                return list.get(current=++index);
089        }
090
091}