Java Code Examples for com.google.common.math.IntMath#checkedMultiply()

The following examples show how to use com.google.common.math.IntMath#checkedMultiply() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FilterTable.java    From CuckooFilter4J with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a FilterTable
 * 
 * @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);
}
 
Example 2
Source File: CartesianList.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
CartesianList(ImmutableList<List<E>> axes) {
  this.axes = axes;

  int[] axesSizeProduct = new int[axes.size() + 1];
  axesSizeProduct[axes.size()] = 1;
  try {
    for (int i = axes.size() - 1; i >= 0; i--) {
      axesSizeProduct[i] = IntMath.checkedMultiply(axesSizeProduct[i + 1], axes.get(i).size());
    }
  } catch (ArithmeticException e) {
    throw new IllegalArgumentException("Cartesian product too large; must have size at most Integer.MAX_VALUE");
  }
  this.axesSizeProduct = axesSizeProduct;
}
 
Example 3
Source File: MinMaxPriorityQueue.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Returns ~2x the old capacity if small; ~1.5x otherwise. */

  private int calculateNewCapacity() {
    int oldCapacity = queue.length;
    int newCapacity = (oldCapacity < 64) ? (oldCapacity + 1) * 2 : IntMath.checkedMultiply(oldCapacity / 2, 3);
    return capAtMaximumSize(newCapacity, maximumSize);
          }
 
Example 4
Source File: CartesianList.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
CartesianList(ImmutableList<List<E>> axes) {
  this.axes = axes;

  int[] axesSizeProduct = new int[axes.size() + 1];
  axesSizeProduct[axes.size()] = 1;
  try {
    for (int i = axes.size() - 1; i >= 0; i--) {
      axesSizeProduct[i] = IntMath.checkedMultiply(axesSizeProduct[i + 1], axes.get(i).size());
    }
  } catch (ArithmeticException e) {
    throw new IllegalArgumentException("Cartesian product too large; must have size at most Integer.MAX_VALUE");
  }
  this.axesSizeProduct = axesSizeProduct;
}
 
Example 5
Source File: MinMaxPriorityQueue.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Returns ~2x the old capacity if small; ~1.5x otherwise. */

  private int calculateNewCapacity() {
    int oldCapacity = queue.length;
    int newCapacity = (oldCapacity < 64) ? (oldCapacity + 1) * 2 : IntMath.checkedMultiply(oldCapacity / 2, 3);
    return capAtMaximumSize(newCapacity, maximumSize);
          }
 
Example 6
Source File: CartesianList.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
CartesianList(ImmutableList<List<E>> axes) {
  this.axes = axes;

  int[] axesSizeProduct = new int[axes.size() + 1];
  axesSizeProduct[axes.size()] = 1;
  try {
    for (int i = axes.size() - 1; i >= 0; i--) {
      axesSizeProduct[i] = IntMath.checkedMultiply(axesSizeProduct[i + 1], axes.get(i).size());
    }
  } catch (ArithmeticException e) {
    throw new IllegalArgumentException("Cartesian product too large; must have size at most Integer.MAX_VALUE");
  }
  this.axesSizeProduct = axesSizeProduct;
}
 
Example 7
Source File: MinMaxPriorityQueue.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Returns ~2x the old capacity if small; ~1.5x otherwise. */

  private int calculateNewCapacity() {
    int oldCapacity = queue.length;
    int newCapacity = (oldCapacity < 64) ? (oldCapacity + 1) * 2 : IntMath.checkedMultiply(oldCapacity / 2, 3);
    return capAtMaximumSize(newCapacity, maximumSize);
          }
 
Example 8
Source File: CartesianList.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
CartesianList(ImmutableList<List<E>> axes) {
  this.axes = axes;

  int[] axesSizeProduct = new int[axes.size() + 1];
  axesSizeProduct[axes.size()] = 1;
  try {
    for (int i = axes.size() - 1; i >= 0; i--) {
      axesSizeProduct[i] = IntMath.checkedMultiply(axesSizeProduct[i + 1], axes.get(i).size());
    }
  } catch (ArithmeticException e) {
    throw new IllegalArgumentException("Cartesian product too large; must have size at most Integer.MAX_VALUE");
  }
  this.axesSizeProduct = axesSizeProduct;
}
 
Example 9
Source File: MinMaxPriorityQueue.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Returns ~2x the old capacity if small; ~1.5x otherwise. */

  private int calculateNewCapacity() {
    int oldCapacity = queue.length;
    int newCapacity = (oldCapacity < 64) ? (oldCapacity + 1) * 2 : IntMath.checkedMultiply(oldCapacity / 2, 3);
    return capAtMaximumSize(newCapacity, maximumSize);
          }
 
Example 10
Source File: CartesianList.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
CartesianList(ImmutableList<List<E>> axes) {
  this.axes = axes;
  int[] axesSizeProduct = new int[axes.size() + 1];
  axesSizeProduct[axes.size()] = 1;
  try {
    for (int i = axes.size() - 1; i >= 0; i--) {
      axesSizeProduct[i] = IntMath.checkedMultiply(axesSizeProduct[i + 1], axes.get(i).size());
    }
  } catch (ArithmeticException e) {
    throw new IllegalArgumentException(
        "Cartesian product too large; must have size at most Integer.MAX_VALUE");
  }
  this.axesSizeProduct = axesSizeProduct;
}
 
Example 11
Source File: MinMaxPriorityQueue.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Returns ~2x the old capacity if small; ~1.5x otherwise. */
private int calculateNewCapacity() {
  int oldCapacity = queue.length;
  int newCapacity =
      (oldCapacity < 64)
          ? (oldCapacity + 1) * 2
          : IntMath.checkedMultiply(oldCapacity / 2, 3);
  return capAtMaximumSize(newCapacity, maximumSize);
}
 
Example 12
Source File: GuavaMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test(expected = ArithmeticException.class)
public void whenProductOverflow_thenThrowException() {
    IntMath.checkedMultiply(Integer.MAX_VALUE, 2);
}
 
Example 13
Source File: GuavaMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test(expected = ArithmeticException.class)
public void whenProductUnderflow_thenThrowException() {
    IntMath.checkedMultiply(Integer.MIN_VALUE, 2);
}
 
Example 14
Source File: GuavaMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void should_calculate_product() {
    int result = IntMath.checkedMultiply(21, 3);
    assertThat(result, equalTo(63));
}
 
Example 15
Source File: GuavaIntMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenCheckedMultiplyTwoIntegerValues_shouldMultiplyThemAndReturnTheResultIfNotOverflow() {
    int result = IntMath.checkedMultiply(1, 2);
    assertEquals(2, result);
}
 
Example 16
Source File: GuavaIntMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test(expected = ArithmeticException.class)
public void gwhenCheckedMultiplyTwoIntegerValues_shouldThrowArithmeticExceptionIfOverflow() {
    IntMath.checkedMultiply(Integer.MAX_VALUE, 100);
}