Package it.unimi.di.prog2.s15
Class IntRange
java.lang.Object
it.unimi.di.prog2.s15.IntRange
A mutable class representing a range of integers, with configurable initial from, to, and step
values.
The class is iterable; when the iterator is created, if the step is positive, the initial value must be less than the final value, and conversely if the step is negative; once created, the iterator is not affected by successive modification in the range.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionIntRange()
Constructs the range fromInteger.MIN_VALUE
toInteger.MAX_VALUE
(exclusive) with step 1. -
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
from
private int fromThe first number of the iteration. -
to
private int toThe last number of the iteration. -
step
private int stepThe iteration step.
-
-
Constructor Details
-
IntRange
public IntRange()Constructs the range fromInteger.MIN_VALUE
toInteger.MAX_VALUE
(exclusive) with step 1.
-
-
Method Details
-
from
public void from(int from) Sets the initial value.- Parameters:
from
- the initial value.
-
to
public void to(int to) Sets the end value.- Parameters:
to
- the end value.
-
step
public void step(int step) Sets the step.- Parameters:
step
- the step value.- Throws:
IllegalArgumentException
- if the step is 0.
-
toString
-
iterator
Returns an iterator for this range.If the range is modified after the iterator is returned, the modifications will not be reflected in the iterator.
- Specified by:
iterator
in interfaceIterable<Integer>
- Returns:
- the iterator.
- Throws:
IllegalArgumentException
- if the initial value is less than the final, but the step is negative, or conversely if the initial value is greater than the final, but the step is positive.
-