Package org.eclipse.xtext.xbase.lib
Class IntegerRange
- java.lang.Object
-
- org.eclipse.xtext.xbase.lib.IntegerRange
-
- All Implemented Interfaces:
java.lang.Iterable<java.lang.Integer>
@GwtCompatible public class IntegerRange extends java.lang.Object implements java.lang.Iterable<java.lang.Integer>
A sequence of integers starting fromstart
up toend
with an increment ofstep
. The value ofstart
is always included. The value ofend
is included only if the difference betweenend-start
is a multiple ofstep
. Thestep
can be positive or negative, but never 0. It must have the same signum as ofend-start
.- Since:
- 2.3
-
-
Constructor Summary
Constructors Constructor Description IntegerRange(int start, int end)
Constructs a new IntegerRange object.IntegerRange(int start, int end, int step)
Constructs a new IntegerRange object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
contains(int number)
Checks whether this contains the given number, i.e.int
getEnd()
int
getSize()
Returns the number of elements in this IntegerRange.int
getStart()
int
getStep()
java.util.ListIterator<java.lang.Integer>
iterator()
IntegerRange
withStep(int step)
-
-
-
Constructor Detail
-
IntegerRange
@Pure public IntegerRange(int start, int end)
Constructs a new IntegerRange object. The step will be set to -1 if end<start or to 1 otherwise.- Parameters:
start
- the start value (inclusive)end
- the end value (inclusive)
-
IntegerRange
@Pure public IntegerRange(int start, int end, int step)
Constructs a new IntegerRange object.- Parameters:
start
- the start value (inclusive)end
- the end value (inclusive if end-start%step == 0)step
- the increment
-
-
Method Detail
-
iterator
@Pure public java.util.ListIterator<java.lang.Integer> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<java.lang.Integer>
- Returns:
- a read-only
ListIterator
for this.
-
getStart
@Pure public int getStart()
- Returns:
- the start value
-
getStep
@Pure public int getStep()
- Returns:
- the step value
-
getEnd
@Pure public int getEnd()
- Returns:
- the end value
-
getSize
@Pure public int getSize()
Returns the number of elements in this IntegerRange. That is not the length of the interval, but(end-start)/step + 1
.- Returns:
- the number of elements in this IntegerRange.
-
withStep
@Pure public IntegerRange withStep(int step)
- Parameters:
step
- the step of the new range.- Returns:
- a new IntegerRange with the given step.
-
contains
@Pure public boolean contains(int number)
Checks whether this contains the given number, i.e. whether the iterator will yield the number. This is different from interval containment:0..2.by(2)
will not contain 1.- Parameters:
number
- the number to be checked for containment.- Returns:
- whether this sequence contains the given number or not.
-
-