IntegerRange

public final class org.apache.commons.lang3.IntegerRange extends NumberRange<Integer>[source]

Specializes NumberRange for Integers.

This class is not designed to interoperate with other NumberRanges

Since:

3.13.0

Methods

int

fit(int element)

Fits the given value into this range by returning the given value or, if out of bounds, the range minimum if below, or the range maximum if above.

static IntegerRange

of(int fromInclusive, int toInclusive)

Creates a closed range with the specified minimum and maximum values (both inclusive).

static IntegerRange

of(Integer fromInclusive, Integer toInclusive)

Creates a closed range with the specified minimum and maximum values (both inclusive).

IntStream

toIntStream()

Returns a sequential ordered IntStream from Range.getMinimum() (inclusive) to Range.getMaximum() (inclusive) by an incremental step of 1.

public int fit(int element)[source]

Fits the given value into this range by returning the given value or, if out of bounds, the range minimum if below, or the range maximum if above.

IntegerRange range = IntegerRange.of(16, 64);
range.fit(-9) -->  16
range.fit(0)  -->  16
range.fit(15) -->  16
range.fit(16) -->  16
range.fit(17) -->  17
...
range.fit(63) -->  63
range.fit(64) -->  64
range.fit(99) -->  64
Parameters:

element – The element to test.

Returns:

The minimum, the element, or the maximum depending on the element's location relative to the range.

Since:

3.19.0

public static IntegerRange of(int fromInclusive, int toInclusive)[source]

Creates a closed range with the specified minimum and maximum values (both inclusive).

The range uses the natural ordering of the elements to determine where values lie in the range.

The arguments may be passed in the order (min, max) or (max,min). The getMinimum and getMaximum methods will return the correct values.

Parameters:
  • fromInclusive – The first value that defines the edge of the range, inclusive.

  • toInclusive – The second value that defines the edge of the range, inclusive.

Returns:

The range object, not null.

public static IntegerRange of(Integer fromInclusive, Integer toInclusive)[source]

Creates a closed range with the specified minimum and maximum values (both inclusive).

The range uses the natural ordering of the elements to determine where values lie in the range.

The arguments may be passed in the order (min, max) or (max,min). The getMinimum and getMaximum methods will return the correct values.

Parameters:
  • fromInclusive – The first value that defines the edge of the range, inclusive.

  • toInclusive – The second value that defines the edge of the range, inclusive.

Returns:

The range object, not null.

Throws:

NullPointerException – Thrown if either element is null.

public IntStream toIntStream()[source]

Returns a sequential ordered IntStream from Range.getMinimum() (inclusive) to Range.getMaximum() (inclusive) by an incremental step of 1.

Returns:

A sequential IntStream for the range of int elements.

Since:

3.18.0