Java Code Examples for com.google.common.collect.Range#equals()

The following examples show how to use com.google.common.collect.Range#equals() . 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: RangeUtil.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * for NavigableMap sorted by C, given a range of C, return the sub map whose key falls in the range
 */
public static <C extends Comparable<?>, V> NavigableMap<C, V> filter(NavigableMap<C, V> values, Range<C> filterRange) {
    if (filterRange == null || filterRange.isEmpty()) {
        return Maps.newTreeMap();
    } else if (filterRange.equals(Range.all())) {
        return values;
    }

    if (filterRange.hasUpperBound() && !filterRange.hasLowerBound()) {
        return values.headMap(filterRange.upperEndpoint(), upperBoundInclusive(filterRange));
    } else if (filterRange.hasLowerBound() && !filterRange.hasUpperBound()) {
        return values.tailMap(filterRange.lowerEndpoint(), lowerBoundInclusive(filterRange));
    } else {
        return values.subMap(filterRange.lowerEndpoint(), lowerBoundInclusive(filterRange), //
                filterRange.upperEndpoint(), upperBoundInclusive(filterRange));
    }
}
 
Example 2
Source File: RexSimplify.java    From Bats with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 3
Source File: RexSimplify.java    From Quicksql with MIT License 4 votes vote down vote up
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;
  }
}
 
Example 4
Source File: ScreenDensitySelector.java    From bundletool with Apache License 2.0 4 votes vote down vote up
/**
 * For a given range of devices dpi served by a split, returns the range of resources that are
 * reachable from this split.
 *
 * <p>The reachable resources can cover a be different dpi range than that of the devices since a
 * resource with dpi smaller than the lowest dpi device can be still preferred by that device.
 *
 * @param deviceDpiRange range of device dpis to serve by a given split
 * @param values {@link ConfigValue} objects of a resource under consideration
 * @return range of resource config values that can be matched by any device served by the split
 */
@CheckReturnValue
private ImmutableList<ConfigValue> getReachableConfigValues(
    Range<Integer> deviceDpiRange, ImmutableList<ConfigValue> values, Version bundleVersion) {
  // We are calculating the lowest and highest dpi of the resource that could be matched by the
  // devices from the given deviceDpiRange.
  // We take the lowest eligible dpi device and find the best matching resource for it, similarly
  // at the other end of the servedDpiRange. Because the best matching is monotonic, all resource
  // configs between those extremes and no others could be matched by any device that falls into
  // that dpi range.

  Optional<Integer> lowestResourceDpi = Optional.empty();
  Optional<Integer> highestResourceDpi = Optional.empty();
  if (deviceDpiRange.hasLowerBound()) {
    lowestResourceDpi =
        Optional.of(
            selectBestConfigValue(values, deviceDpiRange.lowerEndpoint(), bundleVersion)
                .getConfig()
                .getDensity());
  }
  if (deviceDpiRange.hasUpperBound()) {
    highestResourceDpi =
        Optional.of(
            selectBestConfigValue(values, deviceDpiRange.upperEndpoint(), bundleVersion)
                .getConfig()
                .getDensity());
  }

  Range<Integer> effectiveDpiRange;

  if (deviceDpiRange.equals(Range.all())) {
    effectiveDpiRange = Range.all();
  } else if (!lowestResourceDpi.isPresent()) {
    effectiveDpiRange = Range.atMost(highestResourceDpi.get());
  } else if (!highestResourceDpi.isPresent()) {
    effectiveDpiRange = Range.atLeast(lowestResourceDpi.get());
  } else {
    effectiveDpiRange = Range.closed(lowestResourceDpi.get(), highestResourceDpi.get());
  }

  return values.stream()
      .filter(configValue -> effectiveDpiRange.contains(configValue.getConfig().getDensity()))
      .collect(toImmutableList());
}
 
