Java Code Examples for com.google.common.collect.BoundType#CLOSED

The following examples show how to use com.google.common.collect.BoundType#CLOSED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NamespaceBundleTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example 2
Source File: ServiceUnitZkUtils.java    From pulsar with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 3
Source File: RestartingS3InputStream.java    From emodb with Apache License 2.0 5 votes vote down vote up
public RestartingS3InputStream(AmazonS3 s3, String bucket, String key, @Nullable Range<Long> range) {
    _s3 = s3;
    _bucket = bucket;
    _key = key;

    S3Object s3Object;

    // Get the object synchronously so any immediate S3 errors, such as file not found, are thrown inline.
    if (range == null) {
        s3Object = _s3.getObject(new GetObjectRequest(_bucket, _key));
        _pos = 0;
        _length = s3Object.getObjectMetadata().getContentLength();
    } else {
        long start, end;

        if (range.hasLowerBound()) {
            start = range.lowerEndpoint() + (range.lowerBoundType() == BoundType.CLOSED ? 0 : 1);
        } else {
            start = 0;
        }

        if (range.hasUpperBound()) {
            end = range.upperEndpoint() - (range.upperBoundType() == BoundType.CLOSED ? 0 : 1);
        } else {
            end = Long.MAX_VALUE;
        }

        s3Object = _s3.getObject(new GetObjectRequest(_bucket, _key).withRange(start, end));

        _pos = start;
        // The S3 metadata's content length is the length of the data actually being returned by S3.
        // Since we effectively skipped the first "start" bytes we need to add them back to the total length
        // of data being read to make future calculations using _pos and _length consistent.
        _length = start + s3Object.getObjectMetadata().getContentLength();
    }

    _in = s3Object.getObjectContent();
}
 
Example 4
Source File: Parameter.java    From activitystreams with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example 5
Source File: RandomUtils.java    From luna with MIT License 5 votes vote down vote up
/**
 * 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");
}
 
Example 6
Source File: NumberSpace.java    From batfish with Apache License 2.0 5 votes vote down vote up
/**
 * Return least value.
 *
 * @throws NoSuchElementException if space is empty
 */
public final @Nonnull T least() {
  Range<T> span = _rangeset.span();
  T lowerEndpoint = span.lowerEndpoint();
  return span.lowerBoundType() == BoundType.CLOSED
      ? lowerEndpoint
      : discreteDomain().next(lowerEndpoint);
}
 
Example 7
Source File: KillStreakFilter.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected QueryResponse queryPlayer(PlayerQuery query, MatchPlayer player) {
  int streak =
      player.getMatch().needModule(KillRewardMatchModule.class).getKillStreak(player.getId());
  if (this.repeat && streak > 0) {
    int modulo =
        this.range.upperEndpoint() - (this.range.upperBoundType() == BoundType.CLOSED ? 0 : 1);
    streak = 1 + (streak - 1) % modulo;
  }
  return QueryResponse.fromBoolean(this.range.contains(streak));
}
 
Example 8
Source File: NumberSpace.java    From batfish with Apache License 2.0 5 votes vote down vote up
/**
 * Return greatest value.
 *
 * @throws NoSuchElementException if space is empty
 */
public final @Nonnull T greatest() {
  Range<T> span = _rangeset.span();
  T upperEndpoint = span.upperEndpoint();
  return span.upperBoundType() == BoundType.CLOSED
      ? upperEndpoint
      : discreteDomain().previous(upperEndpoint);
}
 
Example 9
Source File: Ranges.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static int needMaximum(Range<Integer> range) {
    assertUpperBound(range);
    return range.upperBoundType() == BoundType.CLOSED ? range.upperEndpoint()
                                                      : range.upperEndpoint() - 1;
}
 
Example 10
Source File: PostgreSQLGuavaRangeType.java    From hibernate-types with Apache License 2.0 4 votes vote down vote up
@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);
    }
}
 
Example 11
Source File: PostgreSQLGuavaRangeType.java    From hibernate-types with Apache License 2.0 4 votes vote down vote up
@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);
    }
}
 
Example 12
Source File: PostgreSQLGuavaRangeType.java    From hibernate-types with Apache License 2.0 4 votes vote down vote up
@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);
    }
}
 
Example 13
Source File: Ranges.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Range<Integer> toClosed(Range<Integer> range) {
    assertBounded(range);
    return range.lowerBoundType() == BoundType.CLOSED && range.upperBoundType() == BoundType.CLOSED
           ? range
           : Range.closed(needMinimum(range), needMaximum(range));
}
 
Example 14
Source File: Ranges.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static int needOpenMaximum(Range<Integer> range) {
    assertUpperBound(range);
    return range.upperBoundType() == BoundType.CLOSED ? range.upperEndpoint() + 1
                                                      : range.upperEndpoint();
}
 
Example 15
Source File: Ranges.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static int needMinimum(Range<Integer> range) {
    assertLowerBound(range);
    return range.lowerBoundType() == BoundType.CLOSED ? range.lowerEndpoint()
                                                      : range.lowerEndpoint() + 1;
}
 
Example 16
Source File: XMLUtils.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example 17
Source File: Support.java    From immutables with Apache License 2.0 4 votes vote down vote up
private String boundToOperator(boolean lower, boolean negate, BoundType lowerBoundType) {
  boolean closedBound = lowerBoundType == BoundType.CLOSED;
  return comparisonOperators[lower ^ negate ? 1 : 0][closedBound ^ negate ? 1 : 0];
}
 
Example 18
Source File: RangeUtil.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
/**
 * 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;

}
 
Example 19
Source File: RangeUtil.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
public static <C extends Comparable<?>> boolean upperBoundInclusive(Range<C> range) {
    if (!range.hasUpperBound()) {
        throw new IllegalArgumentException(("This range does not have upper bound" + range));
    }
    return range.upperBoundType() == BoundType.CLOSED;
}
 
Example 20
Source File: RangeUtil.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
public static <C extends Comparable<?>> boolean lowerBoundInclusive(Range<C> range) {
    if (!range.hasLowerBound()) {
        throw new IllegalArgumentException(("This range does not have lower bound" + range));
    }
    return range.lowerBoundType() == BoundType.CLOSED;
}