Top Description Inners Constructors Methods
org.apache.commons.math3.util

public Class IntegerSequence

extends Object
Class Inheritance
Imports
java.util.Iterator, org.apache.commons.math3.exception.MaxCountExceededException, .NullArgumentException, .MathUnsupportedOperationException, .NotStrictlyPositiveException, .ZeroException

Provides a sequence of integers.
Since
3.6

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
IntegerSequence.Incrementor

Utility that increments a counter until a maximum is reached, at which point, the instance will by default throw a MaxCountExceededException.

public static class
IntegerSequence.Range

Generates a sequence of integers.

Constructor Summary

AccessConstructor and Description
private
IntegerSequence()

Utility class contains only static methods.

Method Summary

Modifier and TypeMethod and Description
public static IntegerSequence.Range

Returns:

a range.
range
(int
First value of the range.
start
,
int
Last value of the range.
end
)

Creates a sequence [start ..

public static IntegerSequence.Range

Returns:

a range.
range
(final int
First value of the range.
start
,
final int
Last value of the range that satisfies the above construction rule.
max
,
final int
Increment.
step
)

Creates a sequence \( a_i, i < 0 <= n \) where \( a_i = start + i * step \) and \( n \) is such that \( a_n <= max \) and \( a_{n+1} > max \).

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

IntegerSequenceback to summary
private IntegerSequence()

Utility class contains only static methods.

Method Detail

rangeback to summary
public static IntegerSequence.Range range(int start, int end)

Creates a sequence [start .. end]. It calls range(start, end, 1).

Parameters
start:int

First value of the range.

end:int

Last value of the range.

Returns:IntegerSequence.Range

a range.

rangeback to summary
public static IntegerSequence.Range range(final int start, final int max, final int step)

Creates a sequence \( a_i, i < 0 <= n \) where \( a_i = start + i * step \) and \( n \) is such that \( a_n <= max \) and \( a_{n+1} > max \).

Parameters
start:int

First value of the range.

max:int

Last value of the range that satisfies the above construction rule.

step:int

Increment.

Returns:IntegerSequence.Range

a range.

org.apache.commons.math3.util back to summary

public Class IntegerSequence.Incrementor

extends Object
implements Iterator<Integer>
Class Inheritance
All Implemented Interfaces
java.util.Iterator

Utility that increments a counter until a maximum is reached, at which point, the instance will by default throw a MaxCountExceededException. However, the user is able to override this behaviour by defining a custom callback, in order to e.g. select which exception must be thrown.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
IntegerSequence.Incrementor.MaxCountExceededCallback

Defines a method to be called at counter exhaustion.

Field Summary

Modifier and TypeField and Description
private static final IntegerSequence.Incrementor.MaxCountExceededCallback
CALLBACK

Default callback.

private int
count

Current count.

private final int
increment

Increment.

private final int
init

Initial value the counter.

private final IntegerSequence.Incrementor.MaxCountExceededCallback
maxCountCallback

Function called at counter exhaustion.

private final int
maximalCount

Upper limit for the counter.

Constructor Summary

AccessConstructor and Description
private
Incrementor(int
Initial value.
start
,
int
Maximal count.
max
,
int
Increment.
step
,
IntegerSequence.Incrementor.MaxCountExceededCallback
Function to be called when the maximal count has been reached.
cb
)

Creates an incrementor.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

false if calling increment() will trigger a MaxCountExceededException, true otherwise.
canIncrement
()

Checks whether incrementing the counter nTimes is allowed.

public boolean

Returns:

false if calling increment(nTimes) would call the callback true otherwise.
canIncrement
(int
Number of increments.
nTimes
)

Checks whether incrementing the counter several times is allowed.

public static IntegerSequence.Incrementor

Returns:

an new instance.
create
()

Factory method that creates a default instance.

public int

Returns:

the current count.
getCount
()

Gets the current count.

public int

Returns:

the counter upper limit.
getMaximalCount
()

Gets the upper limit of the counter.

public boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.

public void
increment(int
Number of increments.
nTimes
)

Performs multiple increments.

public void
increment()

Adds the increment value to the current iteration count.

public Integer
next()

Implements java.util.Iterator.next.

Returns the next element in the iteration.

public void
remove()

Overrides default java.util.Iterator.remove.

Not applicable.

public IntegerSequence.Incrementor

Returns:

a new instance.
withCallback
(IntegerSequence.Incrementor.MaxCountExceededCallback
Callback to be called at counter exhaustion.
cb
)

Creates a new instance with a given callback.

public IntegerSequence.Incrementor

Returns:

a new instance.
withIncrement
(int
Increment.
step
)

Creates a new instance with a given increment.

public IntegerSequence.Incrementor

Returns:

a new instance.
withMaximalCount
(int
Maximal count.
max
)

Creates a new instance with a given maximal count.

public IntegerSequence.Incrementor

Returns:

a new instance.
withStart
(int
Initial value of the counter.
start
)

Creates a new instance with a given initial value.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CALLBACKback to summary
private static final IntegerSequence.Incrementor.MaxCountExceededCallback CALLBACK

Default callback.

countback to summary
private int count

Current count.

incrementback to summary
private final int increment

Increment.

initback to summary
private final int init

Initial value the counter.

maxCountCallbackback to summary
private final IntegerSequence.Incrementor.MaxCountExceededCallback maxCountCallback

Function called at counter exhaustion.

maximalCountback to summary
private final int maximalCount

Upper limit for the counter.

Constructor Detail

Incrementorback to summary
private Incrementor(int start, int max, int step, IntegerSequence.Incrementor.MaxCountExceededCallback cb) throws NullArgumentException

Creates an incrementor. The counter will be exhausted either when max is reached or when nTimes increments have been performed.

Parameters
start:int

Initial value.

max:int

Maximal count.

step:int

Increment.

cb:IntegerSequence.Incrementor.MaxCountExceededCallback

Function to be called when the maximal count has been reached.

Exceptions
NullArgumentException:
if cb is null.

Method Detail

canIncrementback to summary
public boolean canIncrement()

Checks whether incrementing the counter nTimes is allowed.

Returns:boolean

false if calling increment() will trigger a MaxCountExceededException, true otherwise.

canIncrementback to summary
public boolean canIncrement(int nTimes)

Checks whether incrementing the counter several times is allowed.

Parameters
nTimes:int

Number of increments.

Returns:boolean

false if calling increment(nTimes) would call the callback true otherwise.

createback to summary
public static IntegerSequence.Incrementor create()

Factory method that creates a default instance. The initial and maximal values are set to 0. For the new instance to be useful, the maximal count must be set by calling withMaximalCount.

Returns:IntegerSequence.Incrementor

an new instance.

getCountback to summary
public int getCount()

Gets the current count.

Returns:int

the current count.

getMaximalCountback to summary
public int getMaximalCount()

Gets the upper limit of the counter.

Returns:int

the counter upper limit.

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if the iteration has more elements

incrementback to summary
public void increment(int nTimes) throws MaxCountExceededException

Performs multiple increments.

Parameters
nTimes:int

Number of increments.

Exceptions
MaxCountExceededException:
at counter exhaustion.
NotStrictlyPositiveException:
if nTimes <= 0.
See Also
increment()
incrementback to summary
public void increment() throws MaxCountExceededException

Adds the increment value to the current iteration count. At counter exhaustion, this method will call the trigger method of the callback object passed to the withCallback(MaxCountExceededCallback) method. If not explicitly set, a default callback is used that will throw a MaxCountExceededException.

Exceptions
MaxCountExceededException:
at counter exhaustion, unless a custom callback has been set.
See Also
increment(int)
nextback to summary
public Integer next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

Returns:Integer

the next element in the iteration

removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Not applicable.

withCallbackback to summary
public IntegerSequence.Incrementor withCallback(IntegerSequence.Incrementor.MaxCountExceededCallback cb)

Creates a new instance with a given callback. The counter is reset to the initial value.

Parameters
cb:IntegerSequence.Incrementor.MaxCountExceededCallback

Callback to be called at counter exhaustion.

Returns:IntegerSequence.Incrementor

a new instance.

withIncrementback to summary
public IntegerSequence.Incrementor withIncrement(int step)

Creates a new instance with a given increment. The counter is reset to the initial value.

Parameters
step:int

Increment.

Returns:IntegerSequence.Incrementor

a new instance.

withMaximalCountback to summary
public IntegerSequence.Incrementor withMaximalCount(int max)

Creates a new instance with a given maximal count. The counter is reset to the initial value.

Parameters
max:int

Maximal count.

Returns:IntegerSequence.Incrementor

a new instance.

withStartback to summary
public IntegerSequence.Incrementor withStart(int start)

Creates a new instance with a given initial value. The counter is reset to the initial value.

Parameters
start:int

Initial value of the counter.

Returns:IntegerSequence.Incrementor

a new instance.

org.apache.commons.math3.util back to summary

public Interface IntegerSequence.Incrementor.MaxCountExceededCallback


Defines a method to be called at counter exhaustion. The trigger method should usually throw an exception.

Method Summary

Modifier and TypeMethod and Description
public void
trigger(int
Maximal count.
maximalCount
)

Function called when the maximal count has been reached.

Method Detail

triggerback to summary
public void trigger(int maximalCount) throws MaxCountExceededException

Function called when the maximal count has been reached.

Parameters
maximalCount:int

Maximal count.

Exceptions
MaxCountExceededException:
at counter exhaustion
org.apache.commons.math3.util back to summary

public Class IntegerSequence.Range

extends Object
implements Iterable<Integer>
Class Inheritance
All Implemented Interfaces
java.lang.Iterable

Generates a sequence of integers.

Field Summary

Modifier and TypeField and Description
private final int
max

Final value.

private final int
size

Number of integers contained in this range.

private final int
start

First value.

private final int
step

Increment.

Constructor Summary

AccessConstructor and Description
public
Range(int
First value of the range.
start
,
int
Last value of the range that satisfies the above construction rule.
max
,
int
Increment.
step
)

Creates a sequence \( a_i, i < 0 <= n \) where \( a_i = start + i * step \) and \( n \) is such that \( a_n <= max \) and \( a_{n+1} > max \).

Method Summary

Modifier and TypeMethod and Description
public Iterator<Integer>
iterator()

Implements java.lang.Iterable.iterator.

Returns an iterator over elements of type T.

public int

Returns:

the size of the range.
size
()

Gets the number of elements contained in the range.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

maxback to summary
private final int max

Final value.

sizeback to summary
private final int size

Number of integers contained in this range.

startback to summary
private final int start

First value.

stepback to summary
private final int step

Increment.

Constructor Detail

Rangeback to summary
public Range(int start, int max, int step)

Creates a sequence \( a_i, i < 0 <= n \) where \( a_i = start + i * step \) and \( n \) is such that \( a_n <= max \) and \( a_{n+1} > max \).

Parameters
start:int

First value of the range.

max:int

Last value of the range that satisfies the above construction rule.

step:int

Increment.

Method Detail

iteratorback to summary
public Iterator<Integer> iterator()

Implements java.lang.Iterable.iterator.

Doc from java.lang.Iterable.iterator.

Returns an iterator over elements of type T.

Returns:Iterator<Integer>

an Iterator.

sizeback to summary
public int size()

Gets the number of elements contained in the range.

Returns:int

the size of the range.