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

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

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:org.apache.impala.util.MathUtil.java

public static long saturatingMultiply(long a, long b) {
    try {//from www .ja  v a  2s.c o m
        return LongMath.checkedMultiply(a, b);
    } catch (ArithmeticException e) {
        return a < 0 != b < 0 ? Long.MIN_VALUE : Long.MAX_VALUE;
    }
}

From source file:org.apache.phoenix.util.SequenceUtil.java

/**
 * @return true if we limit of a sequence has been reached.
 *///from   w  w w.  j  av  a2s  .  c  om
public static boolean checkIfLimitReached(long currentValue, long minValue, long maxValue, long incrementBy,
        long cacheSize, long numToAllocate) {
    long nextValue = 0;
    boolean increasingSeq = incrementBy > 0 ? true : false;
    // advance currentValue while checking for overflow    
    try {
        long incrementValue;
        if (isBulkAllocation(numToAllocate)) {
            // For bulk allocation we increment independent of cache size
            incrementValue = LongMath.checkedMultiply(incrementBy, numToAllocate);
        } else {
            incrementValue = LongMath.checkedMultiply(incrementBy, cacheSize);
        }
        nextValue = LongMath.checkedAdd(currentValue, incrementValue);
    } catch (ArithmeticException e) {
        return true;
    }

    // check if limit was reached
    if ((increasingSeq && nextValue > maxValue) || (!increasingSeq && nextValue < minValue)) {
        return true;
    }
    return false;
}

From source file:com.github.mgunlogson.cuckoofilter4j.FilterTable.java

/**
 * Creates a FilterTable//from   ww w .  j a va2 s.com
 * 
 * @param bitsPerTag
 *            number of bits needed for each tag
 * @param numBuckets
 *            number of buckets in filter
 * @return
 */
static FilterTable create(int bitsPerTag, long numBuckets) {
    // why would this ever happen?
    checkArgument(bitsPerTag < 48, "tagBits (%s) should be less than 48 bits", bitsPerTag);
    // shorter fingerprints don't give us a good fill capacity
    checkArgument(bitsPerTag > 4, "tagBits (%s) must be > 4", bitsPerTag);
    checkArgument(numBuckets > 1, "numBuckets (%s) must be > 1", numBuckets);
    // checked so our implementors don't get too.... "enthusiastic" with
    // table size
    long bitsPerBucket = IntMath.checkedMultiply(CuckooFilter.BUCKET_SIZE, bitsPerTag);
    long bitSetSize = LongMath.checkedMultiply(bitsPerBucket, numBuckets);
    LongBitSet memBlock = new LongBitSet(bitSetSize);
    return new FilterTable(memBlock, bitsPerTag, numBuckets);
}

From source file:com.duprasville.guava.probably.CuckooTable.java

public static int calculateDataLength(long numBuckets, int numEntriesPerBucket, int numBitsPerEntry) {
    checkArgument(numBuckets > 0, "numBuckets (%s) must be > 0", numBuckets);
    checkArgument(numEntriesPerBucket > 0, "numEntriesPerBucket (%s) must be > 0", numEntriesPerBucket);
    checkArgument(numBitsPerEntry > 0, "numBitsPerEntry (%s) must be > 0", numBitsPerEntry);

    return Ints.checkedCast(LongMath.divide(
            LongMath.checkedMultiply(numBuckets,
                    LongMath.checkedMultiply(numEntriesPerBucket, numBitsPerEntry)),
            Long.SIZE, RoundingMode.CEILING));
}

From source file:com.android.tradefed.util.TimeVal.java

/**
 * Parses the string as a hierarchical time value
 * <p />/*w ww  . j a v a2  s . c  o  m*/
 * The default unit is millis.  The parser will accept {@code s} for seconds (1000 millis),
 * {@code m} for minutes (60 seconds), {@code h} for hours (60 minutes), or {@code d} for days
 * (24 hours).
 * <p />
 * Units may be mixed and matched, so long as each unit appears at most once, and so long as
 * all units which do appear are listed in decreasing order of scale.  So, for instance,
 * {@code h} may only appear before {@code m}, and may only appear after {@code d}.  As a
 * specific example, "1d2h3m4s5ms" would be a valid time value, as would "4" or "4ms".  All
 * embedded whitespace is discarded.
 * <p />
 * Do note that this method rejects overflows.  So the output number is guaranteed to be
 * non-negative, and to fit within the {@code long} type.
 */
public static long fromString(String value) throws NumberFormatException {
    if (value == null)
        throw new NumberFormatException("value is null");

    try {
        value = value.replaceAll("\\s+", "");
        Matcher m = TIME_PATTERN.matcher(value);
        if (m.matches()) {
            // This works by, essentially, modifying the units of timeValue, from the
            // largest supported unit, until we've dropped down to millis.
            long timeValue = 0;
            timeValue = val(m.group("d"));

            // 1 day == 24 hours
            timeValue = LongMath.checkedMultiply(timeValue, 24);
            timeValue = LongMath.checkedAdd(timeValue, val(m.group("h")));

            // 1 hour == 60 minutes
            timeValue = LongMath.checkedMultiply(timeValue, 60);
            timeValue = LongMath.checkedAdd(timeValue, val(m.group("m")));

            // 1 hour == 60 seconds
            timeValue = LongMath.checkedMultiply(timeValue, 60);
            timeValue = LongMath.checkedAdd(timeValue, val(m.group("s")));

            // 1 second == 1000 millis
            timeValue = LongMath.checkedMultiply(timeValue, 1000);
            timeValue = LongMath.checkedAdd(timeValue, val(m.group("ms")));

            return timeValue;
        }
    } catch (ArithmeticException e) {
        throw new NumberFormatException(
                String.format("Failed to parse value %s as a time value: %s", value, e.getMessage()));
    }

    throw new NumberFormatException(String.format("Failed to parse value %s as a time value", value));
}

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

public Fiat multiply(final long factor) {
    return new Fiat(currencyCode, LongMath.checkedMultiply(this.value, factor));
}

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

public Value multiply(final long factor) {
    return new Value(this.type, LongMath.checkedMultiply(this.value, factor));
}

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

public Coin multiply(final long factor) {
    return new Coin(LongMath.checkedMultiply(this.value, factor));
}

From source file:org.apache.beam.runners.dataflow.worker.util.ScalableBloomFilter.java

/** Calculate the number of optimal insertions based upon the number of bytes. */
private static long optimalNumInsertions(long bytes, double p) {
    checkArgument(p > 0, "Expected false positive probability to be positive.");
    return LongMath.checkedMultiply(8,
            DoubleMath.roundToLong(-bytes * Math.log(2) * Math.log(2) / Math.log(p), RoundingMode.DOWN));
}

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.  jav  a 2s .com
        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;
    }
}