Range

public class org.apache.commons.lang3.Range<T> implements Serializable[source]

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.

Parameters:

<T> – The type of range values.

Since:

3.0

Methods

between(Comparable, Comparable)

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

between(Object, Object, Comparator)

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

contains(Object)

Checks whether the specified element occurs within this range.

containsRange(Range)

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

elementCompareTo(Object)

Checks where the specified element occurs relative to this range.

equals(Object)

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

fit(Object)

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.

getComparator()

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

getMaximum()

Gets the maximum value in this range.

getMinimum()

Gets the minimum value in this range.

hashCode()

Gets a suitable hash code for the range.

intersectionWith(Range)

Calculate the intersection of this and an overlapping Range.

is(Comparable)

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

is(Object, Comparator)

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

isAfter(Object)

Tests whether this range is after the specified element.

isAfterRange(Range)

Tests whether this range is completely after the specified range.

isBefore(Object)

Tests whether this range is before the specified element.

isBeforeRange(Range)

Tests whether this range is completely before the specified range.

isEndedBy(Object)

Tests whether this range ends with the specified element.

isNaturalOrdering()

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

isOverlappedBy(Range)

Tests whether this range is overlapped by the specified range.

isStartedBy(Object)

Tests whether this range starts with the specified element.

of(Comparable, Comparable)

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

of(Object, Object, Comparator)

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

toString()

Gets the range as a String.

toString(String)

Formats the receiver using the given format.

@Deprecated public static <T extends Comparable<? super T>> Range<T> between(T fromInclusive, T toInclusive)[source]

Deprecated: Use #of(Comparable, Comparable).

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.

Type parameters:

<T> – The type of the elements in this range.

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.NullPointerException – Thrown when fromInclusive is null.

    Thrown when toInclusive is null.

  • java.lang.ClassCastException – Thrown if the elements are not Comparable.

  • java.lang.IllegalArgumentException – Thrown if either element is a floating-point NaN.

@Deprecated public static <T> Range<T> between(T fromInclusive, T toInclusive, Comparator<T> comparator)[source]

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.

Type parameters:

<T> – The type of the elements in this range.

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.

  • comparator – The comparator to be used, null for natural ordering.

Returns:

The range object, not null.

Throws:
  • java.lang.NullPointerException – Thrown when fromInclusive is null.

    Thrown when toInclusive is null.

  • java.lang.ClassCastException – Thrown if using natural ordering and the elements are not Comparable.

  • java.lang.IllegalArgumentException – Thrown if either element is a floating-point NaN.

public boolean contains(T element)[source]

Checks whether the specified element occurs within this range.

Parameters:

element – The element to check for, null returns false.

Returns:

true if the specified element occurs within this range.

public boolean containsRange(Range<T> otherRange)[source]

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 – The range to check, null returns false.

Returns:

true if this range contains the specified range.

Throws:

java.lang.RuntimeException – Thrown if ranges cannot be compared.

public int elementCompareTo(T element)[source]

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 – The element to check for, not null.

Returns:

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

Throws:

java.lang.NullPointerException – Thrown if element is null.

@Override public boolean equals(Object obj)[source]

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 – The reference object with which to compare.

Returns:

true if this object is equal.

public T fit(T element)[source]

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 – The element to check for, not null.

Returns:

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

Throws:

java.lang.NullPointerException – Thrown if element is null.

Since:

3.10

public Comparator<T> getComparator()[source]

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:

The comparator being used, not null.

public T getMaximum()[source]

Gets the maximum value in this range.

Returns:

The maximum value in this range, not null.

public T getMinimum()[source]

Gets the minimum value in this range.

Returns:

The minimum value in this range, not null.

@Override public int hashCode()[source]

Gets a suitable hash code for the range.

Returns:

A hash code value for this object.

public Range<T> intersectionWith(Range<T> other)[source]

Calculate the intersection of this and an overlapping Range.

Parameters:

other – overlapping Range.

