-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRange.java
More file actions
41 lines (35 loc) · 1.12 KB
/
Copy pathRange.java
File metadata and controls
41 lines (35 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.NoSuchElementException;
public class Range implements IntegerSequence{
private int start,end,current;
//@param start : the starting value (inclusive) which must be <= end.
//@param end : the ending value which is also inclusive.
public Range(int start, int end){
if (start > end){
throw new NoSuchElementException("Start was greater than end.");
}else{
this.start = start;
this.end = end;
this.current = start;
}
}
public void reset(){
current = start;
}
public int length(){
return (this.end-this.start+1);
}
//When current is no longer a valid element in the range, it should return false.
public boolean hasNext(){
return (current <= end) && (current >= start);
}
//@throws NoSuchElementException when hasNext() is false.
//This will return the current value, it will also increase current value by 1.
//e.g. if current is 5. This will make current 6, and return 5.
public int next(){
if (!hasNext()){
throw new NoSuchElementException();
}
current++;
return current - 1;
}
}