Example usage for com.google.common.collect Range hasLowerBound

List of usage examples for com.google.common.collect Range hasLowerBound

Introduction

In this page you can find the example usage for com.google.common.collect Range hasLowerBound.

Prototype

public boolean hasLowerBound() 

Source Link

Document

Returns true if this range has a lower endpoint.

Usage

From source file:org.pshdl.model.extensions.RangeExtension.java

protected Optional<Range<BigInteger>> _determineRange(final HDLShiftOp obj,
        final HDLEvaluationContext context) {
    HDLExpression _left = obj.getLeft();
    final Optional<Range<BigInteger>> leftRange = this.determineRange(_left, context);
    boolean _isPresent = leftRange.isPresent();
    boolean _not = (!_isPresent);
    if (_not) {/*from ww w .j  ava2s . c om*/
        return Optional.<Range<BigInteger>>absent();
    }
    final Range<BigInteger> lrVal = leftRange.get();
    boolean _or = false;
    boolean _hasLowerBound = lrVal.hasLowerBound();
    boolean _not_1 = (!_hasLowerBound);
    if (_not_1) {
        _or = true;
    } else {
        boolean _hasUpperBound = lrVal.hasUpperBound();
        boolean _not_2 = (!_hasUpperBound);
        _or = _not_2;
    }
    if (_or) {
        return Optional.<Range<BigInteger>>absent();
    }
    HDLExpression _right = obj.getRight();
    final Optional<Range<BigInteger>> rightRange = this.determineRange(_right, context);
    boolean _isPresent_1 = rightRange.isPresent();
    boolean _not_3 = (!_isPresent_1);
    if (_not_3) {
        return Optional.<Range<BigInteger>>absent();
    }
    final Range<BigInteger> rrVal = rightRange.get();
    boolean _or_1 = false;
    boolean _hasLowerBound_1 = rrVal.hasLowerBound();
    boolean _not_4 = (!_hasLowerBound_1);
    if (_not_4) {
        _or_1 = true;
    } else {
        boolean _hasUpperBound_1 = rrVal.hasUpperBound();
        boolean _not_5 = (!_hasUpperBound_1);
        _or_1 = _not_5;
    }
    if (_or_1) {
        return Optional.<Range<BigInteger>>absent();
    }
    HDLShiftOp.HDLShiftOpType _type = obj.getType();
    if (_type != null) {
        switch (_type) {
        case SLL:
            BigInteger _lowerEndpoint = lrVal.lowerEndpoint();
            BigInteger _lowerEndpoint_1 = rrVal.lowerEndpoint();
            int _intValue = _lowerEndpoint_1.intValue();
            final BigInteger ff = _lowerEndpoint.shiftLeft(_intValue);
            BigInteger _lowerEndpoint_2 = lrVal.lowerEndpoint();
            BigInteger _upperEndpoint = rrVal.upperEndpoint();
            int _intValue_1 = _upperEndpoint.intValue();
            final BigInteger ft = _lowerEndpoint_2.shiftLeft(_intValue_1);
            BigInteger _upperEndpoint_1 = lrVal.upperEndpoint();
            BigInteger _lowerEndpoint_3 = rrVal.lowerEndpoint();
            int _intValue_2 = _lowerEndpoint_3.intValue();
            final BigInteger tf = _upperEndpoint_1.shiftLeft(_intValue_2);
            BigInteger _upperEndpoint_2 = lrVal.upperEndpoint();
            BigInteger _upperEndpoint_3 = rrVal.upperEndpoint();
            int _intValue_3 = _upperEndpoint_3.intValue();
            final BigInteger tt = _upperEndpoint_2.shiftLeft(_intValue_3);
            BigInteger _min = ff.min(ft);
            BigInteger _min_1 = _min.min(tf);
            BigInteger _min_2 = _min_1.min(tt);
            BigInteger _max = ff.max(ft);
            BigInteger _max_1 = _max.max(tf);
            BigInteger _max_2 = _max_1.max(tt);
            Range<BigInteger> _createRange = RangeTool.<BigInteger>createRange(_min_2, _max_2);
            return Optional.<Range<BigInteger>>of(_createRange);
        case SRA:
            BigInteger _lowerEndpoint_4 = lrVal.lowerEndpoint();
            BigInteger _lowerEndpoint_5 = rrVal.lowerEndpoint();
            int _intValue_4 = _lowerEndpoint_5.intValue();
            final BigInteger ff_1 = _lowerEndpoint_4.shiftRight(_intValue_4);
            BigInteger _lowerEndpoint_6 = lrVal.lowerEndpoint();
            BigInteger _upperEndpoint_4 = rrVal.upperEndpoint();
            int _intValue_5 = _upperEndpoint_4.intValue();
            final BigInteger ft_1 = _lowerEndpoint_6.shiftRight(_intValue_5);
            BigInteger _upperEndpoint_5 = lrVal.upperEndpoint();
            BigInteger _lowerEndpoint_7 = rrVal.lowerEndpoint();
            int _intValue_6 = _lowerEndpoint_7.intValue();
            final BigInteger tf_1 = _upperEndpoint_5.shiftRight(_intValue_6);
            BigInteger _upperEndpoint_6 = lrVal.upperEndpoint();
            BigInteger _upperEndpoint_7 = rrVal.upperEndpoint();
            int _intValue_7 = _upperEndpoint_7.intValue();
            final BigInteger tt_1 = _upperEndpoint_6.shiftRight(_intValue_7);
            BigInteger _min_3 = ff_1.min(ft_1);
            BigInteger _min_4 = _min_3.min(tf_1);
            BigInteger _min_5 = _min_4.min(tt_1);
            BigInteger _max_3 = ff_1.max(ft_1);
            BigInteger _max_4 = _max_3.max(tf_1);
            BigInteger _max_5 = _max_4.max(tt_1);
            Range<BigInteger> _createRange_1 = RangeTool.<BigInteger>createRange(_min_5, _max_5);
            return Optional.<Range<BigInteger>>of(_createRange_1);
        case SRL:
            BigInteger _lowerEndpoint_8 = lrVal.lowerEndpoint();
            BigInteger _lowerEndpoint_9 = rrVal.lowerEndpoint();
            final BigInteger ff_2 = RangeExtension.srl(_lowerEndpoint_8, _lowerEndpoint_9);
            BigInteger _lowerEndpoint_10 = lrVal.lowerEndpoint();
            BigInteger _upperEndpoint_8 = rrVal.upperEndpoint();
            final BigInteger ft_2 = RangeExtension.srl(_lowerEndpoint_10, _upperEndpoint_8);
            BigInteger _upperEndpoint_9 = lrVal.upperEndpoint();
            BigInteger _lowerEndpoint_11 = rrVal.lowerEndpoint();
            final BigInteger tf_2 = RangeExtension.srl(_upperEndpoint_9, _lowerEndpoint_11);
            BigInteger _upperEndpoint_10 = lrVal.upperEndpoint();
            BigInteger _upperEndpoint_11 = rrVal.upperEndpoint();
            final BigInteger tt_2 = RangeExtension.srl(_upperEndpoint_10, _upperEndpoint_11);
            BigInteger _min_6 = ff_2.min(ft_2);
            BigInteger _min_7 = _min_6.min(tf_2);
            BigInteger _min_8 = _min_7.min(tt_2);
            BigInteger _max_6 = ff_2.max(ft_2);
            BigInteger _max_7 = _max_6.max(tf_2);
            BigInteger _max_8 = _max_7.max(tt_2);
            Range<BigInteger> _createRange_2 = RangeTool.<BigInteger>createRange(_min_8, _max_8);
            return Optional.<Range<BigInteger>>of(_createRange_2);
        default:
            break;
        }
    }
    throw new RuntimeException("Incorrectly implemented obj op");
}