Example 5
Source File: SchedulerImpl.java    From ganttproject with GNU General Public License v3.0 4 votes vote down vote up
private void schedule(Node node) {
  Logger logger = GPLogger.getLogger(this);
  GPLogger.debug(logger, "Scheduling node %s", node);
  Range<Date> startRange = Range.all();
  Range<Date> endRange = Range.all();

  Range<Date> weakStartRange = Range.all();
  Range<Date> weakEndRange = Range.all();

  List<Date> subtaskRanges = Lists.newArrayList();
  List<DependencyEdge> incoming = node.getIncoming();
  GPLogger.debug(logger, ".. #incoming edges=%d", incoming.size());
  for (DependencyEdge edge : incoming) {
    if (!edge.refresh()) {
      continue;
    }
    if (edge instanceof ImplicitSubSuperTaskDependency) {
      subtaskRanges.add(edge.getStartRange().upperEndpoint());
      subtaskRanges.add(edge.getEndRange().lowerEndpoint());
    } else {
      if (edge.isWeak()) {
        weakStartRange = weakStartRange.intersection(edge.getStartRange());
        weakEndRange = weakEndRange.intersection(edge.getEndRange());
      } else {
        startRange = startRange.intersection(edge.getStartRange());
        endRange = endRange.intersection(edge.getEndRange());
      }
    }
    if (startRange.isEmpty() || endRange.isEmpty()) {
      GPLogger.logToLogger("both start and end ranges were calculated as empty for task=" + node.getTask() + ". Skipping it");
    }
  }
  GPLogger.debug(logger, "..Ranges: start=%s end=%s weakStart=%s weakEnd=%s", startRange, endRange, weakStartRange, weakEndRange);

  Range<Date> subtasksSpan = subtaskRanges.isEmpty() ?
      Range.closed(node.getTask().getStart().getTime(), node.getTask().getEnd().getTime()) : Range.encloseAll(subtaskRanges);
  Range<Date> subtreeStartUpwards = subtasksSpan.span(Range.downTo(node.getTask().getStart().getTime(), BoundType.CLOSED));
  Range<Date> subtreeEndDownwards = subtasksSpan.span(Range.upTo(node.getTask().getEnd().getTime(), BoundType.CLOSED));
  GPLogger.debug(logger, "..Subtasks span=%s", subtasksSpan);

  if (!startRange.equals(Range.all())) {
    startRange = startRange.intersection(weakStartRange);
  } else if (!weakStartRange.equals(Range.all())) {
    startRange = weakStartRange.intersection(subtreeStartUpwards);
  }
  if (!endRange.equals(Range.all())) {
    endRange = endRange.intersection(weakEndRange);
  } else if (!weakEndRange.equals(Range.all())) {
    endRange = weakEndRange.intersection(subtreeEndDownwards);
  }
  if (node.getTask().getThirdDateConstraint() == TaskImpl.EARLIESTBEGIN && node.getTask().getThird() != null) {
    startRange = startRange.intersection(Range.downTo(node.getTask().getThird().getTime(), BoundType.CLOSED));
    GPLogger.debug(logger, ".. applying earliest start=%s. Now start range=%s", node.getTask().getThird(), startRange);
  }
  if (!subtaskRanges.isEmpty()) {
    startRange = startRange.intersection(subtasksSpan);
    endRange = endRange.intersection(subtasksSpan);
  }
  GPLogger.debug(logger, ".. finally, start range=%s", startRange);
  if (startRange.hasLowerBound()) {
    modifyTaskStart(node.getTask(), startRange.lowerEndpoint());
  }
  if (endRange.hasUpperBound()) {
    GPCalendarCalc cal = node.getTask().getManager().getCalendar();
    Date endDate = endRange.upperEndpoint();
    TimeUnit timeUnit = node.getTask().getDuration().getTimeUnit();
    if (DayMask.WORKING == (cal.getDayMask(endDate) & DayMask.WORKING)) {
      // in case if calculated end date falls on first day after holidays (say, on Monday)
      // we'll want to modify it a little bit, so that it falls on that holidays start
      // If we don't do this, it will be done automatically the next time task activities are recalculated,
      // and thus task end date will keep changing
      Date closestWorkingEndDate = cal.findClosest(
          endDate, timeUnit, GPCalendarCalc.MoveDirection.BACKWARD, GPCalendar.DayType.WORKING);
      Date closestNonWorkingEndDate = cal.findClosest(
          endDate, timeUnit, GPCalendarCalc.MoveDirection.BACKWARD, GPCalendar.DayType.NON_WORKING, closestWorkingEndDate);
      // If there is a non-working date between current task end and closest working date
      // then we're really just after holidays
      if (closestNonWorkingEndDate != null && closestWorkingEndDate.before(closestNonWorkingEndDate)) {
        // we need to adjust-right closest working date to position to the very beginning of the holidays interval
        Date nonWorkingPeriodStart = timeUnit.adjustRight(closestWorkingEndDate);
        if (nonWorkingPeriodStart.after(node.getTask().getStart().getTime())) {
          endDate = nonWorkingPeriodStart;
        }
      }
    }
    modifyTaskEnd(node.getTask(), endDate);
  }
}
 
Example 6
Source File: RexSimplify.java    From calcite with Apache License 2.0 4 votes vote down vote up
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;
  }
}