Top Description Inners Fields Constructors Methods
org.apache.commons.lang3

public Class Range<T>

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Known Direct Subclasses
org.apache.commons.lang3.NumberRange
Type Parameters
<T>
The type of range values.
Imports
java.io.Serializable, java.util.Comparator, .Objects

An immutable range of objects from a minimum to maximum point inclusive.

The objects need to either be implementations of Comparable or you need to supply a Comparator.

#ThreadSafe# if the objects and comparator are thread-safe.

Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static enum

Field Summary

Modifier and TypeField and Description
private final Comparator<T>
comparator

The ordering scheme used in this range.

private transient int
hashCode

Cached output hashCode (class is immutable).

private final T
maximum

The maximum value in this range (inclusive).

private final T
minimum

The minimum value in this range (inclusive).

private static final long
serialVersionUID

Serialization version.

private transient String
toString

Cached output toString (class is immutable).

Constructor Summary

AccessConstructor and Description
pack-priv
Range(final T
the first element, not null.
element1
,
final T
the second element, not null
element2
,
final Comparator<T>
the comparator to be used, null for natural ordering.
comp
)

Creates an instance.

Method Summary

Modifier and TypeMethod and Description
public static <
the type of the elements in this range.
T extends Comparable<? super T>
>
Range<T>

Returns:

the range object, not null.
between
(final T
the first value that defines the edge of the range, inclusive.
fromInclusive
,
final T
the second value that defines the edge of the range, inclusive.
toInclusive
)
Deprecated Use of(Comparable, Comparable).

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

public static <
the type of the elements in this range.
T
>
Range<T>

Returns:

the range object, not null.
between
(final T
the first value that defines the edge of the range, inclusive.
fromInclusive
,
final T
the second value that defines the edge of the range, inclusive.
toInclusive
,
final Comparator<T>
the comparator to be used, null for natural ordering.
comparator
)

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

public boolean

Returns:

true if the specified element occurs within this range.
contains
(final T
the element to check for, null returns false.
element
)

Checks whether the specified element occurs within this range.

public boolean

Returns:

true if this range contains the specified range.
containsRange
(final Range<T>
the range to check, null returns false.
otherRange
)

Checks whether this range contains all the elements of the specified range.

public int

Returns:

-1, 0 or +1 depending on the element's location relative to the range.
elementCompareTo
(final T
the element to check for, not null.
element
)

Checks where the specified element occurs relative to this range.

public boolean

Returns:

