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

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

Introduction

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

Prototype

public static int checkedMultiply(int a, int b) 

Source Link

Document

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

Usage

From source file:com.forerunnergames.tools.common.Maths.java

/**
 * Calculates the next higher multiple of the specified integer.
 *
 * For example, the next higher multiple of 4 from 10 is 12.
 *
 * @param n//w  w w.j a v  a  2  s  .  c om
 *          The number to obtain the next higher multiple of, must be >= 0.
 * @param multiple
 *          The desired multiple that n must be evenly divisible by, must be > 0.
 */
public static int nextHigherMultiple(final int n, final int multiple) {
    Arguments.checkIsNotNegative(n, "n");
    Arguments.checkLowerInclusiveBound(multiple, 1, "multiple");

    return IntMath.checkedMultiply(((IntMath.checkedAdd(n, (multiple - 1))) / multiple), multiple);
}

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

/**
 * Creates a FilterTable/*from   ww w  . j a  v  a  2  s  . c o  m*/
 * 
 * @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:omeis.providers.re.RGBIntBuffer.java

/**
 * Creates a new 3-band packed integer buffer.
 * //ww w.  jav a 2  s  .  co m
 * @param sizeX1
 *            The number of pixels on the <i>X1</i>-axis. This is the <i>X</i>-axis
 *            in the case of an <i>XY</i>-plane or <i>XZ</i>-plane.
 *            Otherwise it is the <i>Z</i>-axis &#151; <i>ZY</i>-plane.
 * @param sizeX2
 *            The number of pixels on the <i>X2</i>-axis. This is the <i>Y</i>-axis
 *            in the case of an <i>XY</i>-plane or <i>ZY</i>-plane.
 *            Otherwise it is the <i>Z</i>-axis &#151; <i>XZ</i>-plane.
 * @see #bands
 */
public RGBIntBuffer(int sizeX1, int sizeX2) {
    this.sizeX1 = sizeX1;
    this.sizeX2 = sizeX2;
    dataBuf = new int[IntMath.checkedMultiply(sizeX1, sizeX2)];
}

From source file:com.tkmtwo.timex.WallClock.java

private static int sumFields(int hrs, int mins, int secs) {
    int sumSecs = 0;
    sumSecs = IntMath.checkedAdd(sumSecs, IntMath.checkedMultiply(hrs, SECONDS_PER_HOUR));
    sumSecs = IntMath.checkedAdd(sumSecs, IntMath.checkedMultiply(mins, SECONDS_PER_MINUTE));
    sumSecs = IntMath.checkedAdd(sumSecs, secs);
    return sumSecs;
}

From source file:edu.mit.streamjit.test.Datasets.java

public static <I> Input<I> nCopies(final int n, final Input<I> input) {
    //0 would be valid (an empty input), but would usually be a bug.
    checkArgument(n > 0, "%s must be nonnegative", n);
    if (n == 1)/*from   w w  w. j  a va 2 s  . com*/
        return input;
    return InputBufferFactory.wrap(new InputBufferFactory() {
        @Override
        public Buffer createReadableBuffer(final int readerMinSize) {
            final Buffer firstBuffer = InputBufferFactory.unwrap(input).createReadableBuffer(readerMinSize);
            if (firstBuffer instanceof PeekableBuffer)
                return new LoopedBufferBuffer(n, (PeekableBuffer) firstBuffer);
            return new AbstractReadOnlyBuffer() {
                private Buffer currentBuffer = firstBuffer;
                private final int bufferSize = currentBuffer.size();
                private int copiesRemaining = n - 1;

                @Override
                public Object read() {
                    if (currentBuffer.size() == 0 && copiesRemaining > 0) {
                        currentBuffer = InputBufferFactory.unwrap(input).createReadableBuffer(readerMinSize);
                        --copiesRemaining;
                    }
                    return currentBuffer.read();
                }

                @Override
                public int size() {
                    try {
                        return IntMath.checkedAdd(currentBuffer.size(),
                                IntMath.checkedMultiply(copiesRemaining, bufferSize));
                    } catch (ArithmeticException overflow) {
                        return Integer.MAX_VALUE;
                    }
                }
            };
        }
    });
}

From source file:com.hubrick.vertx.kafka.consumer.KafkaConsumer.java

private int computeNextDelay(int delaySeconds) {
    try {//ww w  .  jav  a  2 s  . co m
        return Math.min(IntMath.checkedMultiply(delaySeconds, 2), configuration.getMaxRetryDelaySeconds());
    } catch (ArithmeticException e) {
        return configuration.getMaxRetryDelaySeconds();
    }
}

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

static Integer binop(Kind kind, int lhs, int rhs) {
    switch (kind) {
    case MULTIPLY:
        return IntMath.checkedMultiply(lhs, rhs);
    case DIVIDE:/*from  w  ww .  java  2  s. c  o  m*/
        return lhs / rhs;
    case REMAINDER:
        return lhs % rhs;
    case PLUS:
        return IntMath.checkedAdd(lhs, rhs);
    case MINUS:
        return IntMath.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;
    }
}