Example usage for com.google.common.math LongMath checkedSubtract

List of usage examples for com.google.common.math LongMath checkedSubtract

Introduction

In this page you can find the example usage for com.google.common.math LongMath checkedSubtract.

Prototype

@GwtIncompatible("TODO")
public static long checkedSubtract(long a, long b) 

Source Link

Document

Returns the difference of a and b , provided it does not overflow.

Usage

From source file:io.druid.segment.data.IntermediateLongSupplierSerializer.java

private void makeDelegate() throws IOException {
    CompressionFactory.LongEncodingWriter writer;
    long delta;//w  w  w  . j a va  2s  . co m
    try {
        delta = LongMath.checkedSubtract(maxVal, minVal);
    } catch (ArithmeticException e) {
        delta = -1;
    }
    if (uniqueValues.size() <= CompressionFactory.MAX_TABLE_SIZE) {
        writer = new TableLongEncodingWriter(uniqueValues);
    } else if (delta != -1 && delta != Long.MAX_VALUE) {
        writer = new DeltaLongEncodingWriter(minVal, delta);
    } else {
        writer = new LongsLongEncodingWriter(order);
    }

    if (compression == CompressedObjectStrategy.CompressionStrategy.NONE) {
        delegate = new EntireLayoutLongSupplierSerializer(ioPeon, filenameBase, order, writer);
    } else {
        delegate = new BlockLayoutLongSupplierSerializer(ioPeon, filenameBase, order, writer, compression);
    }

    DataInputStream tempIn = new DataInputStream(new BufferedInputStream(ioPeon.makeInputStream(tempFile)));
    delegate.open();
    while (tempIn.available() > 0) {
        delegate.add(tempIn.readLong());
    }
}

From source file:org.neoscoinj.utils.Fiat.java

public Fiat subtract(final Fiat value) {
    checkArgument(value.currencyCode.equals(currencyCode));
    return new Fiat(currencyCode, LongMath.checkedSubtract(this.value, value.value));
}

From source file:org.apache.druid.segment.data.IntermediateColumnarLongsSerializer.java

private void makeDelegate() throws IOException {
    //noinspection VariableNotUsedInsideIf
    if (delegate != null) {
        return;/*from  w  w w.  j  a  v  a 2 s . co m*/
    }
    CompressionFactory.LongEncodingWriter writer;
    long delta;
    try {
        delta = LongMath.checkedSubtract(maxVal, minVal);
    } catch (ArithmeticException e) {
        delta = -1;
    }
    if (uniqueValues.size() <= CompressionFactory.MAX_TABLE_SIZE) {
        writer = new TableLongEncodingWriter(uniqueValues, valuesAddedInOrder);
    } else if (delta != -1 && delta != Long.MAX_VALUE) {
        writer = new DeltaLongEncodingWriter(minVal, delta);
    } else {
        writer = new LongsLongEncodingWriter(order);
    }

    if (compression == CompressionStrategy.NONE) {
        delegate = new EntireLayoutColumnarLongsSerializer(segmentWriteOutMedium, writer);
    } else {
        delegate = new BlockLayoutColumnarLongsSerializer(segmentWriteOutMedium, filenameBase, order, writer,
                compression);
    }

    delegate.open();
    for (int i = 0; i < tempOut.size(); i++) {
        delegate.add(tempOut.getLong(i));
    }
}

From source file:com.mygeopay.core.coins.Value.java

public Value subtract(final Value value) {
    checkArgument(type.equals(value.type), "Cannot subtract a different type");
    return new Value(this.type, LongMath.checkedSubtract(this.value, value.value));
}

From source file:org.apache.hadoop.hbase.regionserver.compactions.ExponentialCompactionWindowFactory.java

private long getMaxTierAgeCutoff(long now) {
    try {//from ww w.j a  va2s . com
        return LongMath.checkedSubtract(now, maxTierAgeMillis);
    } catch (ArithmeticException ae) {
        LOG.warn("Value for " + MAX_TIER_AGE_MILLIS_KEY + ": " + maxTierAgeMillis
                + ". Will always promote to next tier.");
        return Long.MIN_VALUE;
    }
}

From source file:com.matthewmitchell.nubitsj.core.Coin.java

public Coin subtract(final Coin value) {
    return new Coin(LongMath.checkedSubtract(this.value, value.value));
}

From source file:com.coinomi.core.coins.Value.java

public Value subtract(final Coin value) {
    return new Value(this.type, LongMath.checkedSubtract(this.value, value.value));
}

From source file:org.eclipse.che.ide.ext.datasource.server.SqlRequestService.java

/**
 * Executes the SQL requests given as parameter.
 * //from  w w  w .  ja  v  a 2  s  .  c o m
 * @param request the requests parameters
 * @return a result object, either success (with data) or failure (with message)
 * @throws SQLException if the execution caused an error
 * @throws DatabaseDefinitionException if the datasource is not correctly defined
 */
// same path as 'class'
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN })
public String executeSqlRequest(final RequestParameterDTO request)
        throws SQLException, DatabaseDefinitionException, BadSQLRequestParameterException {
    checkParameters(request);
    try (final Connection connection = this.jdbcConnectionFactory
            .getDatabaseConnection(request.getDatabase())) {

        MultipleRequestExecutionMode mode = SqlRequestService.DEFAULT_MODE;
        if (request.getMultipleRequestExecutionMode() != null) {
            mode = request.getMultipleRequestExecutionMode();
        }

        long startTime = System.currentTimeMillis();
        final RequestResultGroupDTO resultGroup = executeSqlRequest(request, connection, mode);
        long endExecTime = System.currentTimeMillis();

        String json = DtoFactory.getInstance().toJson(resultGroup);
        long endJsonTime = System.currentTimeMillis();
        try {
            LOG.debug(
                    "Execution of SQL request '{}' with result limit {} - sql duration={}, json conversion duration={}",
                    request.getSqlRequest(), request.getResultLimit(),
                    LongMath.checkedSubtract(endExecTime, startTime),
                    LongMath.checkedSubtract(endJsonTime, endExecTime));
        } catch (final ArithmeticException e) {
            LOG.debug("Execution of SQL request '{}' with result limit {} - unknwown durations");
        }
        LOG.trace("Return {}", json);
        return json;
    }
}

From source file:com.coinomi.core.coins.Value.java

public Value subtract(long value) {
    return new Value(this.type, LongMath.checkedSubtract(this.value, value));
}

From source file:com.google.errorprone.bugpatterns.ConstantOverflow.java

static Long binop(Kind kind, long lhs, long rhs) {
    switch (kind) {
    case MULTIPLY:
        return LongMath.checkedMultiply(lhs, rhs);
    case DIVIDE://  w w  w . j  a v a2  s  .co m
        return lhs / rhs;
    case REMAINDER:
        return lhs % rhs;
    case PLUS:
        return LongMath.checkedAdd(lhs, rhs);
    case MINUS:
        return LongMath.checkedSubtract(lhs, rhs);
    case LEFT_SHIFT:
        return lhs << rhs;
    case RIGHT_SHIFT:
        return lhs >> rhs;
    case UNSIGNED_RIGHT_SHIFT:
        return lhs >>> rhs;
    case AND:
        return lhs & rhs;
    case XOR:
        return lhs ^ rhs;
    case OR:
        return lhs | rhs;
    default:
        return null;
    }
}