org.apache.commons.math3.util

## Class Incrementor

- java.lang.Object
- - org.apache.commons.math3.util.Incrementor

    public class Incrementor
    extends Object

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.

Since:
  3.0
Version:
  $Id: Incrementor.java 1364389 2012-07-22 18:19:26Z tn $

### Nested Class Summary

Nested Classes
`static interface` `Incrementor.MaxCountExceededCallback` Defines a method to be called at counter exhaustion.

### Field Summary

Fields
`private int` `count` Current count.
`private Incrementor.MaxCountExceededCallback` `maxCountCallback` Function called at counter exhaustion.
`private int` `maximalCount` Upper limit for the counter.

### Constructor Summary

Constructors
Constructor and Description
`Incrementor()` Default constructor.
`Incrementor(int max)` Defines a maximal count.
`Incrementor(int max, Incrementor.MaxCountExceededCallback cb)` Defines a maximal count and a callback method to be triggered at counter exhaustion.

### Method Summary

Methods
`boolean` `canIncrement()` Checks whether a single increment is allowed.
`int` `getCount()` Gets the current count.
`int` `getMaximalCount()` Gets the upper limit of the counter.
`void` `incrementCount()` Adds one to the current iteration count.
`void` `incrementCount(int value)` Performs multiple increments.
`void` `resetCount()` Resets the counter to 0.
`void` `setMaximalCount(int max)` Sets the upper limit for the counter.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### maximalCount

    private int maximalCount

Upper limit for the counter.

#### count

    private int count

Current count.

#### maxCountCallback

    private final Incrementor.MaxCountExceededCallback maxCountCallback

Function called at counter exhaustion.

### Constructor Detail

#### Incrementor

    public Incrementor()

Default constructor. For the new instance to be useful, the maximal count must be set by calling `setMaximalCount`.

#### Incrementor

    public Incrementor(int max)

Defines a maximal count.

Parameters:
  `max` - Maximal count.

#### Incrementor

    public Incrementor(int max,
               Incrementor.MaxCountExceededCallback cb)

Defines a maximal count and a callback method to be triggered at counter exhaustion.

Parameters:
  `max` - Maximal count.
  `cb` - Function to be called when the maximal count has been reached.
Throws:
  `NullArgumentException` - if `cb` is `null`

### Method Detail

#### setMaximalCount

    public void setMaximalCount(int max)

Sets the upper limit for the counter. This does not automatically reset the current count to zero (see `resetCount()`).

Parameters:
  `max` - Upper limit of the counter.

#### getMaximalCount

    public int getMaximalCount()

Gets the upper limit of the counter.

Returns:
  the counter upper limit.

#### getCount

    public int getCount()

Gets the current count.

Returns:
  the current count.

#### canIncrement

    public boolean canIncrement()

Checks whether a single increment is allowed.

Returns:
  `false` if the next call to `incrementCount` will trigger a `MaxCountExceededException`, `true` otherwise.

#### incrementCount

    public void incrementCount(int value)

Performs multiple increments. See the other `incrementCount` method).

Parameters:
  `value` - Number of increments.
Throws:
  `MaxCountExceededException` - at counter exhaustion.

#### incrementCount

    public void incrementCount()

Adds one to the current iteration count. At counter exhaustion, this method will call the `trigger` method of the callback object passed to the `constructor`. If not explictly set, a default callback is used that will throw a `MaxCountExceededException`.

Throws:
  `MaxCountExceededException` - at counter exhaustion, unless a custom `callback` has been set at construction.

#### resetCount

    public void resetCount()

Resets the counter to 0.