From source file:org.kiji.schema.impl.cassandra.CQLStatementCache.java

/**
 * Create a CQL statement for selecting the columns that make up the Entity ID from a Cassandra
 * Kiji Table./*from w ww  .  j  a  v  a2 s .com*/
 *
 * @param table The translated Cassandra table name.
 * @param tokenRange A range of tokens to scan.
 * @return a statement that will get the single column.
 */
public Statement createEntityIDScanStatement(final CassandraTableName table, final Range<Long> tokenRange) {

    // Retrieve the prepared statement from the cache

    // We can only use the 'DISTINCT' optimization if all entity ID components are part of the
    // partition key. CQL does not allow DISTINCT over non partition-key columns.
    final boolean useDistinct = mEntityIDClusteringColumns.isEmpty();

    final Optional<BoundType> lowerBound = tokenRange.hasLowerBound() ? Optional.of(tokenRange.lowerBoundType())
            : Optional.<BoundType>absent();

    final Optional<BoundType> upperBound = tokenRange.hasUpperBound() ? Optional.of(tokenRange.upperBoundType())
            : Optional.<BoundType>absent();

    final EntityIDScanStatementKey key = new EntityIDScanStatementKey(table, lowerBound, upperBound,
            useDistinct);
    final PreparedStatement statement = mCache.getUnchecked(key);

    // Bind the parameters to the prepared statement

    // slots are for the min/max token
    final List<Object> values = Lists.newArrayListWithCapacity(2);

    if (tokenRange.hasLowerBound()) {
        values.add(tokenRange.lowerEndpoint());
    }

    if (tokenRange.hasUpperBound()) {
        values.add(tokenRange.upperEndpoint());
    }

    return statement.bind(values.toArray()).setFetchSize(ENTITY_ID_BATCH_SIZE);
}

