View Javadoc

1   package cz.cuni.amis.utils.iterators;
2   
3   /**
4    * Circular iterator that can start from any given position in given iterable.
5    * Even less tested then {@link CircularIterator} or {@link CircularListIterator}.
6    * @author Radek 'Black_Hand' Pibil
7    *
8    * @param <E>
9    */
10  public class ShiftedCircularIterator<E> extends CircularIterator<E> {
11  	
12  	private int steps;
13  	private int passedSteps = 0;
14  
15  	public ShiftedCircularIterator(Iterable<E> toIterateOver, int steps) {
16  		super(toIterateOver);
17  		
18  		for (int i = 0; i < steps; ++i)
19  			next();
20  		
21  		this.steps = steps;
22  		passedSteps = 0;
23  	}
24  	
25  	@Override
26  	public boolean hasNext() {
27  		return getIterable().iterator().hasNext();
28  	}
29  
30  	@Override
31  	public E next() {
32  		
33  		E value = getIterator().next();
34  		++passedSteps;
35  		
36  		if (!getIterator().hasNext()) {
37  			restartIterator();
38  			passedSteps = 0;
39  		}
40  		
41  		if (passedSteps == steps)
42  			passedEnd = true;
43  		
44  		return value;
45  	}
46  
47  	@Override
48  	public void remove() {
49  		getIterator().remove();
50  	}
51  	
52  	public boolean hasPassedEnd() {
53  		if (passedEnd) {
54  			passedEnd = false;
55  			return passedEnd;
56  		}
57  		return false;
58  	}
59  }