Example usage for com.google.common.collect BoundType CLOSED

List of usage examples for com.google.common.collect BoundType CLOSED

Introduction

In this page you can find the example usage for com.google.common.collect BoundType CLOSED.

Prototype

BoundType CLOSED

To view the source code for com.google.common.collect BoundType CLOSED.

Click Source Link

Document

The endpoint value is considered part of the set ("inclusive").

Usage

From source file:com.wealdtech.collect.TreeRangedMap.java

/**
 * Validate a range prior to insertion//w  ww . j  a  v  a 2s. c o  m
 * @param range the range to validate
 */
private void validateRange(final Range<K> range) {
    if (!range.hasLowerBound()) {
        throw new IllegalArgumentException("RangedMap only supports ranges with defined lower bound");
    }
    if (!range.lowerBoundType().equals(BoundType.CLOSED)) {
        throw new IllegalArgumentException("RangedMap must use ranges with closed lower bound");
    }
    if (!range.hasUpperBound()) {
        throw new IllegalArgumentException("RangedMap must use ranges with defined upper bound");
    }
    if (!range.upperBoundType().equals(BoundType.OPEN)) {
        throw new IllegalArgumentException("RangedMap must use ranges with open upper bound");
    }
    if (range.isEmpty()) {
        throw new IllegalArgumentException("RangedMap must use ranges with non-zero size");
    }
}

From source file:com.yahoo.pulsar.broker.namespace.ServiceUnitZkUtils.java

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);
}

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);
    }//from  w ww . ja  v  a  2s .c o  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.calcite.adapter.druid.DruidDateTimeUtils.java

protected static List<LocalInterval> toInterval(List<Range<Calendar>> ranges) {
    List<LocalInterval> intervals = Lists.transform(ranges, new Function<Range<Calendar>, LocalInterval>() {
        public LocalInterval apply(Range<Calendar> range) {
            if (!range.hasLowerBound() && !range.hasUpperBound()) {
                return DruidTable.DEFAULT_INTERVAL;
            }/*from   w  ww  .  j  a v  a 2 s  .co  m*/
            long start = range.hasLowerBound() ? range.lowerEndpoint().getTime().getTime()
                    : DruidTable.DEFAULT_INTERVAL.getStartMillis();
            long end = range.hasUpperBound() ? range.upperEndpoint().getTime().getTime()
                    : DruidTable.DEFAULT_INTERVAL.getEndMillis();
            if (range.hasLowerBound() && range.lowerBoundType() == BoundType.OPEN) {
                start++;
            }
            if (range.hasUpperBound() && range.upperBoundType() == BoundType.CLOSED) {
                end++;
            }
            return LocalInterval.create(start, end);
        }
    });
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("Converted time ranges " + ranges + " to interval " + intervals);
    }
    return intervals;
}

From source file:org.apache.bookkeeper.mledger.impl.ManagedLedgerOfflineBacklog.java

private long getNumberOfEntries(Range<PositionImpl> range,
        NavigableMap<Long, MLDataFormats.ManagedLedgerInfo.LedgerInfo> ledgers) {
    PositionImpl fromPosition = range.lowerEndpoint();
    boolean fromIncluded = range.lowerBoundType() == BoundType.CLOSED;
    PositionImpl toPosition = range.upperEndpoint();
    boolean toIncluded = range.upperBoundType() == BoundType.CLOSED;

    if (fromPosition.getLedgerId() == toPosition.getLedgerId()) {
        // If the 2 positions are in the same ledger
        long count = toPosition.getEntryId() - fromPosition.getEntryId() - 1;
        count += fromIncluded ? 1 : 0;/*  w  ww  .  j a v  a2  s. c  o m*/
        count += toIncluded ? 1 : 0;
        return count;
    } else {
        long count = 0;
        // If the from & to are pointing to different ledgers, then we need to :
        // 1. Add the entries in the ledger pointed by toPosition
        count += toPosition.getEntryId();
        count += toIncluded ? 1 : 0;

        // 2. Add the entries in the ledger pointed by fromPosition
        MLDataFormats.ManagedLedgerInfo.LedgerInfo li = ledgers.get(fromPosition.getLedgerId());
        if (li != null) {
            count += li.getEntries() - (fromPosition.getEntryId() + 1);
            count += fromIncluded ? 1 : 0;
        }

        // 3. Add the whole ledgers entries in between
        for (MLDataFormats.ManagedLedgerInfo.LedgerInfo ls : ledgers
                .subMap(fromPosition.getLedgerId(), false, toPosition.getLedgerId(), false).values()) {
            count += ls.getEntries();
        }

        return count;
    }
}

From source file:org.apache.kylin.common.util.RangeUtil.java

/**
 * remove from self the elements that exist in other
 * @return/*w w w .jav a2  s.co m*/
 */
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;

}

From source file:org.eclipse.smarthome.transform.scale.internal.ScaleTransformationService.java

@Override
protected Map<Range<Double>, String> internalLoadTransform(String filename) throws TransformationException {
    try {/*ww  w  .j  ava  2s  . c o m*/
        Properties properties = new Properties();
        properties.load(new FileReader(filename));
        Map<Range<Double>, String> data = new HashMap<>();

        for (Entry<Object, Object> f : properties.entrySet()) {
            String key = (String) f.getKey();
            String value = properties.getProperty(key);
            Matcher matcher = limits_pattern.matcher(key);
            if (matcher.matches() && (matcher.groupCount() == 4)) {

                BoundType lowBoundType = matcher.group(1).equals("]") ? BoundType.OPEN : BoundType.CLOSED;
                BoundType highBoundType = matcher.group(4).equals("[") ? BoundType.OPEN : BoundType.CLOSED;

                String lowLimit = matcher.group(2);
                String highLimit = matcher.group(3);

                Double lowValue = null;
                Double highValue = null;

                try {
                    if (!lowLimit.isEmpty()) {
                        lowValue = new Double(lowLimit);
                    }
                    if (!highLimit.isEmpty()) {
                        highValue = new Double(highLimit);
                    }
                } catch (NumberFormatException e) {
                    throw new TransformationException("Error parsing bounds : " + lowLimit + ".." + highLimit);
                }

                Range<Double> range = getRange(lowBoundType, highBoundType, lowValue, highValue);

                data.put(range, value);

            } else {
                logger.warn("Scale transform entry does not comply with syntax : '{}', '{}'", key, value);
            }
        }
        return data;
    } catch (IOException e) {
        throw new TransformationException("An error occured while opening file.", e);
    }
}

