下面列出了com.google.common.collect.Range#openClosed ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Range<Calendar> ceilRange(TimeUnitRange timeUnit, SqlKind comparison, Calendar c) {
final Calendar ceil = ceil(c, timeUnit);
boolean boundary = ceil.equals(c);
switch (comparison) {
case EQUALS:
return Range.openClosed(boundary ? decrement(ceil, timeUnit) : ceil, ceil);
case LESS_THAN:
return Range.atMost(decrement(ceil, timeUnit));
case LESS_THAN_OR_EQUAL:
return boundary ? Range.atMost(ceil) : Range.atMost(decrement(ceil, timeUnit));
case GREATER_THAN:
return boundary ? Range.greaterThan(ceil) : Range.greaterThan(decrement(ceil, timeUnit));
case GREATER_THAN_OR_EQUAL:
return Range.greaterThan(decrement(ceil, timeUnit));
default:
throw Util.unexpected(comparison);
}
}
private Range<Calendar> ceilRange(TimeUnitRange timeUnit, SqlKind comparison,
Calendar c) {
final Calendar ceil = ceil(c, timeUnit);
boolean boundary = ceil.equals(c);
switch (comparison) {
case EQUALS:
return Range.openClosed(boundary ? decrement(ceil, timeUnit) : ceil, ceil);
case LESS_THAN:
return Range.atMost(decrement(ceil, timeUnit));
case LESS_THAN_OR_EQUAL:
return boundary ? Range.atMost(ceil) : Range.atMost(decrement(ceil, timeUnit));
case GREATER_THAN:
return boundary ? Range.greaterThan(ceil) : Range.greaterThan(decrement(ceil, timeUnit));
case GREATER_THAN_OR_EQUAL:
return Range.greaterThan(decrement(ceil, timeUnit));
default:
throw Util.unexpected(comparison);
}
}
@Test
public void testFirstRange() {
ConcurrentOpenLongPairRangeSet<LongPair> set = new ConcurrentOpenLongPairRangeSet<>(consumer);
assertNull(set.firstRange());
Range<LongPair> range = Range.openClosed(new LongPair(0, 97), new LongPair(0, 99));
set.add(range);
assertEquals(set.firstRange(), range);
assertEquals(set.size(), 1);
range = Range.openClosed(new LongPair(0, 98), new LongPair(0, 105));
set.add(range);
assertEquals(set.firstRange(), Range.openClosed(new LongPair(0, 97), new LongPair(0, 105)));
assertEquals(set.size(), 1);
range = Range.openClosed(new LongPair(0, 5), new LongPair(0, 75));
set.add(range);
assertEquals(set.firstRange(), range);
assertEquals(set.size(), 2);
}
@Test
public void testLastRange() {
ConcurrentOpenLongPairRangeSet<LongPair> set = new ConcurrentOpenLongPairRangeSet<>(consumer);
assertNull(set.lastRange());
Range<LongPair> range = Range.openClosed(new LongPair(0, 97), new LongPair(0, 99));
set.add(range);
assertEquals(set.lastRange(), range);
assertEquals(set.size(), 1);
range = Range.openClosed(new LongPair(0, 98), new LongPair(0, 105));
set.add(range);
assertEquals(set.lastRange(), Range.openClosed(new LongPair(0, 97), new LongPair(0, 105)));
assertEquals(set.size(), 1);
range = Range.openClosed(new LongPair(1, 5), new LongPair(1, 75));
set.add(range);
assertEquals(set.lastRange(), range);
assertEquals(set.size(), 2);
range = Range.openClosed(new LongPair(1, 80), new LongPair(1, 120));
set.add(range);
assertEquals(set.lastRange(), range);
assertEquals(set.size(), 3);
}
private Range<Calendar> ceilRange(TimeUnitRange timeUnit, SqlKind comparison,
Calendar c) {
final Calendar ceil = ceil(c, timeUnit);
boolean boundary = ceil.equals(c);
switch (comparison) {
case EQUALS:
return Range.openClosed(boundary ? decrement(ceil, timeUnit) : ceil, ceil);
case LESS_THAN:
return Range.atMost(decrement(ceil, timeUnit));
case LESS_THAN_OR_EQUAL:
return boundary ? Range.atMost(ceil) : Range.atMost(decrement(ceil, timeUnit));
case GREATER_THAN:
return boundary ? Range.greaterThan(ceil) : Range.greaterThan(decrement(ceil, timeUnit));
case GREATER_THAN_OR_EQUAL:
return Range.greaterThan(decrement(ceil, timeUnit));
default:
throw Util.unexpected(comparison);
}
}
/**
* Returns a new {@link MetricPoint} representing a value at a specific {@link Instant}.
*
* <p>Callers should insure that the count of {@code labelValues} matches the count of labels for
* the given metric.
*/
@VisibleForTesting
public static <V> MetricPoint<V> create(
Metric<V> metric, ImmutableList<String> labelValues, Instant timestamp, V value) {
MetricsUtils.checkLabelValuesLength(metric, labelValues);
return new AutoValue_MetricPoint<>(
metric, labelValues, Range.openClosed(timestamp, timestamp), value);
}
/**
* Returns a new {@link MetricPoint} representing a value over an {@link Range} from {@code
* startTime} to {@code endTime}.
*
* <p>Callers should insure that the count of {@code labelValues} matches the count of labels for
* the given metric.
*/
@VisibleForTesting
public static <V> MetricPoint<V> create(
Metric<V> metric,
ImmutableList<String> labelValues,
Instant startTime,
Instant endTime,
V value) {
MetricsUtils.checkLabelValuesLength(metric, labelValues);
return new AutoValue_MetricPoint<>(
metric, labelValues, Range.openClosed(startTime, endTime), value);
}
private void computeLocality(ParquetMetadata footer) throws ExecutionSetupException {
try {
BlockMetaData block = footer.getBlocks().get(readEntry.getRowGroupIndex());
Iterable<FileBlockLocation> blockLocations = fs.getFileBlockLocations(Path.of(readEntry.getPath()), block.getStartingPos(), block.getCompressedSize());
String localHost = InetAddress.getLocalHost().getCanonicalHostName();
List<Range<Long>> intersectingRanges = new ArrayList<>();
Range<Long> rowGroupRange = Range.openClosed(block.getStartingPos(), block.getStartingPos() + block.getCompressedSize());
for (FileBlockLocation loc : blockLocations) {
for (String host : loc.getHosts()) {
if (host.equals(localHost)) {
intersectingRanges.add(Range.closedOpen(loc.getOffset(), loc.getOffset() + loc.getSize()).intersection(rowGroupRange));
}
}
}
long totalIntersect = 0;
for (Range<Long> range : intersectingRanges) {
totalIntersect += (range.upperEndpoint() - range.lowerEndpoint());
}
if (totalIntersect < block.getCompressedSize()) {
context.getStats().addLongStat(Metric.NUM_REMOTE_READERS, 1);
} else {
context.getStats().addLongStat(Metric.NUM_REMOTE_READERS, 0);
}
} catch (IOException e) {
throw new ExecutionSetupException(e);
}
}
@Override
public Range<T> span() {
Entry<Long, BitSet> firstSet = rangeBitSetMap.firstEntry();
Entry<Long, BitSet> lastSet = rangeBitSetMap.lastEntry();
int first = firstSet.getValue().nextSetBit(0);
int last = lastSet.getValue().previousSetBit(lastSet.getValue().size());
return Range.openClosed(consumer.apply(firstSet.getKey(), first - 1), consumer.apply(lastSet.getKey(), last));
}
@Override
public Range<T> firstRange() {
if (rangeBitSetMap.isEmpty()) {
return null;
}
Entry<Long, BitSet> firstSet = rangeBitSetMap.firstEntry();
int lower = firstSet.getValue().nextSetBit(0);
int upper = Math.max(lower, firstSet.getValue().nextClearBit(lower) - 1);
return Range.openClosed(consumer.apply(firstSet.getKey(), lower - 1), consumer.apply(firstSet.getKey(), upper));
}
@Override
public Range<T> lastRange() {
if (rangeBitSetMap.isEmpty()) {
return null;
}
Entry<Long, BitSet> lastSet = rangeBitSetMap.lastEntry();
int upper = lastSet.getValue().previousSetBit(lastSet.getValue().size());
int lower = Math.min(lastSet.getValue().previousClearBit(upper), upper);
return Range.openClosed(consumer.apply(lastSet.getKey(), lower), consumer.apply(lastSet.getKey(), upper));
}
@Test
public void testToString() {
ConcurrentOpenLongPairRangeSet<LongPair> set = new ConcurrentOpenLongPairRangeSet<>(consumer);
Range<LongPair> range = Range.openClosed(new LongPair(0, 97), new LongPair(0, 99));
set.add(range);
assertEquals(set.toString(), "[(0:97..0:99]]");
range = Range.openClosed(new LongPair(0, 98), new LongPair(0, 105));
set.add(range);
assertEquals(set.toString(), "[(0:97..0:105]]");
range = Range.openClosed(new LongPair(0, 5), new LongPair(0, 75));
set.add(range);
assertEquals(set.toString(), "[(0:5..0:75],(0:97..0:105]]");
}
private List<Range<LongPair>> getConnectedRange(Set<Range<LongPair>> gRanges) {
List<Range<LongPair>> gRangeConnected = Lists.newArrayList();
Range<LongPair> lastRange = null;
for (Range<LongPair> range : gRanges) {
if (lastRange == null) {
lastRange = range;
continue;
}
LongPair previousUpper = lastRange.upperEndpoint();
LongPair currentLower = range.lowerEndpoint();
int previousUpperValue = (int) (lastRange.upperBoundType().equals(BoundType.CLOSED)
? previousUpper.getValue()
: previousUpper.getValue() - 1);
int currentLowerValue = (int) (range.lowerBoundType().equals(BoundType.CLOSED) ? currentLower.getValue()
: currentLower.getValue() + 1);
boolean connected = (previousUpper.getKey() == currentLower.getKey())
? (previousUpperValue >= currentLowerValue)
: false;
if (connected) {
lastRange = Range.closed(lastRange.lowerEndpoint(), range.upperEndpoint());
} else {
gRangeConnected.add(lastRange);
lastRange = range;
}
}
int lowerOpenValue = (int) (lastRange.lowerBoundType().equals(BoundType.CLOSED)
? (lastRange.lowerEndpoint().getValue() - 1)
: lastRange.lowerEndpoint().getValue());
lastRange = Range.openClosed(new LongPair(lastRange.lowerEndpoint().getKey(), lowerOpenValue),
lastRange.upperEndpoint());
gRangeConnected.add(lastRange);
return gRangeConnected;
}
/**
* Constructor
*
* @param builder
*/
private RangeHeader(final Builder builder) {
this.field = builder.field;
if (builder.fromId != null && builder.toId != null) {
if (builder.fromInclusive && builder.toInclusive) {
range = Range.closed(builder.fromId, builder.toId);
} else if (builder.fromInclusive && !builder.toInclusive) {
range = Range.closedOpen(builder.fromId, builder.toId);
} else if (!builder.fromInclusive && builder.toInclusive) {
range = Range.openClosed(builder.fromId, builder.toId);
} else {
range = Range.open(builder.fromId, builder.toId);
}
} else if (builder.fromId != null && builder.toId == null) {
if (builder.fromInclusive) {
range = Range.atLeast(builder.fromId);
} else {
range = Range.greaterThan(builder.fromId);
}
} else if (builder.fromId == null && builder.toId != null) {
if (builder.toInclusive) {
range = Range.atMost(builder.toId);
} else {
range = Range.lessThan(builder.toId);
}
} else {
range = Range.all();
}
this.max = builder.max;
}
private void computeLocality(FileSystem fs, Path path, DiskRangeList range) {
if (this.remoteRead) {
return;
}
boolean currentReadIsRemote = false;
try {
String localHost = InetAddress.getLocalHost().getCanonicalHostName();
while (range != null) {
int len = (int) (range.getEnd() - range.getOffset());
long off = range.getOffset();
BlockLocation[] blockLocations = fs.getFileBlockLocations(path, off, len);
List<Range<Long>> intersectingRanges = new ArrayList<>();
Range<Long> rowGroupRange = Range.openClosed(off, off+len);
for (BlockLocation loc : blockLocations) {
for (String host : loc.getHosts()) {
if (host.equals(localHost)) {
intersectingRanges.add(Range.closedOpen(loc.getOffset(), loc.getOffset() + loc.getLength()).intersection(rowGroupRange));
}
}
}
long totalIntersect = 0;
for (Range<Long> intersectingRange : intersectingRanges) {
totalIntersect += (intersectingRange.upperEndpoint() - intersectingRange.lowerEndpoint());
}
if (totalIntersect < len) {
currentReadIsRemote = true;
break;
}
range = range.next;
}
} catch (Throwable e) {
// ignoring any exception in this code path as it is used to collect
// remote readers metric in profile for debugging
logger.debug("computeLocality failed with message: {} for path {}", e.getMessage(), path, e);
}
if (currentReadIsRemote) {
this.remoteRead = true;
}
}
private void computeLocality(FileSystem fs, Path path, DiskRangeList range) {
if (this.remoteRead) {
return;
}
boolean currentReadIsRemote = false;
try {
String localHost = InetAddress.getLocalHost().getCanonicalHostName();
while (range != null) {
int len = (int) (range.getEnd() - range.getOffset());
long off = range.getOffset();
BlockLocation[] blockLocations = fs.getFileBlockLocations(path, off, len);
List<Range<Long>> intersectingRanges = new ArrayList<>();
Range<Long> rowGroupRange = Range.openClosed(off, off+len);
for (BlockLocation loc : blockLocations) {
for (String host : loc.getHosts()) {
if (host.equals(localHost)) {
intersectingRanges.add(Range.closedOpen(loc.getOffset(), loc.getOffset() + loc.getLength()).intersection(rowGroupRange));
}
}
}
long totalIntersect = 0;
for (Range<Long> intersectingRange : intersectingRanges) {
totalIntersect += (intersectingRange.upperEndpoint() - intersectingRange.lowerEndpoint());
}
if (totalIntersect < len) {
currentReadIsRemote = true;
break;
}
range = range.next;
}
} catch (Throwable e) {
// ignoring any exception in this code path as it is used to collect
// remote readers metric in profile for debugging
logger.debug("computeLocality failed with message: {} for path {}", e.getMessage(), path, e);
}
if (currentReadIsRemote) {
this.remoteRead = true;
}
}
static
public Range<Double> toRange(Interval interval){
Double leftMargin = NumberUtil.asDouble(interval.getLeftMargin());
Double rightMargin = NumberUtil.asDouble(interval.getRightMargin());
// "The leftMargin and rightMargin attributes are optional, but at least one value must be defined"
if(leftMargin == null && rightMargin == null){
throw new MissingAttributeException(interval, PMMLAttributes.INTERVAL_LEFTMARGIN);
} // End if
if(leftMargin != null && rightMargin != null && NumberUtil.compare(leftMargin, rightMargin) > 0){
throw new InvalidElementException(interval);
}
Interval.Closure closure = interval.getClosure();
if(closure == null){
throw new MissingAttributeException(interval, PMMLAttributes.INTERVAL_CLOSURE);
}
switch(closure){
case OPEN_OPEN:
{
if(leftMargin == null){
return Range.lessThan(rightMargin);
} else
if(rightMargin == null){
return Range.greaterThan(leftMargin);
}
return Range.open(leftMargin, rightMargin);
}
case OPEN_CLOSED:
{
if(leftMargin == null){
return Range.atMost(rightMargin);
} else
if(rightMargin == null){
return Range.greaterThan(leftMargin);
}
return Range.openClosed(leftMargin, rightMargin);
}
case CLOSED_OPEN:
{
if(leftMargin == null){
return Range.lessThan(rightMargin);
} else
if(rightMargin == null){
return Range.atLeast(leftMargin);
}
return Range.closedOpen(leftMargin, rightMargin);
}
case CLOSED_CLOSED:
{
if(leftMargin == null){
return Range.atMost(rightMargin);
} else
if(rightMargin == null){
return Range.atLeast(leftMargin);
}
return Range.closed(leftMargin, rightMargin);
}
default:
throw new UnsupportedAttributeException(interval, closure);
}
}
@Test
public void givenRange_whenVerifying_thenShouldBeCorrect() throws Exception {
final Range<String> range = Range.openClosed("a", "g");
assertThat(range).hasOpenedLowerBound().isNotEmpty().hasClosedUpperBound().contains("b");
}