@GwtCompatible
public class ExclusiveRange
extends java.lang.Object
implements java.lang.Iterable<java.lang.Integer>
start
to end
counting up or down.
It excludes the end
value when counting up and the start
value when counting down.
Examples:
new ExclusiveRange(1, 5, true) | (1,2,3,4) |
new ExclusiveRange(0, 0, true) | () |
new ExclusiveRange(0, -1, true) | () |
new ExclusiveRange(-1, 0, true) | (-1) |
new ExclusiveRange(5, 1, false) | (4,3,2,1) |
new ExclusiveRange(0, 0, false) | () |
new ExclusiveRange(-1, 0, false) | () |
new ExclusiveRange(0, -1, false) | (-1) |
IntegerRange
this class meets the requirements to iterate arrays or lists without
the need for further guards, e.g.
for(i: new ExclusiveRange(0, list.size, true)) list.get(i)... for(i: new ExclusiveRange(array.length, 0, false)) array.get(i)... for(i: new ExclusiveRange(0, string.indexOf('x'), true) string.charAt(i)...
Constructor and Description |
---|
ExclusiveRange(int start,
int end,
boolean increment)
Constructs a new ExclusiveRange object.
|
Modifier and Type | Method and Description |
---|---|
boolean |
contains(int number)
Checks whether this contains the given number, i.e. whether the iterator will yield the number.
|
boolean |
isEmpty()
Returns whether this is range is empty.
|
java.util.ListIterator<java.lang.Integer> |
iterator() |
int |
size()
Returns the number of elements in this ExclusiveRange.
|
@Pure public ExclusiveRange(int start, int end, boolean increment)
start
- the start valueend
- the end valueincrement
- if true, the range goes from start up to end (exclusive)
if false, the range goes from end down to start (exclusive)@Pure public java.util.ListIterator<java.lang.Integer> iterator()
iterator
in interface java.lang.Iterable<java.lang.Integer>
ListIterator
for this.@Pure public int size()
@Pure public boolean isEmpty()
@Pure public boolean contains(int number)
0..2.by(2)
will not contain 1.number
- the number to be checked for containment.