true if this object is equal.
equals
(final Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Compares this range to another object to test if they are equal.

public T

Returns:

the minimum, the element, or the maximum depending on the element's location relative to the range.
fit
(final T
the element to check for, not null.
element
)

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

public Comparator<T>

Returns:

the comparator being used, not null.
getComparator
()

Gets the comparator being used to determine if objects are within the range.

public T

Returns:

the maximum value in this range, not null.
getMaximum
()

Gets the maximum value in this range.

public T

Returns:

the minimum value in this range, not null.
getMinimum
()

Gets the minimum value in this range.

public int

Returns:

a hash code value for this object.
hashCode
()

Overrides java.lang.Object.hashCode.

Gets a suitable hash code for the range.

public Range<T>

Returns:

range representing the intersection of this and other (this if equal).
intersectionWith
(final Range<T>
overlapping Range.
other
)

Calculate the intersection of this and an overlapping Range.

public static <
the type of the elements in this range.
T extends Comparable<? super T>
>
Range<T>

Returns:

the range object, not null.
is
(final T
the value to use for this range, not null.
element
)

Creates a range using the specified element as both the minimum and maximum in this range.

public static <
the type of the elements in this range.
T
>
Range<T>

Returns:

the range object, not null.
is
(final T
the value to use for this range, must not be null.
element
,
final Comparator<T>
the comparator to be used, null for natural ordering.
comparator
)

Creates a range using the specified element as both the minimum and maximum in this range.

public boolean

Returns:

true if this range is entirely after the specified element.
isAfter
(final T
the element to check for, null returns false.
element
)

Checks whether this range is after the specified element.

public boolean

Returns:

true if this range is completely after the specified range.
isAfterRange
(final Range<T>
the range to check, null returns false.
otherRange
)

Checks whether this range is completely after the specified range.

public boolean

Returns:

true if this range is entirely before the specified element.
isBefore
(final T
the element to check for, null returns false.
element
)

Checks whether this range is before the specified element.

public boolean

Returns:

true if this range is completely before the specified range.
isBeforeRange
(final Range<T>
the range to check, null returns false.
otherRange
)

Checks whether this range is completely before the specified range.

public boolean

Returns:

true if the specified element occurs within this range.
isEndedBy
(final T
the element to check for, null returns false.
element
)

Checks whether this range ends with the specified element.

public boolean

Returns:

true if using natural ordering.
isNaturalOrdering
()

Tests whether or not the Range is using the natural ordering of the elements.

public boolean

Returns:

true if the specified range overlaps with this range; otherwise, false.
isOverlappedBy
(final Range<T>
the range to test, null returns false.
otherRange
)

Tests whether this range is overlapped by the specified range.

public boolean

Returns:

true if the specified element occurs within this range.
isStartedBy
(final T
the element to check for, null returns false.
element
)

Tests whether this range starts with the specified element.

public static <
the type of the elements in this range.
T extends Comparable<? super T>
>
Range<T>

Returns:

the range object, not null.
of
(final T
the first value that defines the edge of the range, inclusive.
fromInclusive
,
final T
the second value that defines the edge of the range, inclusive.
toInclusive
)

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

public static <
the type of the elements in this range.
T
>
Range<T>

Returns:

the range object, not null.
of
(final T
the first value that defines the edge of the range, inclusive.
fromInclusive
,
final T
the second value that defines the edge of the range, inclusive.
toInclusive
,
final Comparator<T>
the comparator to be used, null for natural ordering.
comparator
)

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

public String

Returns:

the String representation of this range.
toString
()

Overrides java.lang.Object.toString.

Gets the range as a String.

public String

Returns:

the formatted string, not null.
toString
(final String
the format string, optionally containing %1$s, %2$s and %3$s, not null.
format
)

Formats the receiver using the given format.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

comparatorback to summary
private final Comparator<T> comparator

The ordering scheme used in this range.

hashCodeback to summary
private transient int hashCode

Cached output hashCode (class is immutable).

maximumback to summary
private final T maximum

The maximum value in this range (inclusive).

minimumback to summary
private final T minimum

The minimum value in this range (inclusive).

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version.

See Also
java.io.Serializable
toStringback to summary
private transient String toString

Cached output toString (class is immutable).

Constructor Detail

Rangeback to summary
pack-priv Range(final T element1, final T element2, final Comparator<T> comp)

Creates an instance.

Parameters
element1:T

the first element, not null.

element2:T

the second element, not null

comp:Comparator<T>

the comparator to be used, null for natural ordering.

Annotations
@SuppressWarnings:unchecked
Exceptions
NullPointerException:
  • when element1 is null.
  • when element2 is null.

Method Detail

betweenback to summary
public static <T extends Comparable<? super T>> Range<T> between(final T fromInclusive, final T toInclusive)

Creates a 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
<T>
the type of the elements in this range.
fromInclusive:T

the first value that defines the edge of the range, inclusive.

toInclusive:T

the second value that defines the edge of the range, inclusive.

Returns:Range<T>

the range object, not null.

Annotations
@Deprecated
Exceptions
NullPointerException:
  • when fromInclusive is null.
  • when toInclusive is null.
ClassCastException:
if the elements are not Comparable.
betweenback to summary
public static <T> Range<T> between(final T fromInclusive, final T toInclusive, final Comparator<T> comparator)

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

The range uses the specified Comparator 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
<T>
the type of the elements in this range.
fromInclusive:T

the first value that defines the edge of the range, inclusive.

toInclusive:T

the second value that defines the edge of the range, inclusive.

comparator:Comparator<T>

the comparator to be used, null for natural ordering.

Returns:Range<T>

the range object, not null.

Annotations
@Deprecated
Exceptions
NullPointerException:
  • when fromInclusive is null.
  • when toInclusive is null.
ClassCastException:
if using natural ordering and the elements are not Comparable.
containsback to summary
public boolean contains(final T element)

Checks whether the specified element occurs within this range.

Parameters
element:T

the element to check for, null returns false.

Returns:boolean

true if the specified element occurs within this range.

containsRangeback to summary
public boolean containsRange(final Range<T> otherRange)

Checks whether this range contains all the elements of the specified range.

This method may fail if the ranges have two different comparators or element types.

Parameters
otherRange:Range<T>

the range to check, null returns false.

Returns:boolean

true if this range contains the specified range.

Exceptions
RuntimeException:
if ranges cannot be compared.
elementCompareToback to summary
public int elementCompareTo(final T element)

Checks where the specified element occurs relative to this range.

The API is reminiscent of the Comparable interface returning -1 if the element is before the range, 0 if contained within the range and 1 if the element is after the range.

Parameters
element:T

the element to check for, not null.

Returns:int

-1, 0 or +1 depending on the element's location relative to the range.

Exceptions
NullPointerException:
if element is null.
equalsback to summary
public boolean equals(final Object obj)

Overrides java.lang.Object.equals.

Compares this range to another object to test if they are equal.

To be equal, the minimum and maximum values must be equal, which ignores any differences in the comparator.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is equal.

Annotations
@Override
fitback to summary
public T fit(final T element)

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

Range<Integer> range = Range.between(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:T

the element to check for, not null.

Returns:T

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

Exceptions
NullPointerException:
if element is null.
Since
3.10
getComparatorback to summary
public Comparator<T> getComparator()

Gets the comparator being used to determine if objects are within the range.

Natural ordering uses an internal comparator implementation, thus this method never returns null. See isNaturalOrdering().

Returns:Comparator<T>

the comparator being used, not null.

getMaximumback to summary
public T getMaximum()

Gets the maximum value in this range.

Returns:T

the maximum value in this range, not null.

getMinimumback to summary
public T getMinimum()

Gets the minimum value in this range.

Returns:T

the minimum value in this range, not null.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Gets a suitable hash code for the range.

Returns:int

a hash code value for this object.

Annotations
@Override
intersectionWithback to summary
public Range<T> intersectionWith(final Range<T> other)

Calculate the intersection of this and an overlapping Range.

Parameters
other:Range<T>

overlapping Range.

Returns:Range<T>

range representing the intersection of this and other (this if equal).

Exceptions
IllegalArgumentException:
if other does not overlap this.
Since
3.0.1
isback to summary
public static <T extends Comparable<? super T>> Range<T> is(final T element)

Creates a range using the specified element as both the minimum and maximum in this range.

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

Parameters
<T>
the type of the elements in this range.
element:T

the value to use for this range, not null.

Returns:Range<T>

the range object, not null.

Exceptions
NullPointerException:
if the element is null.
ClassCastException:
if the element is not Comparable.
isback to summary
public static <T> Range<T> is(final T element, final Comparator<T> comparator)

Creates a range using the specified element as both the minimum and maximum in this range.

The range uses the specified Comparator to determine where values lie in the range.

Parameters
<T>
the type of the elements in this range.
element:T

the value to use for this range, must not be null.

comparator:Comparator<T>

the comparator to be used, null for natural ordering.

Returns:Range<T>

the range object, not null.

Exceptions
NullPointerException:
if the element is null.
ClassCastException:
if using natural ordering and the elements are not Comparable.
isAfterback to summary
public boolean isAfter(final T element)

Checks whether this range is after the specified element.

Parameters
element:T

the element to check for, null returns false.

Returns:boolean

true if this range is entirely after the specified element.

isAfterRangeback to summary
public boolean isAfterRange(final Range<T> otherRange)

Checks whether this range is completely after the specified range.

This method may fail if the ranges have two different comparators or element types.

Parameters
otherRange:Range<T>

the range to check, null returns false.

Returns:boolean

true if this range is completely after the specified range.

Exceptions
RuntimeException:
if ranges cannot be compared.
isBeforeback to summary
public boolean isBefore(final T element)

Checks whether this range is before the specified element.

Parameters
element:T

the element to check for, null returns false.

Returns:boolean

true if this range is entirely before the specified element.

isBeforeRangeback to summary
public boolean isBeforeRange(final Range<T> otherRange)

Checks whether this range is completely before the specified range.

This method may fail if the ranges have two different comparators or element types.

Parameters
otherRange:Range<T>

the range to check, null returns false.

Returns:boolean

true if this range is completely before the specified range.

Exceptions
RuntimeException:
if ranges cannot be compared.
isEndedByback to summary
public boolean isEndedBy(final T element)

Checks whether this range ends with the specified element.

Parameters
element:T

the element to check for, null returns false.

Returns:boolean

true if the specified element occurs within this range.

isNaturalOrderingback to summary
public boolean isNaturalOrdering()

Tests whether or not the Range is using the natural ordering of the elements.

Natural ordering uses an internal comparator implementation, thus this method is the only way to check if a null comparator was specified.

Returns:boolean

true if using natural ordering.

isOverlappedByback to summary
public boolean isOverlappedBy(final Range<T> otherRange)

Tests whether this range is overlapped by the specified range.

Two ranges overlap if there is at least one element in common.

This method may fail if the ranges have two different comparators or element types.

Parameters
otherRange:Range<T>

the range to test, null returns false.

Returns:boolean

true if the specified range overlaps with this range; otherwise, false.

Exceptions
RuntimeException:
if ranges cannot be compared.
isStartedByback to summary
public boolean isStartedBy(final T element)

Tests whether this range starts with the specified element.

Parameters
element:T

the element to check for, null returns false.

Returns:boolean

true if the specified element occurs within this range.

ofback to summary
public static <T extends Comparable<? super T>> Range<T> of(final T fromInclusive, final T toInclusive)

Creates a 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
<T>
the type of the elements in this range.
fromInclusive:T

the first value that defines the edge of the range, inclusive.

toInclusive:T

the second value that defines the edge of the range, inclusive.

Returns:Range<T>

the range object, not null.

Exceptions
NullPointerException:
if either element is null.
ClassCastException:
if the elements are not Comparable.
Since
3.13.0
ofback to summary
public static <T> Range<T> of(final T fromInclusive, final T toInclusive, final Comparator<T> comparator)

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

The range uses the specified Comparator 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
<T>
the type of the elements in this range.
fromInclusive:T

the first value that defines the edge of the range, inclusive.

toInclusive:T

the second value that defines the edge of the range, inclusive.

comparator:Comparator<T>

the comparator to be used, null for natural ordering.

Returns:Range<T>

the range object, not null.

Exceptions
NullPointerException:
  • when fromInclusive is null.
  • when toInclusive is null.
ClassCastException:
if using natural ordering and the elements are not Comparable.
Since
3.13.0
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Gets the range as a String.

The format of the String is '[min..max]'.

Returns:String

the String representation of this range.

Annotations
@Override
toStringback to summary
public String toString(final String format)

Formats the receiver using the given format.

This uses java.util.Formattable to perform the formatting. Three variables may be used to embed the minimum, maximum and comparator. Use %1$s for the minimum element, %2$s for the maximum element and %3$s for the comparator. The default format used by toString() is [%1$s..%2$s].

Parameters
format:String

the format string, optionally containing %1$s, %2$s and %3$s, not null.

Returns:String

the formatted string, not null.

org.apache.commons.lang3 back to summary

private final Enum Range.ComparableComparator

extends Enum<Range.ComparableComparator>
implements Comparator
Class Inheritance
All Implemented Interfaces
java.util.Comparator
Annotations
@SuppressWarnings:rawtypes, unchecked

Field Summary

Modifier and TypeField and Description
public static final Range.ComparableComparator

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

negative, 0, positive comparison value.
compare
(final Object
left-hand side side of comparison.
obj1
,
final Object
right-hand side side of comparison.
obj2
)

Implements java.util.Comparator.compare.

Comparable based compare implementation.

public static Range.ComparableComparator
public static Range.ComparableComparator[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

INSTANCEback to summary
public static final Range.ComparableComparator INSTANCE

Constructor Detail

ComparableComparatorback to summary
private ComparableComparator()

Method Detail

compareback to summary
public int compare(final Object obj1, final Object obj2)

Implements java.util.Comparator.compare.

Comparable based compare implementation.

Parameters
obj1:Object

left-hand side side of comparison.

obj2:Object

right-hand side side of comparison.

Returns:int

negative, 0, positive comparison value.

Annotations
@Override
valueOfback to summary
public static Range.ComparableComparator valueOf(String name)
valuesback to summary
public static Range.ComparableComparator[] values()