List of usage examples for com.google.common.collect Range atMost
public static <C extends Comparable<?>> Range<C> atMost(C endpoint)
From source file:org.apache.druid.query.filter.BoundDimFilter.java
@Override public RangeSet<String> getDimensionRangeSet(String dimension) { if (!(Objects.equals(getDimension(), dimension) && getExtractionFn() == null && ordering.equals(StringComparators.LEXICOGRAPHIC))) { return null; }/*ww w. j a v a2 s. c om*/ RangeSet<String> retSet = TreeRangeSet.create(); Range<String> range; if (getLower() == null) { range = isUpperStrict() ? Range.lessThan(getUpper()) : Range.atMost(getUpper()); } else if (getUpper() == null) { range = isLowerStrict() ? Range.greaterThan(getLower()) : Range.atLeast(getLower()); } else { range = Range.range(getLower(), isLowerStrict() ? BoundType.OPEN : BoundType.CLOSED, getUpper(), isUpperStrict() ? BoundType.OPEN : BoundType.CLOSED); } retSet.add(range); return retSet; }
From source file:org.opentestsystem.authoring.testauth.validation.AbstractDomainValidator.java
protected Range<Float> buildFloatRange(final String min, final String max) { Range<Float> floatRange = null; if (StringUtils.isNotEmpty(min) && StringUtils.isNotEmpty(max)) { // has both ends of a range floatRange = Range.closed(Float.valueOf(min), Float.valueOf(max)); } else if (StringUtils.isNotEmpty(min) && StringUtils.isEmpty(max)) { // only has a minimum floatRange = Range.atLeast(Float.valueOf(min)); } else if (StringUtils.isEmpty(min) && StringUtils.isNotEmpty(max)) { // only has a maximum floatRange = Range.atMost(Float.valueOf(max)); }/*from w ww.ja v a 2s . c om*/ return floatRange; }
From source file:org.opentestsystem.authoring.testauth.validation.AbstractDomainValidator.java
protected Range<Long> buildLongRange(final String min, final String max) { Range<Long> longRange = null; if (StringUtils.isNotEmpty(min) && StringUtils.isNotEmpty(max)) { // has both ends of a range longRange = Range.closed(Long.valueOf(min), Long.valueOf(max)); } else if (StringUtils.isNotEmpty(min) && StringUtils.isEmpty(max)) { // only has a minimum longRange = Range.atLeast(Long.valueOf(min)); } else if (StringUtils.isEmpty(min) && StringUtils.isNotEmpty(max)) { // only has a maximum longRange = Range.atMost(Long.valueOf(max)); }// ww w. j a v a 2 s .c om return longRange; }
From source file:com.ibm.common.activitystreams.actions.Parameter.java
public <O extends Comparable<? super O>> Range<O> bounds() { O mini = minInclusive();//w w w. j a va 2s. c om 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); } }
From source file:com.palantir.atlasdb.keyvalue.cassandra.CassandraClientPool.java
private void refreshTokenRanges() { try {/* www .j a va 2 s . c o m*/ List<TokenRange> tokenRanges = getRandomGoodHost().runWithPooledResource(describeRing); ImmutableRangeMap.Builder<LightweightOPPToken, List<InetSocketAddress>> newTokenRing = ImmutableRangeMap .builder(); for (TokenRange tokenRange : tokenRanges) { List<InetSocketAddress> hosts = Lists.transform(tokenRange.getEndpoints(), new Function<String, InetSocketAddress>() { @Override public InetSocketAddress apply(String endpoint) { return new InetSocketAddress(endpoint, CassandraConstants.DEFAULT_THRIFT_PORT); } }); LightweightOPPToken startToken = new LightweightOPPToken( BaseEncoding.base16().decode(tokenRange.getStart_token().toUpperCase())); LightweightOPPToken endToken = new LightweightOPPToken( BaseEncoding.base16().decode(tokenRange.getEnd_token().toUpperCase())); if (startToken.compareTo(endToken) <= 0) { newTokenRing.put(Range.openClosed(startToken, endToken), hosts); } else { // Handle wrap-around newTokenRing.put(Range.greaterThan(startToken), hosts); newTokenRing.put(Range.atMost(endToken), hosts); } } tokenMap = newTokenRing.build(); } catch (Exception e) { log.error("Couldn't grab new token ranges for token aware cassandra mapping!", e); } }
From source file:org.apache.calcite.rex.RexSimplify.java
private static RexNode processRange(RexBuilder rexBuilder, List<RexNode> terms, Map<String, Pair<Range, List<RexNode>>> rangeTerms, RexNode term, RexNode ref, RexLiteral constant, SqlKind comparison) {//from w w w . j av a2 s .c o m final Comparable v0 = constant.getValue(); Pair<Range, List<RexNode>> p = rangeTerms.get(ref.toString()); if (p == null) { Range r; switch (comparison) { case EQUALS: r = Range.singleton(v0); break; case LESS_THAN: r = Range.lessThan(v0); break; case LESS_THAN_OR_EQUAL: r = Range.atMost(v0); break; case GREATER_THAN: r = Range.greaterThan(v0); break; case GREATER_THAN_OR_EQUAL: r = Range.atLeast(v0); break; default: throw new AssertionError(); } rangeTerms.put(ref.toString(), new Pair(r, ImmutableList.of(term))); } else { // Exists boolean removeUpperBound = false; boolean removeLowerBound = false; Range r = p.left; switch (comparison) { case EQUALS: if (!r.contains(v0)) { // Range is empty, not satisfiable return rexBuilder.makeLiteral(false); } rangeTerms.put(ref.toString(), new Pair(Range.singleton(v0), ImmutableList.of(term))); // remove terms.removeAll(p.right); break; case LESS_THAN: { int comparisonResult = 0; if (r.hasUpperBound()) { comparisonResult = v0.compareTo(r.upperEndpoint()); } if (comparisonResult <= 0) { // 1) No upper bound, or // 2) We need to open the upper bound, or // 3) New upper bound is lower than old upper bound if (r.hasLowerBound()) { if (v0.compareTo(r.lowerEndpoint()) < 0) { // Range is empty, not satisfiable return rexBuilder.makeLiteral(false); } // a <= x < b OR a < x < b r = Range.range(r.lowerEndpoint(), r.lowerBoundType(), v0, BoundType.OPEN); } else { // x < b r = Range.lessThan(v0); } if (r.isEmpty()) { // Range is empty, not satisfiable return rexBuilder.makeLiteral(false); } // remove prev upper bound removeUpperBound = true; } else { // Remove this term as it is contained in current upper bound terms.remove(term); } break; } case LESS_THAN_OR_EQUAL: { int comparisonResult = -1; if (r.hasUpperBound()) { comparisonResult = v0.compareTo(r.upperEndpoint()); } if (comparisonResult < 0) { // 1) No upper bound, or // 2) New upper bound is lower than old upper bound if (r.hasLowerBound()) { if (v0.compareTo(r.lowerEndpoint()) < 0) { // Range is empty, not satisfiable return rexBuilder.makeLiteral(false); } // a <= x <= b OR a < x <= b r = Range.range(r.lowerEndpoint(), r.lowerBoundType(), v0, BoundType.CLOSED); } else { // x <= b r = Range.atMost(v0); } if (r.isEmpty()) { // Range is empty, not satisfiable return rexBuilder.makeLiteral(false); } // remove prev upper bound removeUpperBound = true; } else { // Remove this term as it is contained in current upper bound terms.remove(term); } break; } case GREATER_THAN: { int comparisonResult = 0; if (r.hasLowerBound()) { comparisonResult = v0.compareTo(r.lowerEndpoint()); } if (comparisonResult >= 0) { // 1) No lower bound, or // 2) We need to open the lower bound, or // 3) New lower bound is greater than old lower bound if (r.hasUpperBound()) { if (v0.compareTo(r.upperEndpoint()) > 0) { // Range is empty, not satisfiable return rexBuilder.makeLiteral(false); } // a < x <= b OR a < x < b r = Range.range(v0, BoundType.OPEN, r.upperEndpoint(), r.upperBoundType()); } else { // x > a r = Range.greaterThan(v0); } if (r.isEmpty()) { // Range is empty, not satisfiable return rexBuilder.makeLiteral(false); } // remove prev lower bound removeLowerBound = true; } else { // Remove this term as it is contained in current lower bound terms.remove(term); } break; } case GREATER_THAN_OR_EQUAL: { int comparisonResult = 1; if (r.hasLowerBound()) { comparisonResult = v0.compareTo(r.lowerEndpoint()); } if (comparisonResult > 0) { // 1) No lower bound, or // 2) New lower bound is greater than old lower bound if (r.hasUpperBound()) { if (v0.compareTo(r.upperEndpoint()) > 0) { // Range is empty, not satisfiable return rexBuilder.makeLiteral(false); } // a <= x <= b OR a <= x < b r = Range.range(v0, BoundType.CLOSED, r.upperEndpoint(), r.upperBoundType()); } else { // x >= a r = Range.atLeast(v0); } if (r.isEmpty()) { // Range is empty, not satisfiable return rexBuilder.makeLiteral(false); } // remove prev lower bound removeLowerBound = true; } else { // Remove this term as it is contained in current lower bound terms.remove(term); } break; } default: throw new AssertionError(); } if (removeUpperBound) { ImmutableList.Builder<RexNode> newBounds = ImmutableList.builder(); for (RexNode e : p.right) { if (e.isA(SqlKind.LESS_THAN) || e.isA(SqlKind.LESS_THAN_OR_EQUAL)) { terms.remove(e); } else { newBounds.add(e); } } newBounds.add(term); rangeTerms.put(ref.toString(), new Pair(r, newBounds.build())); } else if (removeLowerBound) { ImmutableList.Builder<RexNode> newBounds = ImmutableList.builder(); for (RexNode e : p.right) { if (e.isA(SqlKind.GREATER_THAN) || e.isA(SqlKind.GREATER_THAN_OR_EQUAL)) { terms.remove(e); } else { newBounds.add(e); } } newBounds.add(term); rangeTerms.put(ref.toString(), new Pair(r, newBounds.build())); } } // Default return null; }
From source file:org.apache.bookkeeper.mledger.impl.ManagedCursorImpl.java
/** * * @param newMarkDeletePosition//from w w w . jav a 2 s. co m * the new acknowledged position * @return the previous acknowledged position */ PositionImpl setAcknowledgedPosition(PositionImpl newMarkDeletePosition) { if (newMarkDeletePosition.compareTo(markDeletePosition) < 0) { throw new IllegalArgumentException("Mark deleting an already mark-deleted position"); } if (readPosition.compareTo(newMarkDeletePosition) <= 0) { // If the position that is mark-deleted is past the read position, it // means that the client has skipped some entries. We need to move // read position forward PositionImpl oldReadPosition = readPosition; readPosition = ledger.getNextValidPosition(newMarkDeletePosition); if (log.isDebugEnabled()) { log.debug("Moved read position from: {} to: {}", oldReadPosition, readPosition); } } PositionImpl oldMarkDeletePosition = markDeletePosition; if (!newMarkDeletePosition.equals(oldMarkDeletePosition)) { long skippedEntries = 0; if (newMarkDeletePosition.getLedgerId() == oldMarkDeletePosition.getLedgerId() && newMarkDeletePosition.getEntryId() == oldMarkDeletePosition.getEntryId() + 1) { // Mark-deleting the position next to current one skippedEntries = individualDeletedMessages.contains(newMarkDeletePosition) ? 0 : 1; } else { skippedEntries = getNumberOfEntries(Range.openClosed(oldMarkDeletePosition, newMarkDeletePosition)); } PositionImpl positionAfterNewMarkDelete = ledger.getNextValidPosition(newMarkDeletePosition); if (individualDeletedMessages.contains(positionAfterNewMarkDelete)) { Range<PositionImpl> rangeToBeMarkDeleted = individualDeletedMessages .rangeContaining(positionAfterNewMarkDelete); newMarkDeletePosition = rangeToBeMarkDeleted.upperEndpoint(); } if (log.isDebugEnabled()) { log.debug("Moved ack position from: {} to: {} -- skipped: {}", oldMarkDeletePosition, newMarkDeletePosition, skippedEntries); } messagesConsumedCounter += skippedEntries; } // markDelete-position and clear out deletedMsgSet markDeletePosition = PositionImpl.get(newMarkDeletePosition); individualDeletedMessages.remove(Range.atMost(markDeletePosition)); return newMarkDeletePosition; }
From source file:org.apache.bookkeeper.mledger.impl.ManagedCursorImpl.java
void internalMarkDelete(final PendingMarkDeleteEntry mdEntry) { // The counter is used to mark all the pending mark-delete request that were submitted to BK and that are not // yet finished. While we have outstanding requests we cannot close the current ledger, so the switch to new // ledger is postponed to when the counter goes to 0. PENDING_MARK_DELETED_SUBMITTED_COUNT_UPDATER.incrementAndGet(this); persistPosition(cursorLedger, mdEntry.newPosition, new VoidCallback() { @Override/*from w w w .j av a 2 s . c o m*/ public void operationComplete() { if (log.isDebugEnabled()) { log.debug("[{}] Mark delete cursor {} to position {} succeeded", ledger.getName(), name, mdEntry.newPosition); } // Remove from the individual deleted messages all the entries before the new mark delete // point. lock.writeLock().lock(); try { individualDeletedMessages.remove(Range.atMost(mdEntry.newPosition)); } finally { lock.writeLock().unlock(); } ledger.updateCursor(ManagedCursorImpl.this, mdEntry.newPosition); decrementPendingMarkDeleteCount(); // Trigger the final callback after having (eventually) triggered the switchin-ledger operation. This // will ensure that no race condition will happen between the next mark-delete and the switching // operation. if (mdEntry.callbackGroup != null) { // Trigger the callback for every request in the group for (PendingMarkDeleteEntry e : mdEntry.callbackGroup) { e.callback.markDeleteComplete(e.ctx); } } else { // Only trigger the callback for the current request mdEntry.callback.markDeleteComplete(mdEntry.ctx); } } @Override public void operationFailed(ManagedLedgerException exception) { log.warn("[{}] Failed to mark delete position for cursor={} position={}", ledger.getName(), ManagedCursorImpl.this, mdEntry.newPosition); if (log.isDebugEnabled()) { log.debug( "[{}] Consumer {} cursor mark delete failed with counters: consumed {} mdPos {} rdPos {}", ledger.getName(), name, messagesConsumedCounter, markDeletePosition, readPosition); } decrementPendingMarkDeleteCount(); if (mdEntry.callbackGroup != null) { for (PendingMarkDeleteEntry e : mdEntry.callbackGroup) { e.callback.markDeleteFailed(exception, e.ctx); } } else { mdEntry.callback.markDeleteFailed(exception, mdEntry.ctx); } } }); }