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

The following examples show how to use com.google.common.collect.Range#upperBoundType() . 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: RuntimeColorMap.java    From tcl-regex-java with Apache License 2.0 6 votes vote down vote up
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();
            }
        }
    }
}
 
Example 2
Source File: Ranges.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Return an english phrase describing the given {@link Range} e.g.
 *
 *     Range.all()                      -> "unbounded"
 *     Range.singleton(3)               -> "3"
 *     Range.atLeast(3)                 -> "at least 3"
 *     Range.closedOpen(3, 7)           -> "at least 3 and less than 7"
 *     Range.closed(3, 7)               -> "between 3 and 7"
 */
public static String describe(Range<?> range) {
    if(range.hasLowerBound() && range.hasUpperBound() && range.lowerBoundType() == BoundType.CLOSED && range.upperBoundType() == BoundType.CLOSED) {
        if(range.lowerEndpoint().equals(range.upperEndpoint())) {
            // singleton
            return range.lowerEndpoint().toString();
        } else {
            // closed-closed
            return "between " + range.lowerEndpoint() + " and " + range.upperEndpoint();
        }
    }

    final List<String> parts = new ArrayList<>(2);

    if(range.hasLowerBound()) {
        parts.add((range.lowerBoundType() == BoundType.CLOSED ? "at least " : "more than ") + range.lowerEndpoint());
    }

    if(range.hasUpperBound()) {
        parts.add((range.upperBoundType() == BoundType.CLOSED ? "at most " : "less than ") + range.upperEndpoint());
    }

    switch(parts.size()) {
        case 0: return "unbounded";
        case 1: return parts.get(0);
        default: return parts.get(0) + " and " + parts.get(1);
    }
}
 
Example 3
Source File: MongoDataPointRepositoryImpl.java    From omh-dsu-ri with Apache License 2.0 5 votes vote down vote up
void addCreationTimestampCriteria(Query query, Range<OffsetDateTime> timestampRange) {

        if (timestampRange.hasLowerBound() || timestampRange.hasUpperBound()) {

            Criteria timestampCriteria = where("header.creation_date_time");

            if (timestampRange.hasLowerBound()) {
                if (timestampRange.lowerBoundType() == CLOSED) {
                    timestampCriteria = timestampCriteria.gte(timestampRange.lowerEndpoint());
                }
                else {
                    timestampCriteria = timestampCriteria.gt(timestampRange.lowerEndpoint());
                }
            }

            if (timestampRange.hasUpperBound()) {
                if (timestampRange.upperBoundType() == CLOSED) {
                    timestampCriteria = timestampCriteria.lte(timestampRange.upperEndpoint());
                }
                else {
                    timestampCriteria = timestampCriteria.lt(timestampRange.upperEndpoint());
                }
            }

            query.addCriteria(timestampCriteria);
        }
    }
 
Example 4
Source File: ColorMap.java    From tcl-regex-java with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 5
Source File: Parameter.java    From activitystreams with Apache License 2.0 5 votes vote down vote up
public Builder bound(Range<?> range) {
  if (range != null) {
    if (range.hasLowerBound()) {
      switch(range.lowerBoundType()) {
      case CLOSED:
        minInclusive(range.lowerEndpoint());
        break;
      case OPEN:
        minExclusive(range.lowerEndpoint());
        break;
      default:
        break;
      }
    } else {
      minInclusive(null);
      minExclusive(null);
    }
    if (range.hasUpperBound()) {
      switch(range.upperBoundType()) {
      case CLOSED:
        maxInclusive(range.upperEndpoint());
        break;
      case OPEN:
        maxExclusive(range.upperEndpoint());
        break;
      default:
        break;
      }
    } else {
      maxInclusive(null);
      maxExclusive(null);
    }
  }
  return this;
}
 
Example 6
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 7
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 8
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 9
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 10
Source File: RangeUtil.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static String formatTsRange(Range<Long> tsRange) {
    if (tsRange == null)
        return null;

    StringBuilder sb = new StringBuilder();
    if (tsRange.hasLowerBound()) {
        if (tsRange.lowerBoundType() == BoundType.CLOSED) {
            sb.append("[");
        } else {
            sb.append("(");
        }
        sb.append(DateFormat.formatToTimeStr(tsRange.lowerEndpoint()));
    } else {
        sb.append("(-∞");
    }

    sb.append("~");

    if (tsRange.hasUpperBound()) {
        sb.append(DateFormat.formatToTimeStr(tsRange.upperEndpoint()));
        if (tsRange.upperBoundType() == BoundType.CLOSED) {
            sb.append("]");
        } else {
            sb.append(")");
        }
    } else {
        sb.append("+∞)");
    }
    return sb.toString();
}
 
Example 11
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 12
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 13
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 14
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;
  }
}
 
Example 15
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 16
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 17
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;
  }
}