From source file:org.kitesdk.data.spi.predicates.Ranges.java

@VisibleForTesting
static <C extends Comparable<C>> com.google.common.collect.Range<C> asGuavaRange(Range<C> range) {
    if (range.hasLowerBound()) {
        if (range.hasUpperBound()) {
            return com.google.common.collect.Ranges.range(range.lowerEndpoint(),
                    range.isLowerBoundOpen() ? BoundType.OPEN : BoundType.CLOSED, range.upperEndpoint(),
                    range.isUpperBoundOpen() ? BoundType.OPEN : BoundType.CLOSED);
        } else {//from   w w  w . j  a  v a 2  s .  co m
            return com.google.common.collect.Ranges.downTo(range.lowerEndpoint(),
                    range.isLowerBoundOpen() ? BoundType.OPEN : BoundType.CLOSED);
        }
    } else if (range.hasUpperBound()) {
        return com.google.common.collect.Ranges.upTo(range.upperEndpoint(),
                range.isUpperBoundOpen() ? BoundType.OPEN : BoundType.CLOSED);
    } else {
        return com.google.common.collect.Ranges.all();
    }
}

From source file:edu.mit.streamjit.impl.compiler2.DescendingShareAllocationStrategy.java

@Override
public void allocateGroup(ActorGroup group, Range<Integer> iterations, List<Core> cores, Configuration config) {
    List<Float> shares = new ArrayList<>(cores.size());
    for (int core = 0; core < cores.size(); ++core) {
        String name = String.format("node%dcore%diter", group.id(), core);
        Configuration.FloatParameter parameter = config.getParameter(name, Configuration.FloatParameter.class);
        if (parameter == null)
            shares.add(0f);/*from w  w w. ja  va2  s  .  co m*/
        else
            shares.add(parameter.getValue());
    }

    assert iterations.lowerBoundType() == BoundType.CLOSED && iterations.upperBoundType() == BoundType.OPEN;
    int totalAvailable = iterations.upperEndpoint() - iterations.lowerEndpoint();
    while (!iterations.isEmpty()) {
        int max = CollectionUtils.maxIndex(shares);
        float share = shares.get(max);
        if (share == 0)
            break;
        int amount = DoubleMath.roundToInt(share * totalAvailable, RoundingMode.HALF_EVEN);
        int done = iterations.lowerEndpoint();
        Range<Integer> allocation = group.isStateful() ? iterations
                : iterations.intersection(Range.closedOpen(done, done + amount));
        cores.get(max).allocate(group, allocation);
        iterations = Range.closedOpen(allocation.upperEndpoint(), iterations.upperEndpoint());
        shares.set(max, 0f); //don't allocate to this core again
    }

    //If we have iterations left over not assigned to a core, spread them
    //evenly over all cores.
    if (!iterations.isEmpty()) {
        int perCore = IntMath.divide(iterations.upperEndpoint() - iterations.lowerEndpoint(), cores.size(),
                RoundingMode.CEILING);
        for (int i = 0; i < cores.size() && !iterations.isEmpty(); ++i) {
            int min = iterations.lowerEndpoint();
            Range<Integer> allocation = group.isStateful() ? iterations
                    : iterations.intersection(Range.closedOpen(min, min + perCore));
            cores.get(i).allocate(group, allocation);
            iterations = Range.closedOpen(allocation.upperEndpoint(), iterations.upperEndpoint());
        }
    }
    assert iterations.isEmpty();
}

From source file:org.apache.lens.server.api.driver.hooks.QueryCostBasedQueryHook.java

public Range<T> parseRange(String rangeStr) {
    if (rangeStr == null) {
        return null;
    }/* w  ww  .java 2 s  . com*/
    rangeStr = rangeStr.trim();
    BoundType lowerBound, upperBound;
    if (rangeStr.startsWith("[")) {
        lowerBound = BoundType.CLOSED;
    } else if (rangeStr.startsWith("(")) {
        lowerBound = BoundType.OPEN;
    } else {
        throw new IllegalArgumentException("Range should start with either ( or [");
    }
    if (rangeStr.endsWith("]")) {
        upperBound = BoundType.CLOSED;
    } else if (rangeStr.endsWith(")")) {
        upperBound = BoundType.OPEN;
    } else {
        throw new IllegalArgumentException("Range should end with either ) or ]");
    }
    String[] pair = rangeStr.substring(1, rangeStr.length() - 1).split(",");
    String leftStr = pair[0].trim();
    String rightStr = pair[1].trim();
    if (leftStr.isEmpty() && rightStr.isEmpty()) {
        return Range.all();
    } else if (leftStr.isEmpty()) {
        return Range.upTo(parser.parse(rightStr), upperBound);
    } else if (rightStr.isEmpty()) {
        return Range.downTo(parser.parse(leftStr), lowerBound);
    } else {
        return Range.range(parser.parse(leftStr), lowerBound, parser.parse(rightStr), upperBound);
    }
}