下面列出了com.google.common.collect.BoundType#OPEN 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testIncludes() throws Exception {
TopicName topicName = TopicName.get("persistent://pulsar/use/ns1/topic-1");
Long hashKey = factory.getLongHashCode(topicName.toString());
Long upper = Math.max(hashKey + 1, NamespaceBundles.FULL_UPPER_BOUND);
BoundType upperType = upper.equals(NamespaceBundles.FULL_UPPER_BOUND) ? BoundType.CLOSED : BoundType.OPEN;
NamespaceBundle bundle = factory.getBundle(topicName.getNamespaceObject(),
Range.range(hashKey / 2, BoundType.CLOSED, upper, upperType));
assertTrue(bundle.includes(topicName));
bundle = factory.getBundle(NamespaceName.get("pulsar/use/ns1"),
Range.range(upper, BoundType.CLOSED, NamespaceBundles.FULL_UPPER_BOUND, BoundType.CLOSED));
assertFalse(bundle.includes(topicName));
NamespaceBundle otherBundle = factory.getBundle(NamespaceName.get("pulsar/use/ns2"),
Range.range(0l, BoundType.CLOSED, 0x40000000L, BoundType.OPEN));
assertFalse(otherBundle.includes(topicName));
}
private void computeBmp(RangeMap<Integer, Short> fullMap) {
for (Map.Entry<Range<Integer>, Short> me : fullMap.asMapOfRanges().entrySet()) {
Range<Integer> range = me.getKey();
int min = range.lowerEndpoint();
if (range.lowerBoundType() == BoundType.OPEN) {
min++;
}
if (min < Character.MAX_VALUE) {
int rmax = range.upperEndpoint();
if (range.upperBoundType() == BoundType.OPEN) {
rmax--;
}
int max = Math.min(Character.MAX_VALUE, rmax);
for (int x = min; x <= max; x++) {
this.bmpMap[x] = me.getValue();
}
}
}
}
private <C extends Comparable<C>> RexNode simplifyUsingPredicates(RexNode e, Class<C> clazz) {
final Comparison comparison = Comparison.of(e);
// Check for comparison with null values
if (comparison == null || comparison.kind == SqlKind.NOT_EQUALS || comparison.literal.getValue() == null) {
return e;
}
final C v0 = comparison.literal.getValueAs(clazz);
final Range<C> range = range(comparison.kind, v0);
final Range<C> range2 = residue(comparison.ref, range, predicates.pulledUpPredicates, clazz);
if (range2 == null) {
// Term is impossible to satisfy given these predicates
return rexBuilder.makeLiteral(false);
} else if (range2.equals(range)) {
// no change
return e;
} else if (range2.equals(Range.all())) {
// Range is always satisfied given these predicates; but nullability might
// be problematic
return simplify(rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, comparison.ref), RexUnknownAs.UNKNOWN);
} else if (range2.lowerEndpoint().equals(range2.upperEndpoint())) {
if (range2.lowerBoundType() == BoundType.OPEN || range2.upperBoundType() == BoundType.OPEN) {
// range is a point, but does not include its endpoint, therefore is
// effectively empty
return rexBuilder.makeLiteral(false);
}
// range is now a point; it's worth simplifying
return rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, comparison.ref, rexBuilder.makeLiteral(
range2.lowerEndpoint(), comparison.literal.getType(), comparison.literal.getTypeName()));
} else {
// range has been reduced but it's not worth simplifying
return e;
}
}
private static Range<Long> getHashRange(String rangePathPart) {
String[] endPoints = rangePathPart.split("_");
checkArgument(endPoints.length == 2, "Malformed bundle hash range path part:" + rangePathPart);
Long startLong = Long.decode(endPoints[0]);
Long endLong = Long.decode(endPoints[1]);
BoundType endType = (endPoints[1].equals(LAST_BOUNDARY)) ? BoundType.CLOSED : BoundType.OPEN;
return Range.range(startLong, BoundType.CLOSED, endLong, endType);
}
/**
* Retrieves a random value from an {@code int} range.
*
* @param range The range.
* @return The random value.
*/
public static int random(Range<Integer> range) {
int low = range.hasLowerBound() ? range.lowerEndpoint() : Integer.MIN_VALUE;
int high = range.hasUpperBound() ? range.upperEndpoint() : Integer.MAX_VALUE;
if (range.upperBoundType() == BoundType.OPEN && range.lowerBoundType() == BoundType.CLOSED) {
return inclusive(low - 1, high);
} else if (range.upperBoundType() == BoundType.CLOSED && range.lowerBoundType() == BoundType.OPEN) {
return inclusive(low, high - 1);
} else if (range.upperBoundType() == BoundType.OPEN && range.lowerBoundType() == BoundType.OPEN) {
return inclusive(low, high);
} else if (range.upperBoundType() == BoundType.CLOSED && range.lowerBoundType() == BoundType.CLOSED) {
return inclusive(low - 1, high - 1);
}
throw new Error("impossible");
}
public <O extends Comparable<? super O>>Range<O> bounds() {
O mini = minInclusive();
O mine = minExclusive();
O maxi = maxInclusive();
O maxe = maxExclusive();
Ordering<O> ordering = Ordering.<O>natural();
O min = ordering.nullsLast().min(mini,mine);
O max = ordering.nullsFirst().max(maxi,maxe);
BoundType lower =
min == null ? null :
min == mini ? BoundType.CLOSED :
BoundType.OPEN;
BoundType upper =
max == null ? null :
max == maxi ? BoundType.CLOSED :
BoundType.OPEN;
if (lower == null && upper == null)
return Range.<O>all();
else if (lower != null && upper == null)
return lower == BoundType.CLOSED ?
Range.atLeast(min) :
Range.greaterThan(min);
else if (lower == null && upper != null)
return upper == BoundType.CLOSED ?
Range.atMost(max) :
Range.lessThan(max);
else {
return Range.range(min, lower, max, upper);
}
}
/**
* subrange - allocate new subcolors to this range of chars, fill in arcs.
* The range will overlap existing ranges; even in the simplest case,
* it will overlap the initial WHITE range. For each existing range that
* it overlaps, allocate a new color, mark the range as mapping to that color,
* and add an arc between the states for that color.
*/
void subrange(int from, int to, State lp, State rp) throws RegexException {
/* Avoid one call to map.get() for each character in the range.
* This map will usually contain one item, but in complex cases more.
* For example, if we had [a-f][g-h] and then someone asked for [f-g], there
* would be two. Each of these new ranges will get a new color via subcolor.
*/
Map<Range<Integer>, Short> curColors = map.subRangeMap(Range.closed(from, to)).asMapOfRanges();
/*
* To avoid concurrent mod problems, we need to copy the ranges we are working from.
*/
List<Range<Integer>> ranges = Lists.newArrayList(curColors.keySet());
for (Range<Integer> rangeToProcess : ranges) {
// bound management here irritating.
int start = rangeToProcess.lowerEndpoint();
if (rangeToProcess.lowerBoundType() == BoundType.OPEN) {
start++;
}
int end = rangeToProcess.upperEndpoint();
if (rangeToProcess.upperBoundType() == BoundType.CLOSED) {
end++;
}
// allocate a new subcolor and account it owning the entire range.
short color = subcolor(start, end - start);
compiler.getNfa().newarc(Compiler.PLAIN, color, lp, rp);
}
}
private <C extends Comparable<C>> RexNode simplifyUsingPredicates(RexNode e,
Class<C> clazz) {
final Comparison comparison = Comparison.of(e);
// Check for comparison with null values
if (comparison == null
|| comparison.kind == SqlKind.NOT_EQUALS
|| comparison.literal.getValue() == null) {
return e;
}
final C v0 = comparison.literal.getValueAs(clazz);
final Range<C> range = range(comparison.kind, v0);
final Range<C> range2 =
residue(comparison.ref, range, predicates.pulledUpPredicates,
clazz);
if (range2 == null) {
// Term is impossible to satisfy given these predicates
return rexBuilder.makeLiteral(false);
} else if (range2.equals(range)) {
// no change
return e;
} else if (range2.equals(Range.all())) {
// Range is always satisfied given these predicates; but nullability might
// be problematic
return simplify(
rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, comparison.ref),
RexUnknownAs.UNKNOWN);
} else if (range2.lowerEndpoint().equals(range2.upperEndpoint())) {
if (range2.lowerBoundType() == BoundType.OPEN
|| range2.upperBoundType() == BoundType.OPEN) {
// range is a point, but does not include its endpoint, therefore is
// effectively empty
return rexBuilder.makeLiteral(false);
}
// range is now a point; it's worth simplifying
return rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, comparison.ref,
rexBuilder.makeLiteral(range2.lowerEndpoint(),
comparison.literal.getType(), comparison.literal.getTypeName()));
} else {
// range has been reduced but it's not worth simplifying
return e;
}
}
/**
* remove from self the elements that exist in other
* @return
*/
public static <C extends Comparable<?>> List<Range<C>> remove(Range<C> self, Range<C> other) {
// mimic the following logic in guava 18:
// RangeSet<C> rangeSet = TreeRangeSet.create();
// rangeSet.add(self);
// rangeSet.remove(other);
// return Lists.newArrayList(rangeSet.asRanges());
if (other == null || !self.isConnected(other)) {
return Collections.singletonList(self);
}
Range<C> share = self.intersection(other);
if (share.isEmpty()) {
return Collections.singletonList(self);
}
List<Range<C>> ret = Lists.newArrayList();
//see left part
if (!self.hasLowerBound()) {
if (share.hasLowerBound()) {
if (share.lowerBoundType() == BoundType.CLOSED) {
ret.add(Range.lessThan(share.lowerEndpoint()));
} else {
ret.add(Range.atMost(share.lowerEndpoint()));
}
}
} else {
if (self.lowerEndpoint() != share.lowerEndpoint()) {
if (self.lowerBoundType() == BoundType.CLOSED) {
if (share.lowerBoundType() == BoundType.CLOSED) {
ret.add(Range.closedOpen(self.lowerEndpoint(), share.lowerEndpoint()));
} else {
ret.add(Range.closed(self.lowerEndpoint(), share.lowerEndpoint()));
}
} else {
if (share.lowerBoundType() == BoundType.CLOSED) {
ret.add(Range.open(self.lowerEndpoint(), share.lowerEndpoint()));
} else {
ret.add(Range.openClosed(self.lowerEndpoint(), share.lowerEndpoint()));
}
}
} else {
if (self.lowerBoundType() == BoundType.CLOSED && share.lowerBoundType() == BoundType.OPEN) {
ret.add(Range.closed(self.lowerEndpoint(), share.lowerEndpoint()));
}
}
}
//see right part
if (!self.hasUpperBound()) {
if (share.hasUpperBound()) {
if (share.upperBoundType() == BoundType.CLOSED) {
ret.add(Range.greaterThan(share.upperEndpoint()));
} else {
ret.add(Range.atLeast(share.upperEndpoint()));
}
}
} else {
if (self.upperEndpoint() != share.upperEndpoint()) {
if (self.upperBoundType() == BoundType.CLOSED) {
if (share.upperBoundType() == BoundType.CLOSED) {
ret.add(Range.openClosed(share.upperEndpoint(), self.upperEndpoint()));
} else {
ret.add(Range.closed(share.upperEndpoint(), self.upperEndpoint()));
}
} else {
if (share.upperBoundType() == BoundType.CLOSED) {
ret.add(Range.open(share.upperEndpoint(), self.upperEndpoint()));
} else {
ret.add(Range.closedOpen(share.upperEndpoint(), self.upperEndpoint()));
}
}
} else {
if (self.upperBoundType() == BoundType.CLOSED && share.upperBoundType() == BoundType.OPEN) {
ret.add(Range.closed(self.upperEndpoint(), share.upperEndpoint()));
}
}
}
return ret;
}
/**
* Parse a range in the standard mathematical format e.g.
*
* [0, 1) for a closed-open range from 0 to 1.
*
* Can also parse single numbers as a closed range e.g.
*
* 5 for a closed-closed range from 5 to 5.
*/
public static <T extends Number & Comparable<T>> Range<T> parseNumericRange(Node node, Class<T> type) throws InvalidXMLException {
String value = node.getValue();
Matcher matcher = RANGE_RE.matcher(value);
if(!matcher.matches()) {
T number = parseNumber(node, value, type, (T) null);
if(number != null) {
return Range.singleton(number);
}
throw new InvalidXMLException("Invalid " + type.getSimpleName().toLowerCase() + " range '" + value + "'", node);
}
T lower = parseNumber(node, matcher.group(2), type, true);
T upper = parseNumber(node, matcher.group(3), type, true);
BoundType lowerType = null, upperType = null;
if(!Double.isInfinite(lower.doubleValue())) {
lowerType = "(".equals(matcher.group(1)) ? BoundType.OPEN : BoundType.CLOSED;
}
if(!Double.isInfinite(upper.doubleValue())) {
upperType = ")".equals(matcher.group(4)) ? BoundType.OPEN : BoundType.CLOSED;
}
if(lower.compareTo(upper) == 1) {
throw new InvalidXMLException("range lower bound (" + lower + ") cannot be greater than upper bound (" + upper + ")", node);
}
if(lowerType == null) {
if(upperType == null) {
return Range.all();
} else {
return Range.upTo(upper, upperType);
}
} else {
if(upperType == null) {
return Range.downTo(lower, lowerType);
} else {
return Range.range(lower, lowerType, upper, upperType);
}
}
}
public static <T extends Number & Comparable<T>> Range<T> parseNumericRange(Element el, Class<T> type, Range<T> def) throws InvalidXMLException {
Attribute lt = el.getAttribute("lt");
Attribute lte = getAttribute(el, "lte", "max");
Attribute gt = el.getAttribute("gt");
Attribute gte = getAttribute(el, "gte", "min");
if(lt != null && lte != null) throw new InvalidXMLException("Conflicting upper bound for numeric range", el);
if(gt != null && gte != null) throw new InvalidXMLException("Conflicting lower bound for numeric range", el);
BoundType lowerBoundType, upperBoundType;
T lowerBound, upperBound;
if(gt != null) {
lowerBound = parseNumber(gt, type, (T) null);
lowerBoundType = BoundType.OPEN;
} else {
lowerBound = parseNumber(gte, type, (T) null);
lowerBoundType = BoundType.CLOSED;
}
if(lt != null) {
upperBound = parseNumber(lt, type, (T) null);
upperBoundType = BoundType.OPEN;
} else {
upperBound = parseNumber(lte, type, (T) null);
upperBoundType = BoundType.CLOSED;
}
if(lowerBound == null) {
if(upperBound == null) {
return def;
} else {
return Range.upTo(upperBound, upperBoundType);
}
} else {
if(upperBound == null) {
return Range.downTo(lowerBound, lowerBoundType);
} else {
return Range.range(lowerBound, lowerBoundType, upperBound, upperBoundType);
}
}
}
@SuppressWarnings("unchecked")
public static <T extends Comparable> Range<T> ofString(String str, Function<String, T> converter, Class<T> cls) {
BoundType lowerBound = str.charAt(0) == '[' ? BoundType.CLOSED : BoundType.OPEN;
BoundType upperBound = str.charAt(str.length() - 1) == ']' ? BoundType.CLOSED : BoundType.OPEN;
int delim = str.indexOf(',');
if (delim == -1) {
throw new IllegalArgumentException("Cannot find comma character");
}
String lowerStr = str.substring(1, delim);
String upperStr = str.substring(delim + 1, str.length() - 1);
T lower = null;
T upper = null;
if (lowerStr.length() > 0) {
lower = converter.apply(lowerStr);
}
if (upperStr.length() > 0) {
upper = converter.apply(upperStr);
}
if (lower == null && upper == null) {
throw new IllegalArgumentException("Cannot find bound type");
}
if (lowerStr.length() == 0) {
return upperBound == BoundType.CLOSED ?
Range.atMost(upper) :
Range.lessThan(upper);
} else if (upperStr.length() == 0) {
return lowerBound == BoundType.CLOSED ?
Range.atLeast(lower) :
Range.greaterThan(lower);
} else {
return Range.range(lower, lowerBound, upper, upperBound);
}
}
@SuppressWarnings("unchecked")
public static <T extends Comparable> Range<T> ofString(String str, Function<String, T> converter, Class<T> cls) {
BoundType lowerBound = str.charAt(0) == '[' ? BoundType.CLOSED : BoundType.OPEN;
BoundType upperBound = str.charAt(str.length() - 1) == ']' ? BoundType.CLOSED : BoundType.OPEN;
int delim = str.indexOf(',');
if (delim == -1) {
throw new IllegalArgumentException("Cannot find comma character");
}
String lowerStr = str.substring(1, delim);
String upperStr = str.substring(delim + 1, str.length() - 1);
T lower = null;
T upper = null;
if (lowerStr.length() > 0) {
lower = converter.apply(lowerStr);
}
if (upperStr.length() > 0) {
upper = converter.apply(upperStr);
}
if (lower == null && upper == null) {
throw new IllegalArgumentException("Cannot find bound type");
}
if (lowerStr.length() == 0) {
return upperBound == BoundType.CLOSED ?
Ranges.atMost(upper) :
Ranges.lessThan(upper);
} else if (upperStr.length() == 0) {
return lowerBound == BoundType.CLOSED ?
Ranges.atLeast(lower) :
Ranges.greaterThan(lower);
} else {
return Ranges.range(lower, lowerBound, upper, upperBound);
}
}
@SuppressWarnings("unchecked")
public static <T extends Comparable> Range<T> ofString(String str, Function<String, T> converter, Class<T> cls) {
BoundType lowerBound = str.charAt(0) == '[' ? BoundType.CLOSED : BoundType.OPEN;
BoundType upperBound = str.charAt(str.length() - 1) == ']' ? BoundType.CLOSED : BoundType.OPEN;
int delim = str.indexOf(',');
if (delim == -1) {
throw new IllegalArgumentException("Cannot find comma character");
}
String lowerStr = str.substring(1, delim);
String upperStr = str.substring(delim + 1, str.length() - 1);
T lower = null;
T upper = null;
if (lowerStr.length() > 0) {
lower = converter.apply(lowerStr);
}
if (upperStr.length() > 0) {
upper = converter.apply(upperStr);
}
if (lower == null && upper == null) {
throw new IllegalArgumentException("Cannot find bound type");
}
if (lowerStr.length() == 0) {
return upperBound == BoundType.CLOSED ?
Ranges.atMost(upper) :
Ranges.lessThan(upper);
} else if (upperStr.length() == 0) {
return lowerBound == BoundType.CLOSED ?
Ranges.atLeast(lower) :
Ranges.greaterThan(lower);
} else {
return Ranges.range(lower, lowerBound, upper, upperBound);
}
}
@SuppressWarnings("unchecked")
public static <T extends Comparable> Range<T> ofString(String str, Function<String, T> converter, Class<T> cls) {
BoundType lowerBound = str.charAt(0) == '[' ? BoundType.CLOSED : BoundType.OPEN;
BoundType upperBound = str.charAt(str.length() - 1) == ']' ? BoundType.CLOSED : BoundType.OPEN;
int delim = str.indexOf(',');
if (delim == -1) {
throw new IllegalArgumentException("Cannot find comma character");
}
String lowerStr = str.substring(1, delim);
String upperStr = str.substring(delim + 1, str.length() - 1);
T lower = null;
T upper = null;
if (lowerStr.length() > 0) {
lower = converter.apply(lowerStr);
}
if (upperStr.length() > 0) {
upper = converter.apply(upperStr);
}
if (lower == null && upper == null) {
throw new IllegalArgumentException("Cannot find bound type");
}
if (lowerStr.length() == 0) {
return upperBound == BoundType.CLOSED ?
Ranges.atMost(upper) :
Ranges.lessThan(upper);
} else if (upperStr.length() == 0) {
return lowerBound == BoundType.CLOSED ?
Ranges.atLeast(lower) :
Ranges.greaterThan(lower);
} else {
return Ranges.range(lower, lowerBound, upper, upperBound);
}
}
private <C extends Comparable<C>> RexNode simplifyUsingPredicates(RexNode e,
Class<C> clazz) {
if (predicates.pulledUpPredicates.isEmpty()) {
return e;
}
if (e.getKind() == SqlKind.NOT_EQUALS) {
return simplifyNotEqual(e);
}
final Comparison comparison = Comparison.of(e);
// Check for comparison with null values
if (comparison == null
|| comparison.literal.getValue() == null) {
return e;
}
final C v0 = comparison.literal.getValueAs(clazz);
final Range<C> range = range(comparison.kind, v0);
final Range<C> range2 =
residue(comparison.ref, range, predicates.pulledUpPredicates,
clazz);
if (range2 == null) {
// Term is impossible to satisfy given these predicates
return rexBuilder.makeLiteral(false);
} else if (range2.equals(range)) {
// no change
return e;
} else if (range2.equals(Range.all())) {
// Range is always satisfied given these predicates; but nullability might
// be problematic
return simplify(
rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, comparison.ref),
RexUnknownAs.UNKNOWN);
} else if (range2.lowerEndpoint().equals(range2.upperEndpoint())) {
if (range2.lowerBoundType() == BoundType.OPEN
|| range2.upperBoundType() == BoundType.OPEN) {
// range is a point, but does not include its endpoint, therefore is
// effectively empty
return rexBuilder.makeLiteral(false);
}
// range is now a point; it's worth simplifying
return rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, comparison.ref,
rexBuilder.makeLiteral(range2.lowerEndpoint(),
comparison.literal.getType(), comparison.literal.getTypeName()));
} else {
// range has been reduced but it's not worth simplifying
return e;
}
}