Package net.solarnetwork.util
Class IntRange
java.lang.Object
net.solarnetwork.util.IntRange
- All Implemented Interfaces:
Serializable,Comparable<IntRange>
An immutable integer range with min/max values.
Inspired and adapted from PCJ's
bak.pcj.set.IntRange class.
- Since:
- 1.58
- Version:
- 1.0
- Author:
- matt
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanTest if this range is adjacent to (but not intersecting) a given range.booleanTest if this range could be merged with another range.intCompares this object with the specified object for order.booleancontains(int value) Test if a value is within this range, inclusive.booleancontainsAll(int min, int max) Test if another range is completely within this range, inclusive.booleanTest if another range is completely within this range, inclusive.booleanintgetMax()Get the maximum value.intgetMin()Get the minimum value.inthashCode()booleanTest if this range intersects with a given range.booleanTest if this range represents a singleton value, where the minimum and maximum values in the range are equal.intlength()Get the number of integer values betweenminandmax, inclusive.Merge this range with a given range, returning the merged range.static IntRangerangeOf(int value) Create a singleton range (where the minimum and maximum are the same).static IntRangerangeOf(int min, int max) Create a range.toString()
-
Constructor Details
-
IntRange
public IntRange(int min, int max) Constructor.Note that if
min > maxthengetMin()will returnmaxandgetMax()will returnmin. That is, the minimum and maximum values passed to this constructor will be compared before storing in this class so thatgetMin()always returns the actual minimum value.- Parameters:
min- the minimum valuemax- the maximum value
-
-
Method Details
-
rangeOf
Create a singleton range (where the minimum and maximum are the same).- Parameters:
value- the singleton value- Returns:
- the new range
-
rangeOf
Create a range.- Parameters:
min- the minimum valuemax- the maximum value- Returns:
- the new range
-
getMin
public int getMin()Get the minimum value.- Returns:
- the minimum
-
getMax
public int getMax()Get the maximum value.- Returns:
- the maximum
-
length
public int length()Get the number of integer values betweenminandmax, inclusive.- Returns:
- the inclusive length between
minandmax
-
isSingleton
public boolean isSingleton()Test if this range represents a singleton value, where the minimum and maximum values in the range are equal.- Returns:
- true if
min == max
-
contains
public boolean contains(int value) Test if a value is within this range, inclusive.- Parameters:
value- the value to test- Returns:
- true if
min <= value <= max
-
containsAll
public boolean containsAll(int min, int max) Test if another range is completely within this range, inclusive.- Parameters:
min- the minimum of the range to testmax- the maximum of the range to test- Returns:
- true if
this.min <= min <= max <= this.max
-
containsAll
Test if another range is completely within this range, inclusive.- Parameters:
o- the range to test- Returns:
- true if
this.min <= o.min <= o.max <= this.max
-
intersects
Test if this range intersects with a given range.- Parameters:
o- the range to compare to this range- Returns:
- true if this range intersects (overlaps) with the given range
- Throws:
NullPointerException- ifois null
-
adjacentTo
Test if this range is adjacent to (but not intersecting) a given range.- Parameters:
o- the range to compare to this range- Returns:
- true if this range is adjacent to the given range
- Throws:
NullPointerException- ifois null
-
canMergeWith
Test if this range could be merged with another range.Two ranges can be merged if they are either adjacent to or intersect with each other.
- Parameters:
o- the range to test- Returns:
- true if this range is either adjacent to or intersects with the given range
-
mergeWith
Merge this range with a given range, returning the merged range.- Parameters:
o- the range to merge with this range- Returns:
- the new merged range
- Throws:
IllegalArgumentException- if the this range cannot be merged with the given range
-
compareTo
Compares this object with the specified object for order.This implementation only compares the
minvalues of each range.- Specified by:
compareToin interfaceComparable<IntRange>
-
hashCode
public int hashCode() -
equals
-
toString
-