Package org.apache.commons.lang3
Class IntegerRange
- java.lang.Object
-
- org.apache.commons.lang3.Range<N>
-
- org.apache.commons.lang3.NumberRange<java.lang.Integer>
-
- org.apache.commons.lang3.IntegerRange
-
- All Implemented Interfaces:
java.io.Serializable
public final class IntegerRange extends NumberRange<java.lang.Integer>
SpecializesNumberRangeforIntegers.This class is not designed to interoperate with other NumberRanges
- Since:
- 3.13.0
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private static longserialVersionUID
-
Constructor Summary
Constructors Modifier Constructor Description privateIntegerRange(java.lang.Integer number1, java.lang.Integer number2)Creates a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intfit(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 IntegerRangeof(int fromInclusive, int toInclusive)Creates a closed range with the specified minimum and maximum values (both inclusive).static IntegerRangeof(java.lang.Integer fromInclusive, java.lang.Integer toInclusive)Creates a closed range with the specified minimum and maximum values (both inclusive).java.util.stream.IntStreamtoIntStream()Returns a sequential orderedIntStreamfromRange.getMinimum()(inclusive) toRange.getMaximum()(inclusive) by an incremental step of1.-
Methods inherited from class org.apache.commons.lang3.Range
between, between, contains, containsRange, elementCompareTo, equals, fit, getComparator, getMaximum, getMinimum, hashCode, intersectionWith, is, is, isAfter, isAfterRange, isBefore, isBeforeRange, isEndedBy, isNaturalOrdering, isOverlappedBy, isStartedBy, of, of, toString, toString
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
IntegerRange
private IntegerRange(java.lang.Integer number1, java.lang.Integer number2)Creates a new instance.- Parameters:
number1- the first element, not nullnumber2- the second element, not null- Throws:
java.lang.NullPointerException- when element1 is null.java.lang.NullPointerException- when element2 is null.
-
-
Method Detail
-
of
public static IntegerRange of(int fromInclusive, int toInclusive)
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.
-
of
public static IntegerRange of(java.lang.Integer fromInclusive, java.lang.Integer toInclusive)
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:
java.lang.IllegalArgumentException- if either element is null.
-
fit
public 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.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
-
toIntStream
public java.util.stream.IntStream toIntStream()
Returns a sequential orderedIntStreamfromRange.getMinimum()(inclusive) toRange.getMaximum()(inclusive) by an incremental step of1.- Returns:
- a sequential
IntStreamfor the range ofintelements - Since:
- 3.18.0
-
-