List of usage examples for com.google.common.collect Range hasLowerBound
public boolean hasLowerBound()
From source file:com.tinspx.util.base.NumberUtils.java
@SuppressWarnings("rawtypes") public static <F extends Comparable, T extends Comparable> Range<T> transform(@NonNull Range<F> range, @NonNull Function<? super F, ? extends T> function) { if (range.hasLowerBound()) { if (range.hasUpperBound()) { return Range.range(function.apply(range.lowerEndpoint()), range.lowerBoundType(), function.apply(range.upperEndpoint()), range.upperBoundType()); } else {/*from w w w.j a v a2s .c o m*/ return Range.downTo(function.apply(range.lowerEndpoint()), range.lowerBoundType()); } } else if (range.hasUpperBound()) { return Range.upTo(function.apply(range.upperEndpoint()), range.upperBoundType()); } else { return Range.all(); } }
From source file:org.apache.kylin.common.util.RangeUtil.java
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 {/*from w w w.j av a 2 s .c o m*/ 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(); }
From source file:org.nmdp.ngs.range.rtree.RangeGeometries.java
/** * Create and return a new rectangle geometry from the specified range. * * @param range range, must not be null, must not be empty, and must have lower and upper bounds * @return a new rectangle geometry from the specified range *//*from ww w . j a va 2s. c o m*/ public static <N extends Number & Comparable<? super N>> Rectangle range(final Range<N> range) { checkNotNull(range); if (range.isEmpty()) { throw new IllegalArgumentException("range must not be empty"); } if (!range.hasLowerBound() || !range.hasUpperBound()) { throw new IllegalArgumentException("range must have lower and upper bounds"); } Number lowerEndpoint = range.lowerEndpoint(); BoundType lowerBoundType = range.lowerBoundType(); Number upperEndpoint = range.upperEndpoint(); BoundType upperBoundType = range.upperBoundType(); /* Since we are representing genomic coordinate systems, the expectation is that endpoints are instance of Integer, Long, or BigInteger; thus for open lower and upper bounds we can safely add or substract 1.0 respectively. Then by convention a rectangle with y1 0.0 and height of 1.0 is used. closed(10, 20) --> (10.0, 0.0, 20.0, 1.0) closedOpen(10, 20) --> (10.0, 0.0, 19.0, 1.0) openClosed(10, 20) --> (11.0, 0.0, 20.0, 1.0) open(10, 20) --> (11.0, 0.0, 19.0, 1.0); closed(10, 11) --> (10.0, 0.0, 11.0, 1.0) closedOpen(10, 11) --> (10.0, 0.0, 10.0, 1.0) openClosed(10, 11) --> (11.0, 0.0, 11.0, 1.0) open(10, 11) --> empty, throw exception closed(10, 10) --> (10.0, 0.0, 10.0, 1.0) closedOpen(10, 10) --> empty, throw exception openClosed(10, 10) --> empty, throw exception open(10, 10) --> empty, throw exception */ double x1 = lowerBoundType == BoundType.OPEN ? lowerEndpoint.doubleValue() + 1.0d : lowerEndpoint.doubleValue(); double y1 = 0.0d; double x2 = upperBoundType == BoundType.OPEN ? upperEndpoint.doubleValue() - 1.0d : upperEndpoint.doubleValue(); double y2 = 1.0d; return Geometries.rectangle(x1, y1, x2, y2); }
From source file:org.apache.kylin.metadata.realization.SQLDigestUtil.java
private static TupleFilter createFilterForRealtime(TupleFilter originFilter, TblColRef partitionColRef, Range<Long> tsRange) { DataType type = partitionColRef.getColumnDesc().getType(); String startTimeStr, endTimeStr; CompareTupleFilter startFilter = null, endFilter = null; if (tsRange.hasLowerBound()) { startTimeStr = formatTimeStr(type, tsRange.lowerEndpoint()); if (tsRange.lowerBoundType() == BoundType.CLOSED) { startFilter = new CompareTupleFilter(TupleFilter.FilterOperatorEnum.GTE); } else {/*w w w . ja v a 2s . c om*/ startFilter = new CompareTupleFilter(TupleFilter.FilterOperatorEnum.GT); } ColumnTupleFilter columnTupleFilter = new ColumnTupleFilter(partitionColRef); ConstantTupleFilter constantTupleFilter = new ConstantTupleFilter(startTimeStr); startFilter.addChild(columnTupleFilter); startFilter.addChild(constantTupleFilter); } if (tsRange.hasUpperBound()) { endTimeStr = formatTimeStr(type, tsRange.upperEndpoint()); if (tsRange.upperBoundType() == BoundType.CLOSED) { endFilter = new CompareTupleFilter(TupleFilter.FilterOperatorEnum.LTE); } else { endFilter = new CompareTupleFilter(TupleFilter.FilterOperatorEnum.LT); } ColumnTupleFilter columnTupleFilter = new ColumnTupleFilter(partitionColRef); ConstantTupleFilter constantTupleFilter = new ConstantTupleFilter(endTimeStr); endFilter.addChild(columnTupleFilter); endFilter.addChild(constantTupleFilter); } if (originFilter == null) { if (endFilter == null) { return startFilter; } if (startFilter == null) { return endFilter; } } LogicalTupleFilter logicalTupleFilter = new LogicalTupleFilter(TupleFilter.FilterOperatorEnum.AND); if (originFilter != null) { logicalTupleFilter.addChild(originFilter); } if (startFilter != null) { logicalTupleFilter.addChild(startFilter); } if (endFilter != null) { logicalTupleFilter.addChild(endFilter); } return logicalTupleFilter; }
From source file:org.dishevelled.bio.range.rtree.RangeGeometries.java
/** * Create and return a new rectangle geometry from the specified range. * * @param <N> value type// ww w. ja va2 s.c om * @param range range, must not be null, must not be empty, and must have lower and upper bounds * @return a new rectangle geometry from the specified range */ public static <N extends Number & Comparable<? super N>> Rectangle range(final Range<N> range) { checkNotNull(range); if (range.isEmpty()) { throw new IllegalArgumentException("range must not be empty"); } if (!range.hasLowerBound() || !range.hasUpperBound()) { throw new IllegalArgumentException("range must have lower and upper bounds"); } Number lowerEndpoint = range.lowerEndpoint(); BoundType lowerBoundType = range.lowerBoundType(); Number upperEndpoint = range.upperEndpoint(); BoundType upperBoundType = range.upperBoundType(); /* Since we are representing genomic coordinate systems, the expectation is that endpoints are instance of Integer, Long, or BigInteger; thus for open lower and upper bounds we can safely add or subtract 1.0 respectively. Then by convention a rectangle with y1 0.0 and height of 1.0 is used. closed(10, 20) --> (10.0, 0.0, 20.0, 1.0) closedOpen(10, 20) --> (10.0, 0.0, 19.0, 1.0) openClosed(10, 20) --> (11.0, 0.0, 20.0, 1.0) open(10, 20) --> (11.0, 0.0, 19.0, 1.0); closed(10, 11) --> (10.0, 0.0, 11.0, 1.0) closedOpen(10, 11) --> (10.0, 0.0, 10.0, 1.0) openClosed(10, 11) --> (11.0, 0.0, 11.0, 1.0) open(10, 11) --> empty, throw exception closed(10, 10) --> (10.0, 0.0, 10.0, 1.0) closedOpen(10, 10) --> empty, throw exception openClosed(10, 10) --> empty, throw exception open(10, 10) --> empty, throw exception */ double x1 = lowerBoundType == BoundType.OPEN ? lowerEndpoint.doubleValue() + 1.0d : lowerEndpoint.doubleValue(); double y1 = 0.0d; double x2 = upperBoundType == BoundType.OPEN ? upperEndpoint.doubleValue() - 1.0d : upperEndpoint.doubleValue(); double y2 = 1.0d; return Geometries.rectangle(x1, y1, x2, y2); }
From source file:ec.util.grid.swing.ext.TableGridCommand.java
private static Table<?> copy2(GridModel model, Range<Integer> r, Range<Integer> c, boolean rowHeader, boolean columnHeader) { if (model.getRowCount() == 0 || model.getColumnCount() == 0) { return new Table<>(0, 0); }// w ww. j a v a 2 s. co m int firstRow = r.hasLowerBound() ? (r.lowerBoundType().equals(BoundType.CLOSED) ? r.lowerEndpoint() : (r.lowerEndpoint() + 1)) : 0; int lastRow = r.hasUpperBound() ? (r.upperBoundType().equals(BoundType.CLOSED) ? r.upperEndpoint() : (r.upperEndpoint() - 1)) : (model.getRowCount() - 1); int firstColumn = c.hasLowerBound() ? (c.lowerBoundType().equals(BoundType.CLOSED) ? c.lowerEndpoint() : (c.lowerEndpoint() + 1)) : 0; int lastColumn = c.hasUpperBound() ? (c.upperBoundType().equals(BoundType.CLOSED) ? c.upperEndpoint() : (c.upperEndpoint() - 1)) : (model.getColumnCount() - 1); return copy(model, firstRow, firstColumn, lastRow, lastColumn, rowHeader, columnHeader); }
From source file:org.apache.druid.sql.calcite.filtration.CombineAndSimplifyBounds.java
/** * Simplify BoundDimFilters that are children of an OR or an AND. * * @param children the filters//from ww w . ja v a 2s .c om * @param disjunction true for disjunction, false for conjunction * * @return simplified filters */ private static DimFilter doSimplify(final List<DimFilter> children, boolean disjunction) { // Copy children list final List<DimFilter> newChildren = Lists.newArrayList(children); // Group Bound filters by dimension, extractionFn, and comparator and compute a RangeSet for each one. final Map<BoundRefKey, List<BoundDimFilter>> bounds = Maps.newHashMap(); final Iterator<DimFilter> iterator = newChildren.iterator(); while (iterator.hasNext()) { final DimFilter child = iterator.next(); if (child.equals(Filtration.matchNothing())) { // Child matches nothing, equivalent to FALSE // OR with FALSE => ignore // AND with FALSE => always false, short circuit if (disjunction) { iterator.remove(); } else { return Filtration.matchNothing(); } } else if (child.equals(Filtration.matchEverything())) { // Child matches everything, equivalent to TRUE // OR with TRUE => always true, short circuit // AND with TRUE => ignore if (disjunction) { return Filtration.matchEverything(); } else { iterator.remove(); } } else if (child instanceof BoundDimFilter) { final BoundDimFilter bound = (BoundDimFilter) child; final BoundRefKey boundRefKey = BoundRefKey.from(bound); List<BoundDimFilter> filterList = bounds.get(boundRefKey); if (filterList == null) { filterList = Lists.newArrayList(); bounds.put(boundRefKey, filterList); } filterList.add(bound); } } // Try to simplify filters within each group. for (Map.Entry<BoundRefKey, List<BoundDimFilter>> entry : bounds.entrySet()) { final BoundRefKey boundRefKey = entry.getKey(); final List<BoundDimFilter> filterList = entry.getValue(); // Create a RangeSet for this group. final RangeSet<BoundValue> rangeSet = disjunction ? RangeSets.unionRanges(Bounds.toRanges(filterList)) : RangeSets.intersectRanges(Bounds.toRanges(filterList)); if (rangeSet.asRanges().size() < filterList.size()) { // We found a simplification. Remove the old filters and add new ones. for (final BoundDimFilter bound : filterList) { if (!newChildren.remove(bound)) { throw new ISE("WTF?! Tried to remove bound but couldn't?"); } } if (rangeSet.asRanges().isEmpty()) { // range set matches nothing, equivalent to FALSE // OR with FALSE => ignore // AND with FALSE => always false, short circuit if (disjunction) { newChildren.add(Filtration.matchNothing()); } else { return Filtration.matchNothing(); } } for (final Range<BoundValue> range : rangeSet.asRanges()) { if (!range.hasLowerBound() && !range.hasUpperBound()) { // range matches all, equivalent to TRUE // AND with TRUE => ignore // OR with TRUE => always true; short circuit if (disjunction) { return Filtration.matchEverything(); } else { newChildren.add(Filtration.matchEverything()); } } else { newChildren.add(Bounds.toFilter(boundRefKey, range)); } } } } Preconditions.checkState(newChildren.size() > 0, "newChildren.size > 0"); if (newChildren.size() == 1) { return newChildren.get(0); } else { return disjunction ? new OrDimFilter(newChildren) : new AndDimFilter(newChildren); } }
From source file:org.dishevelled.variation.range.Ranges.java
/** * Return the center of the specified range. * * @param <C> range endpoint type/*from w w w. j av a 2s.com*/ * @param range range, must not be null * @return the center of the specified range */ public static <C extends Comparable> C center(final Range<C> range) { checkNotNull(range); if (!range.hasUpperBound() && !range.hasUpperBound()) { throw new IllegalStateException("cannot find the center of a range without bounds"); } if (!range.hasLowerBound()) { return range.upperEndpoint(); } if (!range.hasUpperBound()) { return range.lowerEndpoint(); } C lowerEndpoint = range.lowerEndpoint(); C upperEndpoint = range.upperEndpoint(); if (upperEndpoint instanceof Integer) { Integer upper = (Integer) upperEndpoint; Integer lower = (Integer) lowerEndpoint; return (C) Integer.valueOf((upper.intValue() + lower.intValue()) / 2); } if (upperEndpoint instanceof Long) { Long upper = (Long) upperEndpoint; Long lower = (Long) lowerEndpoint; return (C) Long.valueOf((upper.longValue() + lower.longValue()) / 2L); } if (upperEndpoint instanceof BigInteger) { BigInteger upper = (BigInteger) upperEndpoint; BigInteger lower = (BigInteger) lowerEndpoint; BigInteger two = BigInteger.valueOf(2L); return (C) upper.subtract(lower).divide(two); } // todo: could potentially calculate the center of any range with a discrete domain throw new IllegalStateException( "cannot find the center of a range whose endpoint type is not Integer, Long, or BigInteger"); }
From source file:org.apache.kylin.common.util.RangeUtil.java
/** * for NavigableMap sorted by C, given a range of C, return the sub map whose key falls in the range *///w w w .ja v a 2s . co m 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)); } }
From source file:org.apache.hadoop.hive.ql.optimizer.calcite.druid.DruidIntervalUtils.java
protected static List<Interval> toInterval(List<Range> ranges) { List<Interval> intervals = Lists.transform(ranges, new Function<Range, Interval>() { @Override// w w w.ja v a 2 s.c o m public Interval apply(Range range) { if (!range.hasLowerBound() && !range.hasUpperBound()) { return DruidTable.DEFAULT_INTERVAL; } long start = range.hasLowerBound() ? toLong(range.lowerEndpoint()) : DruidTable.DEFAULT_INTERVAL.getStartMillis(); long end = range.hasUpperBound() ? toLong(range.upperEndpoint()) : DruidTable.DEFAULT_INTERVAL.getEndMillis(); if (range.hasLowerBound() && range.lowerBoundType() == BoundType.OPEN) { start++; } if (range.hasUpperBound() && range.upperBoundType() == BoundType.CLOSED) { end++; } return new Interval(start, end); } }); LOG.info("Converted time ranges " + ranges + " to interval " + intervals); return intervals; }