-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved some utils related to limit and offset with guava Ranges from j…
…enax' QueryUtils to RangeUtils.
- Loading branch information
Showing
3 changed files
with
93 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...utils-parent/aksw-commons-utils/src/main/java/org/aksw/commons/util/range/LongRanges.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.aksw.commons.util.range; | ||
|
||
import com.google.common.collect.BoundType; | ||
import com.google.common.collect.DiscreteDomain; | ||
import com.google.common.collect.Range; | ||
|
||
public class LongRanges { | ||
/** | ||
* Transform a range w.r.t. a discrete domain such that any lower bound is closed and the upper bound | ||
* is open. As a result, a zero-length range is represented by [x..x) | ||
* | ||
* @param <T> | ||
* @param range | ||
* @param domain | ||
* @return | ||
*/ | ||
// This seems to be the same as range.canonical(discreteDomain) | ||
// public static <T extends Comparable<T>> Range<T> makeClosedOpen(Range<T> range, DiscreteDomain<T> domain) { | ||
// ContiguousSet<T> set = ContiguousSet.create(range, domain); | ||
// range.canonical(domain) | ||
// Range<T> result = set.isEmpty() | ||
// ? Range.closedOpen(range.lowerEndpoint(), range.lowerEndpoint()) | ||
// : ContiguousSet.create(range, domain).range(BoundType.CLOSED, BoundType.OPEN); | ||
// return result; | ||
// } | ||
|
||
public static <T extends Comparable<T>> T closedLowerEndpointOrNull(Range<T> range, DiscreteDomain<T> domain) { | ||
T result = !range.hasLowerBound() | ||
? null | ||
: range.lowerBoundType().equals(BoundType.CLOSED) | ||
? range.lowerEndpoint() | ||
: domain.next(range.lowerEndpoint()); | ||
|
||
return result; | ||
} | ||
|
||
public static <T extends Comparable<T>> T openUpperEndpointOrNull(Range<T> range, DiscreteDomain<T> domain) { | ||
T result = !range.hasUpperBound() | ||
? null | ||
: range.upperBoundType().equals(BoundType.CLOSED) | ||
? domain.next(range.upperEndpoint()) | ||
: range.upperEndpoint(); | ||
|
||
return result; | ||
} | ||
|
||
public static Long rangeToOffset(Range<Long> range) { | ||
Long tmp = range == null | ||
? null | ||
: closedLowerEndpointOrNull(range, DiscreteDomain.longs()); | ||
|
||
Long result = tmp == null || tmp == 0 ? null : tmp; | ||
return result; | ||
} | ||
|
||
/** | ||
* | ||
* @param range | ||
* @return | ||
*/ | ||
public static Long rangeToLimit(Range<Long> range) { | ||
// range = range == null ? null : makeClosedOpen(range, DiscreteDomain.longs()); | ||
range = range == null ? null : range.canonical(DiscreteDomain.longs()); | ||
|
||
Long result = range == null || !range.hasUpperBound() | ||
? null | ||
: DiscreteDomain.longs().distance(range.lowerEndpoint(), range.upperEndpoint()) | ||
// If the upper bound is closed such as [x, x] then the result is the distance plus 1 | ||
+ (range.upperBoundType().equals(BoundType.CLOSED) ? 1 : 0); | ||
|
||
return result; | ||
} | ||
} |