001/* Copyright (C) 2013 TU Dortmund
002 * This file is part of AutomataLib, http://www.automatalib.net/.
003 * 
004 * AutomataLib is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU Lesser General Public
006 * License version 3.0 as published by the Free Software Foundation.
007 * 
008 * AutomataLib is distributed in the hope that it will be useful,
009 * but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
011 * Lesser General Public License for more details.
012 * 
013 * You should have received a copy of the GNU Lesser General Public
014 * License along with AutomataLib; if not, see
015 * http://www.gnu.de/documents/lgpl.en.html.
016 */
017package net.automatalib.commons.util.collections;
018
019import java.util.ListIterator;
020import java.util.NoSuchElementException;
021
022public class IntRangeIterator implements ListIterator<Integer> {
023        
024        private final int low;
025        private final int step;
026        private final int size;
027        private int curr;
028
029        public IntRangeIterator(int low, int step, int size) {
030                this(low, step, size, 0);
031        }
032        
033        public IntRangeIterator(int low, int step, int size, int startIdx) {
034                this.low = low;
035                this.size = size;
036                this.step = step;
037                this.curr = startIdx;
038        }
039
040        @Override
041        public boolean hasNext() {
042                return curr < size;
043        }
044        
045        public int intNext() {
046                if(!hasNext())
047                        throw new NoSuchElementException();
048                return intValue(curr++);
049        }
050
051        @Override
052        public Integer next() {
053                return Integer.valueOf(intNext());
054        }
055
056        @Override
057        public boolean hasPrevious() {
058                return curr > 0;
059        }
060
061        @Override
062        public Integer previous() {
063                return Integer.valueOf(intPrevious());
064        }
065        
066        public int intPrevious() {
067                if(!hasPrevious())
068                        throw new NoSuchElementException();
069                return intValue(--curr);
070        }
071
072        @Override
073        public int nextIndex() {
074                if(!hasNext())
075                        throw new NoSuchElementException();
076                return curr;
077        }
078
079        @Override
080        public int previousIndex() {
081                if(!hasPrevious())
082                        throw new NoSuchElementException();
083                return curr-1;
084        }
085
086        @Override
087        public void remove() {
088                throw new UnsupportedOperationException();
089        }
090
091        @Override
092        public void set(Integer e) {
093                throw new UnsupportedOperationException();
094        }
095
096        @Override
097        public void add(Integer e) {
098                throw new UnsupportedOperationException();
099        }
100        
101        public final int intValue(int idx) {
102                return low + step * idx;
103        }
104        
105        public final Integer value(int idx) {
106                return Integer.valueOf(intValue(idx));
107        }
108}