001package net.automatalib.commons.util.collections; 002 003import java.util.ListIterator; 004import java.util.NoSuchElementException; 005 006public class IntRangeIterator implements ListIterator<Integer> { 007 008 private final int low; 009 private final int step; 010 private final int size; 011 private int curr; 012 013 public IntRangeIterator(int low, int size, int step) { 014 this(low, size, step, 0); 015 } 016 017 public IntRangeIterator(int low, int size, int step, int startIdx) { 018 this.low = low; 019 this.size = size ; 020 this.step = step; 021 this.curr = low + step * startIdx; 022 } 023 024 @Override 025 public boolean hasNext() { 026 return curr < size; 027 } 028 029 public int intNext() { 030 if(!hasNext()) 031 throw new NoSuchElementException(); 032 return intValue(curr++); 033 } 034 035 @Override 036 public Integer next() { 037 return Integer.valueOf(intNext()); 038 } 039 040 @Override 041 public boolean hasPrevious() { 042 return curr > 0; 043 } 044 045 @Override 046 public Integer previous() { 047 return Integer.valueOf(intPrevious()); 048 } 049 050 public int intPrevious() { 051 if(!hasPrevious()) 052 throw new NoSuchElementException(); 053 return intValue(--curr); 054 } 055 056 @Override 057 public int nextIndex() { 058 if(!hasNext()) 059 throw new NoSuchElementException(); 060 return curr; 061 } 062 063 @Override 064 public int previousIndex() { 065 if(!hasPrevious()) 066 throw new NoSuchElementException(); 067 return curr-1; 068 } 069 070 @Override 071 public void remove() { 072 throw new UnsupportedOperationException(); 073 } 074 075 @Override 076 public void set(Integer e) { 077 throw new UnsupportedOperationException(); 078 } 079 080 @Override 081 public void add(Integer e) { 082 throw new UnsupportedOperationException(); 083 } 084 085 public final int intValue(int idx) { 086 return low + step * idx; 087 } 088 089 public final Integer value(int idx) { 090 return Integer.valueOf(intValue(idx)); 091 } 092}