public class RangeUtils extends Object
Ranges.| Modifier and Type | Field and Description |
|---|---|
static Comparator<com.google.common.collect.Range> |
LOWER_ENDPOINT_COMPARATOR
An
EndpointTypeComparator for comparing the lower end points of a range. |
static Comparator<com.google.common.collect.Range> |
UPPER_ENDPOINT_COMPARATOR
An
EndpointTypeComparator for comparing the upper end points of a range. |
| Modifier and Type | Method and Description |
|---|---|
static <T,C extends Comparable> |
compareByEndpoint(T lhs,
EndpointAccessor<T,C> lhsEndpoint,
T rhs,
EndpointAccessor<T,C> rhsEndpoint)
Compares to endpoints of a range.
|
static <T,C extends Comparable> |
compareByEndpoint(T lhs,
EndpointAccessor<T,C> lhsEndpoint,
T rhs,
EndpointAccessor<T,C> rhsEndpoint,
EndpointTypeComparator comparator)
Compares to endpoints of a range.
|
static <C extends Comparable> |
createClosedRange(C lower,
C upper)
Creates an open range from two end points.
|
static <C extends Comparable> |
createOpenRange(C lower,
C upper)
Creates a closed range from two end points.
|
static <C extends Comparable> |
createRange(C lower,
EndpointDescriptor lowerDesc,
C upper,
EndpointDescriptor upperDesc)
Creates a range from two end points.
|
static <T,C extends Comparable> |
createRange(T lhs,
EndpointAccessor<T,C> lhsEndpoint,
T rhs,
EndpointAccessor<T,C> rhsEndpoint)
Creates a range from two end points.
|
static <T extends Comparable> |
eachPointInRange(com.google.common.collect.Range<T> range,
UnaryOperator<T> nextItem)
Splits the given range into a sequence of points, starting at the lower endpoint of the range.
|
static <T extends Comparable> |
eachPointInRange(com.google.common.collect.Range<T> range,
UnaryOperator<T> nextItem,
boolean includeTail)
Splits the given range into a sequence of points, starting at the lower endpoint of the range.
|
static <C extends Comparable> |
eachSubrange(com.google.common.collect.Range<C> range,
com.google.common.collect.BoundType boundType,
boolean includeTail,
UnaryOperator<C> nextItem)
Splits the given range into a sequence of connected ranges, starting at the lower endpoint of the interval.
|
static <C extends Comparable> |
eachSubrange(com.google.common.collect.Range<C> range,
com.google.common.collect.BoundType boundType,
UnaryOperator<C> nextItem)
Splits the given range into a sequence of touching ranges, starting at the lower endpoint of the interval.
|
static <T,C extends Comparable> |
findOverlappingDepth(List<T> items,
Function<T,com.google.common.collect.Range<C>> rangeAccessor,
EndpointTypeComparator comparator)
Finds the overlapping depth of each given range.
|
static <C extends Comparable> |
hasNonZeroOverlap(com.google.common.collect.Range<C> lhs,
com.google.common.collect.Range<C> rhs)
Checks whether the two given ranges have any overlap that has a length greater than zero, e.g. contains more than a
single point.
|
static <C extends Comparable> |
hasNonZeroOverlap(com.google.common.collect.RangeSet<C> ranges,
com.google.common.collect.Range<C> range)
Checks whether a given range has any overlap (with one range of a set of ranges) that has a length greater than
zero, e.g. contains more than a single point.
|
static <C extends Comparable> |
hasOverlap(com.google.common.collect.Range<C> lhs,
com.google.common.collect.Range<C> rhs)
Checks whether the two given ranges have any overlap that is not empty.
|
static <C extends Comparable> |
hasZeroLength(com.google.common.collect.Range<C> range)
Checks if the length of a range is zero.
|
static <C extends Comparable> |
intersect(com.google.common.collect.Range<C> lhs,
com.google.common.collect.Range<C> rhs)
Returns a range representing the intersection of the two given ranges.
|
static com.google.common.collect.BoundType |
invertBoundType(com.google.common.collect.BoundType boundType) |
static <C extends Comparable> |
lowerEndpointComparator() |
static <C extends Comparable,T extends Comparable> |
mapRange(com.google.common.collect.Range<C> range,
Function<C,T> mapper)
Returns a new range with the points mapped by the given mapping function.
|
static <C extends Comparable,T extends Comparable> |
mapRange(com.google.common.collect.Range<C> range,
Function<C,T> mapper,
EndpointAccessor<com.google.common.collect.Range<C>,C> lowerAccessor,
EndpointAccessor<com.google.common.collect.Range<C>,C> upperAccessor)
Returns a new range with the points mapped by the given mapping function.
|
static <C extends Comparable,T extends Comparable> |
mapRange(com.google.common.collect.Range<C> range,
Function<C,T> lowerMapper,
EndpointAccessor<com.google.common.collect.Range<C>,C> lowerAccessor,
Function<C,T> upperMapper,
EndpointAccessor<com.google.common.collect.Range<C>,C> upperAccessor)
Returns a new range with the points mapped by the given mapping function.
|
static <C extends Comparable,T extends Comparable> |
mapRange(com.google.common.collect.Range<C> range,
Function<C,T> lowerMapper,
Function<C,T> upperMapper)
Returns a new range with the points mapped by the given mapping function.
|
static <T,C extends Comparable> |
merge(List<T> items,
Function<T,com.google.common.collect.Range<C>> rangeAccessor,
BinaryOperator<T> merger,
boolean mergeAdjacent)
Merges all items whose ranges overlap, and returns a list of merged items.
|
static <C extends Comparable> |
toClosed(com.google.common.collect.Range<C> range)
Converts the range to a closed-closed range.
|
static <C extends Comparable> |
toClosedOpen(com.google.common.collect.Range<C> range)
Converts the range to a closed-open range.
|
static <C extends Comparable> |
toOpen(com.google.common.collect.Range<C> range)
Converts the range to an open-open range.
|
static <C extends Comparable> |
toOpenClosed(com.google.common.collect.Range<C> range)
Converts the range to a open -closed range.
|
static <C extends Comparable<C>> |
toRangeSet()
Creates a collector for collecting a sequence of ranges into a
RangeSet. |
static <C extends Comparable> |
upperEndpointComparator() |
public static Comparator<com.google.common.collect.Range> LOWER_ENDPOINT_COMPARATOR
EndpointTypeComparator for comparing the lower end points of a range.public static Comparator<com.google.common.collect.Range> UPPER_ENDPOINT_COMPARATOR
EndpointTypeComparator for comparing the upper end points of a range.public static <T,C extends Comparable> int compareByEndpoint(T lhs, EndpointAccessor<T,C> lhsEndpoint, T rhs, EndpointAccessor<T,C> rhsEndpoint)
T - Type of the item that contains a range.C - Type of the range's end points.lhs - First item with a range to compare.lhsEndpoint - Object for accessing a range endpoint the first item.rhs - Second item with a range to compare.rhsEndpoint - Object for accessing a range endpoint of the second item.0 if they
are equal, and 1 otherwise.public static <T,C extends Comparable> int compareByEndpoint(T lhs, EndpointAccessor<T,C> lhsEndpoint, T rhs, EndpointAccessor<T,C> rhsEndpoint, EndpointTypeComparator comparator)
T - Type of the item that contains a range.C - Type of the range's end points.lhs - First item with a range to compare.lhsEndpoint - Object for accessing a range endpoint the first item.rhs - Second item with a range to compare.rhsEndpoint - Object for accessing a range endpoint of the second item.comparator - Comparator for comparing end point types when they lie at the same point.0 if they
are equal, and 1 otherwise.public static <C extends Comparable> com.google.common.collect.Range<C> createClosedRange(C lower, C upper)
C - Type of the range's end points.lower - Lower end point.upper - Upper end point.(lower, upper). When upper end points is before lower end point, an empty range is
returned. If either end point is null, it is considered unbounded.public static <C extends Comparable> com.google.common.collect.Range<C> createOpenRange(C lower, C upper)
C - Type of the range's end points.lower - Lower end point.upper - Upper end point.[lower, upper]. When upper end points is before lower end point, an empty range is
returned. If either end point is null, it is considered unbounded.public static <C extends Comparable> com.google.common.collect.Range<C> createRange(C lower, EndpointDescriptor lowerDesc, C upper, EndpointDescriptor upperDesc)
C - Type of the range's end points.lower - Lower end point.lowerDesc - Type of the lower end point.upper - Upper end point.upperDesc - Type of the upper end point.public static <T,C extends Comparable> com.google.common.collect.Range<C> createRange(T lhs, EndpointAccessor<T,C> lhsEndpoint, T rhs, EndpointAccessor<T,C> rhsEndpoint)
T - Type of the objects with the end points.C - Type of the range's end points.lhs - Object for the lower end point.lhsEndpoint - Object for extracting the lower end point from the object.rhs - Object for the upper end point.rhsEndpoint - Object for extracting the upper end point from the object.public static <T extends Comparable> Iterable<T> eachPointInRange(com.google.common.collect.Range<T> range, UnaryOperator<T> nextItem)
1,2,3,4,5,6,7,8,9,10:
final Range<Integer> oneToTen = Range.closed(1, 10);S
for (final int i : eachPointInRange(oneToTen, x -> x + 1)) {
System.out.println(x);
}
T - Type of the items in the range.range - A range to split into points.nextItem - A successor function that returns the next point. [0,2,5].public static <T extends Comparable> Iterable<T> eachPointInRange(com.google.common.collect.Range<T> range, UnaryOperator<T> nextItem, boolean includeTail)
1,2,3,4,5,6,7,8,9,10:
final Range<Integer> oneToTen = Range.closed(1, 10);
for (final int i : eachPointInRange(oneToTen, x -> x + 1)) {
System.out.println(x);
}
T - Type of the items in the range.range - A range to split into points.nextItem - A successor function that returns the next point.includeTail - If true, includes the last point of the range when the next point falls outside the
interval. For example, when called with a range [0,5] and a next function x->x+2, this would return
[0,2,5].public static <C extends Comparable> Iterable<com.google.common.collect.Range<C>> eachSubrange(com.google.common.collect.Range<C> range, com.google.common.collect.BoundType boundType, boolean includeTail, UnaryOperator<C> nextItem)
[0,2], [2,4], [4,5]:
final Range<Integer> range = Range.closed(0, 5);
for (final Range<Integer> r : eachSubrange(range, BoundType.CLOSED, true, x -> x + 2)) {
System.out.println(r);
}
C - Type of the items in the range.range - A range to split.boundType - Whether the sub ranges should be open or closed.includeTail - If true, returns the last range when the next cut point falls outside the interval. E.g.
[0,2], [2,4], [4,5] when called with the range [0,5] and the next function x -> x + 1nextItem - A successor function that returns the next point.public static <C extends Comparable> Iterable<com.google.common.collect.Range<C>> eachSubrange(com.google.common.collect.Range<C> range, com.google.common.collect.BoundType boundType, UnaryOperator<C> nextItem)
[0,2], [2,4]:
final Range<Integer> range = Range.closed(0, 5);
for (final Range<Integer> r : eachSubrange(range, BoundType.CLOSED, x -> x + 2)) {
System.out.println(r);
}
C - Type of the items in the range.range - A range to split.boundType - Whether the sub ranges should be open or closed.nextItem - A successor function that returns the next point.public static <T,C extends Comparable> List<org.apache.commons.lang3.tuple.Pair<Integer,T>> findOverlappingDepth(List<T> items, Function<T,com.google.common.collect.Range<C>> rangeAccessor, EndpointTypeComparator comparator)
1 means that a range does not overlap any other
range, a depth of >1 means that that many ranges are overlapping.T - Type of the items holding a range.C - Type of the end points of the ranges.items - Range items to process.rangeAccessor - Function for accessing the range represent by an item.comparator - How to sort end point if they are on the same point.public static <C extends Comparable> boolean hasNonZeroOverlap(com.google.common.collect.Range<C> lhs, com.google.common.collect.Range<C> rhs)
[1,2] and [2,3] is [2,2], which has a length of
0 and this method would return false. Conversely, [1,1.999] and [2,3] have a
non-zero overlap, so this method would return true. This assumes a continuous domain.
Please note that this assumes a continuous domain for the type T.
C - Type of the items in the range.lhs - First range to check.rhs - Second range to check.true if there exists a non-empty range with a positive measure that is enclosed by both this range
and other.public static <C extends Comparable> boolean hasNonZeroOverlap(com.google.common.collect.RangeSet<C> ranges, com.google.common.collect.Range<C> range)
[1,2] and [2,3] is
[2,2], which has a length of 0 and this method would return false. Conversely,
[1,1.999] and [2,3] have a non-zero overlap, so this method would return true. This assumes
a continuous domain.
Please note that this assumes a continuous domain for the type T.
C - Type of the items in the range.ranges - Ranges to check.range - Range to check.true if there exists a non-empty range with a positive measure that is enclosed by both a range of
the given range set; and the other range.public static <C extends Comparable> boolean hasOverlap(com.google.common.collect.Range<C> lhs, com.google.common.collect.Range<C> rhs)
C - Type of the items in the range.lhs - First range to check.rhs - Second range to check.true if there exists a non-empty range which is enclosed by both this range and other.public static <C extends Comparable> boolean hasZeroLength(com.google.common.collect.Range<C> range)
Range.isEmpty() in that a range with a single
point (e.g. [1,1]) has zero length, but is not considered empty. This assumes a continuous domain.C - Type of the end points of the range.range - A range to check.true if the range has zero length, or false otherwise.public static <C extends Comparable> Optional<com.google.common.collect.Range<C>> intersect(com.google.common.collect.Range<C> lhs, com.google.common.collect.Range<C> rhs)
C - Type of the end points of the ranges.lhs - First range to process.rhs - Second range to process.public static com.google.common.collect.BoundType invertBoundType(com.google.common.collect.BoundType boundType)
boundType - A bound type to process.BoundType.OPEN when BoundType.CLOSED is given and
vice versa.public static <C extends Comparable> Comparator<com.google.common.collect.Range<C>> lowerEndpointComparator()
C - Type of the end points of the Range.upperEndpointComparator()public static <C extends Comparable,T extends Comparable> com.google.common.collect.Range<T> mapRange(com.google.common.collect.Range<C> range, Function<C,T> mapper)
C - Type of the items in the given range.T - Type of the items in the mapped range.range - Range to map.mapper - Function that maps from the items in the input range to the items of the target range.public static <C extends Comparable,T extends Comparable> com.google.common.collect.Range<T> mapRange(com.google.common.collect.Range<C> range, Function<C,T> mapper, EndpointAccessor<com.google.common.collect.Range<C>,C> lowerAccessor, EndpointAccessor<com.google.common.collect.Range<C>,C> upperAccessor)
C - Type of the items in the given range.T - Type of the items in the mapped range.range - Range to map.mapper - Function that maps from the lower and upper end point of the input range to the lower and upper end
point of the target range.lowerAccessor - Accessor for the lower end point.upperAccessor - Accessor for the upper end point.public static <C extends Comparable,T extends Comparable> com.google.common.collect.Range<T> mapRange(com.google.common.collect.Range<C> range, Function<C,T> lowerMapper, EndpointAccessor<com.google.common.collect.Range<C>,C> lowerAccessor, Function<C,T> upperMapper, EndpointAccessor<com.google.common.collect.Range<C>,C> upperAccessor)
C - Type of the items in the given range.T - Type of the items in the mapped range.range - Range to map.lowerMapper - Function that maps from the lower end point of the input range to the lower end point of the
target range.lowerAccessor - Accessor for the lower end point.upperMapper - Function that maps from the upper end point of the input range to the upper end point of the
target range.upperAccessor - Accessor for the upper end point.public static <C extends Comparable,T extends Comparable> com.google.common.collect.Range<T> mapRange(com.google.common.collect.Range<C> range, Function<C,T> lowerMapper, Function<C,T> upperMapper)
C - Type of the items in the given range.T - Type of the items in the mapped range.range - Range to map.lowerMapper - Function that maps from the lower end point of the input range to the lower end point of the
target range.upperMapper - Function that maps from the upper end point of the input range to the upper end point of the
target range.public static <T,C extends Comparable> List<T> merge(List<T> items, Function<T,com.google.common.collect.Range<C>> rangeAccessor, BinaryOperator<T> merger, boolean mergeAdjacent)
T - Type of the items.C - Type of the end point of the ranges of the items.items - Items to process.rangeAccessor - Accessor for accessing the range of an item.merger - Merge that merges two overlapping items.mergeAdjacent - If true, adjacent ranges are merged as well (overlapping in a single point).public static <C extends Comparable> com.google.common.collect.Range<C> toClosed(com.google.common.collect.Range<C> range)
C - Type of the end point of the range.range - A range to convert.public static <C extends Comparable> com.google.common.collect.Range<C> toClosedOpen(com.google.common.collect.Range<C> range)
C - Type of the end point of the range.range - A range to convert.public static <C extends Comparable> com.google.common.collect.Range<C> toOpen(com.google.common.collect.Range<C> range)
C - Type of the end point of the range.range - A range to convert.public static <C extends Comparable> com.google.common.collect.Range<C> toOpenClosed(com.google.common.collect.Range<C> range)
C - Type of the end point of the range.range - A range to convert.public static <C extends Comparable<C>> Collector<com.google.common.collect.Range<C>,?,com.google.common.collect.RangeSet<C>> toRangeSet()
RangeSet.C - Type of the range's end points.RangeSet.public static <C extends Comparable> Comparator<com.google.common.collect.Range<C>> upperEndpointComparator()
C - Type of the end points of the Range.lowerEndpointComparator()Copyright © 2021 XIMA MEDIA GmbH. All rights reserved.