From source file:org.pshdl.model.extensions.RangeExtension.java

protected Optional<Range<BigInteger>> _determineRange(final HDLArithOp obj,
        final HDLEvaluationContext context) {
    HDLExpression _left = obj.getLeft();
    final Optional<Range<BigInteger>> leftRange = this.determineRange(_left, context);
    boolean _isPresent = leftRange.isPresent();
    boolean _not = (!_isPresent);
    if (_not) {//from w ww  . j  a  va2s .  c  o m
        return Optional.<Range<BigInteger>>absent();
    }
    final Range<BigInteger> lrVal = leftRange.get();
    boolean _or = false;
    boolean _hasLowerBound = lrVal.hasLowerBound();
    boolean _not_1 = (!_hasLowerBound);
    if (_not_1) {
        _or = true;
    } else {
        boolean _hasUpperBound = lrVal.hasUpperBound();
        boolean _not_2 = (!_hasUpperBound);
        _or = _not_2;
    }
    if (_or) {
        return Optional.<Range<BigInteger>>absent();
    }
    HDLExpression _right = obj.getRight();
    final Optional<Range<BigInteger>> rightRange = this.determineRange(_right, context);
    boolean _isPresent_1 = rightRange.isPresent();
    boolean _not_3 = (!_isPresent_1);
    if (_not_3) {
        return Optional.<Range<BigInteger>>absent();
    }
    final Range<BigInteger> rrVal = rightRange.get();
    boolean _or_1 = false;
    boolean _hasLowerBound_1 = rrVal.hasLowerBound();
    boolean _not_4 = (!_hasLowerBound_1);
    if (_not_4) {
        _or_1 = true;
    } else {
        boolean _hasUpperBound_1 = rrVal.hasUpperBound();
        boolean _not_5 = (!_hasUpperBound_1);
        _or_1 = _not_5;
    }
    if (_or_1) {
        return Optional.<Range<BigInteger>>absent();
    }
    HDLArithOp.HDLArithOpType _type = obj.getType();
    if (_type != null) {
        switch (_type) {
        case PLUS:
            BigInteger _lowerEndpoint = lrVal.lowerEndpoint();
            BigInteger _lowerEndpoint_1 = rrVal.lowerEndpoint();
            BigInteger _add = _lowerEndpoint.add(_lowerEndpoint_1);
            BigInteger _upperEndpoint = lrVal.upperEndpoint();
            BigInteger _upperEndpoint_1 = rrVal.upperEndpoint();
            BigInteger _add_1 = _upperEndpoint.add(_upperEndpoint_1);
            Range<BigInteger> _createRange = RangeTool.<BigInteger>createRange(_add, _add_1);
            return Optional.<Range<BigInteger>>of(_createRange);
        case MINUS:
            BigInteger _lowerEndpoint_2 = lrVal.lowerEndpoint();
            BigInteger _lowerEndpoint_3 = rrVal.lowerEndpoint();
            BigInteger _subtract = _lowerEndpoint_2.subtract(_lowerEndpoint_3);
            BigInteger _upperEndpoint_2 = lrVal.upperEndpoint();
            BigInteger _upperEndpoint_3 = rrVal.upperEndpoint();
            BigInteger _subtract_1 = _upperEndpoint_2.subtract(_upperEndpoint_3);
            Range<BigInteger> _createRange_1 = RangeTool.<BigInteger>createRange(_subtract, _subtract_1);
            return Optional.<Range<BigInteger>>of(_createRange_1);
        case DIV:
            boolean _or_2 = false;
            BigInteger _lowerEndpoint_4 = rrVal.lowerEndpoint();
            boolean _equals = _lowerEndpoint_4.equals(BigInteger.ZERO);
            if (_equals) {
                _or_2 = true;
            } else {
                BigInteger _upperEndpoint_4 = rrVal.upperEndpoint();
                boolean _equals_1 = _upperEndpoint_4.equals(BigInteger.ZERO);
                _or_2 = _equals_1;
            }
            if (_or_2) {
                obj.<IHDLObject>addMeta(ProblemDescription.SOURCE, obj);
                obj.<ProblemDescription>addMeta(ProblemDescription.DESCRIPTION, ProblemDescription.ZERO_DIVIDE);
                return Optional.<Range<BigInteger>>absent();
            }
            boolean _or_3 = false;
            BigInteger _lowerEndpoint_5 = rrVal.lowerEndpoint();
            int _signum = _lowerEndpoint_5.signum();
            BigInteger _upperEndpoint_5 = rrVal.upperEndpoint();
            int _signum_1 = _upperEndpoint_5.signum();
            int _multiply = (_signum * _signum_1);
            boolean _lessThan = (_multiply < 0);
            if (_lessThan) {
                _or_3 = true;
            } else {
                BigInteger _upperEndpoint_6 = rrVal.upperEndpoint();
                int _signum_2 = _upperEndpoint_6.signum();
                boolean _equals_2 = (_signum_2 == 0);
                _or_3 = _equals_2;
            }
            if (_or_3) {
                obj.<IHDLObject>addMeta(ProblemDescription.SOURCE, obj);
                obj.<ProblemDescription>addMeta(ProblemDescription.DESCRIPTION,
                        ProblemDescription.POSSIBLY_ZERO_DIVIDE);
            }
            BigInteger _lowerEndpoint_6 = rrVal.lowerEndpoint();
            BigDecimal _bigDecimal = new BigDecimal(_lowerEndpoint_6);
            BigDecimal _divide = BigDecimal.ONE.divide(_bigDecimal);
            BigInteger _upperEndpoint_7 = rrVal.upperEndpoint();
            BigDecimal _bigDecimal_1 = new BigDecimal(_upperEndpoint_7);
            BigDecimal _divide_1 = BigDecimal.ONE.divide(_bigDecimal_1);
            final Range<BigDecimal> mulRange = RangeTool.<BigDecimal>createRange(_divide, _divide_1);
            BigInteger _lowerEndpoint_7 = lrVal.lowerEndpoint();
            BigDecimal _bigDecimal_2 = new BigDecimal(_lowerEndpoint_7);
            BigDecimal _lowerEndpoint_8 = mulRange.lowerEndpoint();
            final BigDecimal ff = _bigDecimal_2.multiply(_lowerEndpoint_8);
            BigInteger _lowerEndpoint_9 = lrVal.lowerEndpoint();
            BigDecimal _bigDecimal_3 = new BigDecimal(_lowerEndpoint_9);
            BigDecimal _upperEndpoint_8 = mulRange.upperEndpoint();
            final BigDecimal ft = _bigDecimal_3.multiply(_upperEndpoint_8);
            BigInteger _upperEndpoint_9 = lrVal.upperEndpoint();
            BigDecimal _bigDecimal_4 = new BigDecimal(_upperEndpoint_9);
            BigDecimal _lowerEndpoint_10 = mulRange.lowerEndpoint();
            final BigDecimal tf = _bigDecimal_4.multiply(_lowerEndpoint_10);
            BigInteger _upperEndpoint_10 = lrVal.upperEndpoint();
            BigDecimal _bigDecimal_5 = new BigDecimal(_upperEndpoint_10);
            BigDecimal _upperEndpoint_11 = mulRange.upperEndpoint();
            final BigDecimal tt = _bigDecimal_5.multiply(_upperEndpoint_11);
            BigDecimal _min = ff.min(ft);
            BigDecimal _min_1 = _min.min(tf);
            BigDecimal _min_2 = _min_1.min(tt);
            BigInteger _bigInteger = _min_2.toBigInteger();
            BigDecimal _max = ff.max(ft);
            BigDecimal _max_1 = _max.max(tf);
            BigDecimal _max_2 = _max_1.max(tt);
            BigInteger _bigInteger_1 = _max_2.toBigInteger();
            Range<BigInteger> _createRange_2 = RangeTool.<BigInteger>createRange(_bigInteger, _bigInteger_1);
            return Optional.<Range<BigInteger>>of(_createRange_2);
        case MUL:
            BigInteger _lowerEndpoint_11 = lrVal.lowerEndpoint();
            BigInteger _lowerEndpoint_12 = rrVal.lowerEndpoint();
            final BigInteger ff_1 = _lowerEndpoint_11.multiply(_lowerEndpoint_12);
            BigInteger _lowerEndpoint_13 = lrVal.lowerEndpoint();
            BigInteger _upperEndpoint_12 = rrVal.upperEndpoint();
            final BigInteger ft_1 = _lowerEndpoint_13.multiply(_upperEndpoint_12);
            BigInteger _upperEndpoint_13 = lrVal.upperEndpoint();
            BigInteger _lowerEndpoint_14 = rrVal.lowerEndpoint();
            final BigInteger tf_1 = _upperEndpoint_13.multiply(_lowerEndpoint_14);
            BigInteger _upperEndpoint_14 = lrVal.upperEndpoint();
            BigInteger _upperEndpoint_15 = rrVal.upperEndpoint();
            final BigInteger tt_1 = _upperEndpoint_14.multiply(_upperEndpoint_15);
            BigInteger _min_3 = ff_1.min(ft_1);
            BigInteger _min_4 = _min_3.min(tf_1);
            BigInteger _min_5 = _min_4.min(tt_1);
            BigInteger _max_3 = ff_1.max(ft_1);
            BigInteger _max_4 = _max_3.max(tf_1);
            BigInteger _max_5 = _max_4.max(tt_1);
            Range<BigInteger> _createRange_3 = RangeTool.<BigInteger>createRange(_min_5, _max_5);
            return Optional.<Range<BigInteger>>of(_createRange_3);
        case MOD:
            final BigInteger rle = rrVal.lowerEndpoint();
            final BigInteger leftBound = rle.min(BigInteger.ZERO);
            BigInteger _upperEndpoint_16 = rrVal.upperEndpoint();
            final BigInteger rue = _upperEndpoint_16.subtract(BigInteger.ONE);
            final BigInteger rightBound = rue.max(BigInteger.ZERO);
            Range<BigInteger> _createRange_4 = RangeTool.<BigInteger>createRange(leftBound, rightBound);
            return Optional.<Range<BigInteger>>of(_createRange_4);
        case POW:
            BigInteger _lowerEndpoint_15 = lrVal.lowerEndpoint();
            BigInteger _lowerEndpoint_16 = rrVal.lowerEndpoint();
            int _intValue = _lowerEndpoint_16.intValue();
            final BigInteger ff_2 = _lowerEndpoint_15.pow(_intValue);
            BigInteger _lowerEndpoint_17 = lrVal.lowerEndpoint();
            BigInteger _upperEndpoint_17 = rrVal.upperEndpoint();
            int _intValue_1 = _upperEndpoint_17.intValue();
            final BigInteger ft_2 = _lowerEndpoint_17.pow(_intValue_1);
            BigInteger _upperEndpoint_18 = lrVal.upperEndpoint();
            BigInteger _lowerEndpoint_18 = rrVal.lowerEndpoint();
            int _intValue_2 = _lowerEndpoint_18.intValue();
            final BigInteger tf_2 = _upperEndpoint_18.pow(_intValue_2);
            BigInteger _upperEndpoint_19 = lrVal.upperEndpoint();
            BigInteger _upperEndpoint_20 = rrVal.upperEndpoint();
            int _intValue_3 = _upperEndpoint_20.intValue();
            final BigInteger tt_2 = _upperEndpoint_19.pow(_intValue_3);
            BigInteger _min_6 = ff_2.min(ft_2);
            BigInteger _min_7 = _min_6.min(tf_2);
            BigInteger _min_8 = _min_7.min(tt_2);
            BigInteger _max_6 = ff_2.max(ft_2);
            BigInteger _max_7 = _max_6.max(tf_2);
            BigInteger _max_8 = _max_7.max(tt_2);
            Range<BigInteger> _createRange_5 = RangeTool.<BigInteger>createRange(_min_8, _max_8);
            return Optional.<Range<BigInteger>>of(_createRange_5);
        default:
            break;
        }
    }
    throw new RuntimeException("Incorrectly implemented obj op");
}