Returns:

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

Throws:

java.lang.IllegalArgumentException – Thrown if other does not overlap this.

Since:

3.0.1

public static <T extends Comparable<? super T>> Range<T> is(T element)[source]

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.

Type parameters:

<T> – The type of the elements in this range.

Parameters:

element – The value to use for this range, not null.

Returns:

The range object, not null.

Throws:
  • java.lang.NullPointerException – Thrown if the element is null.

  • java.lang.ClassCastException – Thrown if the element is not Comparable.

  • java.lang.IllegalArgumentException – Thrown if the element is a floating-point NaN.

public static <T> Range<T> is(T element, Comparator<T> comparator)[source]

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.

Type parameters:

<T> – The type of the elements in this range.

Parameters:
  • element – The value to use for this range, must not be null.

  • comparator – The comparator to be used, null for natural ordering.

Returns:

The range object, not null.

Throws:
  • java.lang.NullPointerException – Thrown if the element is null.

  • java.lang.ClassCastException – Thrown if using natural ordering and the elements are not Comparable.

  • java.lang.IllegalArgumentException – Thrown if the element is a floating-point NaN.

public boolean isAfter(T element)[source]

Tests whether this range is after the specified element.

Parameters:

element – The element to check for, null returns false.

Returns:

true if this range is entirely after the specified element.

public boolean isAfterRange(Range<T> otherRange)[source]

Tests 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 – The range to check, null returns false.

Returns:

true if this range is completely after the specified range.

Throws:

java.lang.RuntimeException – Thrown if ranges cannot be compared.

public boolean isBefore(T element)[source]

Tests whether this range is before the specified element.

Parameters:

element – The element to check for, null returns false.

Returns:

true if this range is entirely before the specified element.

public boolean isBeforeRange(Range<T> otherRange)[source]

Tests 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 – The range to check, null returns false.

Returns:

true if this range is completely before the specified range.

Throws:

java.lang.RuntimeException – Thrown if ranges cannot be compared.

public boolean isEndedBy(T element)[source]

Tests whether this range ends with the specified element.

Parameters:

element – The element to check for, null returns false.

Returns:

true if the specified element occurs within this range.

public boolean isNaturalOrdering()[source]

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:

true if using natural ordering.

public boolean isOverlappedBy(Range<T> otherRange)[source]

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 – The range to test, null returns false.

Returns:

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

Throws:

java.lang.RuntimeException – Thrown if ranges cannot be compared.

public boolean isStartedBy(T element)[source]

Tests whether this range starts with the specified element.

Parameters:

element – The element to check for, null returns false.

Returns:

true if the specified element occurs within this range.

public static <T extends Comparable<? super T>> Range<T> of(T fromInclusive, T toInclusive)[source]

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.

Type parameters:

<T> – The type of the elements in this range.

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.NullPointerException – Thrown if either element is null.

  • java.lang.ClassCastException – Thrown if the elements are not Comparable.

  • java.lang.IllegalArgumentException – Thrown if either element is a floating-point NaN.

Since:

3.13.0

public static <T> Range<T> of(T fromInclusive, T toInclusive, Comparator<T> comparator)[source]

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.

Type parameters:

<T> – The type of the elements in this range.

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.

  • comparator – The comparator to be used, null for natural ordering.

Returns:

The range object, not null.

Throws:
  • java.lang.NullPointerException – Thrown when fromInclusive is null.

    Thrown when toInclusive is null.

  • java.lang.ClassCastException – Thrown if using natural ordering and the elements are not Comparable.

  • java.lang.IllegalArgumentException – Thrown if either element is a floating-point NaN.

Since:

3.13.0

@Override public String toString()[source]

Gets the range as a String.

The format of the String is ‘[min..max]’.

Returns:

The String representation of this range.

public String toString(String format)[source]

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 – The format string, optionally containing %1$s, %2$s and %3$s, not null.

Returns:

The formatted string, not null.