From source file:org.pshdl.model.validation.builtin.BuiltInValidator.java

/**
 *
 * @param accessRange//from  www . ja  v a2 s. c o m
 *            the range in which the array/bits can be acccessed
 * @param indexRange
 *            the range of the size that array size/ width of the type can
 *            be in
 * @param problems
 *            problems will be added here
 * @param arr
 *            the accessing {@link HDLExpression}
 * @param ref
 *            the reference that is accessed
 * @param bit
 *            when true bit access errors will be reported
 */
private static void checkAccessBoundaries(Range<BigInteger> accessRange, Range<BigInteger> declaredRange,
        Set<Problem> problems, IHDLObject arr, HDLVariableRef ref, boolean bit) {
    // Reduce the declaredRange to the index limits
    Range<BigInteger> indexRange;
    if (declaredRange.hasUpperBound()) {
        final BigInteger upperEndpoint = declaredRange.upperEndpoint();
        final BigInteger subtract = upperEndpoint.subtract(BigInteger.ONE);
        if (subtract.compareTo(BigInteger.ZERO) < 0)
            // Maybe generate a warning here?
            return;
        indexRange = RangeTool.createRange(BigInteger.ZERO, subtract);
    } else {
        indexRange = Range.atLeast(BigInteger.ZERO);
    }
    final String info = "Expected value range:" + indexRange;
    // Check if highest idx is negative (Definitely a problem)
    if (accessRange.hasUpperBound() && (accessRange.upperEndpoint().signum() < 0)) {
        problems.add(new Problem(bit ? BIT_ACCESS_NEGATIVE : ARRAY_INDEX_NEGATIVE, arr, ref, info)
                .addMeta(ACCESS_RANGE, accessRange).addMeta(ARRAY_RANGE, indexRange));
        // Check if lowest idx is negative (Might be a problem)
    } else if (accessRange.hasLowerBound() && (accessRange.lowerEndpoint().signum() < 0)) {
        problems.add(
                new Problem(bit ? BIT_ACCESS_POSSIBLY_NEGATIVE : ARRAY_INDEX_POSSIBLY_NEGATIVE, arr, ref, info)
                        .addMeta(ACCESS_RANGE, accessRange).addMeta(ARRAY_RANGE, indexRange));
    }
    // Check whether the index and the access have at least something in
    // common (index 0..5 access 7..9)
    if (!indexRange.isConnected(accessRange)) {
        problems.add(new Problem(bit ? BIT_ACCESS_OUT_OF_BOUNDS : ARRAY_INDEX_OUT_OF_BOUNDS, arr, ref, info)
                .addMeta(ACCESS_RANGE, accessRange).addMeta(ARRAY_RANGE, indexRange));
    } else if (accessRange.hasUpperBound() && indexRange.hasUpperBound()
            && (accessRange.upperEndpoint().compareTo(indexRange.upperEndpoint()) > 0)) {
        problems.add(new Problem(bit ? BIT_ACCESS_POSSIBLY_OUT_OF_BOUNDS : ARRAY_INDEX_POSSIBLY_OUT_OF_BOUNDS,
                arr, ref, info).addMeta(ACCESS_RANGE, accessRange).addMeta(ARRAY_RANGE, indexRange));
    }
}

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 ww.j a  v a2s.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:net.sourceforge.ganttproject.task.algorithm.SchedulerImpl.java

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;
        }/*from  w  w w  .j av a 2s. com*/
        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);
    }
}