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

The following examples show how to use com.google.common.math.LongMath#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: SequenceUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
   * Returns the nextValue of a sequence 
   * @throws SQLException if cycle is false and the sequence limit has been reached
   */
  public static boolean checkIfLimitReached(long currentValue, long minValue, long maxValue,
          long incrementBy, long cacheSize) throws SQLException {
      long nextValue = 0;
      boolean increasingSeq = incrementBy > 0 ? true : false;
      // advance currentValue while checking for overflow    
      try {
          long 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;
  }
 
Example 2
Source File: SequenceUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
   * @return true if we limit of a sequence has been reached.
   */
  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;
  }
 
Example 3
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 4
Source File: Fiat.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
public Fiat multiply(final long factor) {
    return new Fiat(currencyCode, LongMath.checkedMultiply(this.value, factor));
}
 
Example 5
Source File: Coin.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
public Coin multiply(final long factor) {
    return new Coin(LongMath.checkedMultiply(this.value, factor));
}
 
Example 6
Source File: Altcoin.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
public Altcoin multiply(final long factor) {
    return new Altcoin(currencyCode, LongMath.checkedMultiply(this.value, factor));
}
 
Example 7
Source File: Fiat.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
public Fiat multiply(final long factor) {
    return new Fiat(currencyCode, LongMath.checkedMultiply(this.value, factor));
}
 
Example 8
Source File: Coin.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
public Coin multiply(final long factor) {
    return new Coin(LongMath.checkedMultiply(this.value, factor));
}
 
Example 9
Source File: Fiat.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
public Fiat multiply(final long factor) {
    return new Fiat(currencyCode, LongMath.checkedMultiply(this.value, factor));
}
 
Example 10
Source File: Coin.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
public Coin multiply(final long factor) {
    return new Coin(LongMath.checkedMultiply(this.value, factor));
}
 
Example 11
Source File: AkInterval.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected long parseLong(long parsed, Boolean isYear) {
    if (isYear)
        parsed = LongMath.checkedMultiply(parsed, 12);
    return parsed;
}
 
Example 12
Source File: ArmeriaConfigurationUtil.java    From armeria with Apache License 2.0 4 votes vote down vote up
/**
 * Parses the data size text as a decimal {@code long}.
 *
 * @param dataSizeText the data size text, i.e. {@code 1}, {@code 1B}, {@code 1KB}, {@code 1MB},
 *                     {@code 1GB} or {@code 1TB}
 */
private static long parseDataSize(String dataSizeText) {
    requireNonNull(dataSizeText, "text");
    final Matcher matcher = DATA_SIZE_PATTERN.matcher(dataSizeText);
    checkArgument(matcher.matches(),
                  "Invalid data size text: %s (expected: %s)",
                  dataSizeText, DATA_SIZE_PATTERN);

    final long unit;
    final String unitText = matcher.group(2);
    if (Strings.isNullOrEmpty(unitText)) {
        unit = 1L;
    } else {
        switch (Ascii.toLowerCase(unitText)) {
            case "b":
                unit = 1L;
                break;
            case "kb":
                unit = 1024L;
                break;
            case "mb":
                unit = 1024L * 1024L;
                break;
            case "gb":
                unit = 1024L * 1024L * 1024L;
                break;
            case "tb":
                // TODO(ikhoon): Simplify with Math.pow?
                unit = 1024L * 1024L * 1024L * 1024L;
                break;
            default:
                throw new IllegalArgumentException("Invalid data size text: " + dataSizeText +
                                                   " (expected: " + DATA_SIZE_PATTERN + ')');
        }
    }
    try {
        final long amount = Long.parseLong(matcher.group(1));
        return LongMath.checkedMultiply(amount, unit);
    } catch (Exception e) {
        throw new IllegalArgumentException("Invalid data size text: " + dataSizeText +
                                           " (expected: " + DATA_SIZE_PATTERN + ')', e);
    }
}
 
Example 13
Source File: ArmeriaConfigurationUtil.java    From armeria with Apache License 2.0 4 votes vote down vote up
/**
 * Parses the data size text as a decimal {@code long}.
 *
 * @param dataSizeText the data size text, i.e. {@code 1}, {@code 1B}, {@code 1KB}, {@code 1MB},
 *                     {@code 1GB} or {@code 1TB}
 */
public static long parseDataSize(String dataSizeText) {
    requireNonNull(dataSizeText, "text");
    final Matcher matcher = DATA_SIZE_PATTERN.matcher(dataSizeText);
    checkArgument(matcher.matches(),
                  "Invalid data size text: %s (expected: %s)",
                  dataSizeText, DATA_SIZE_PATTERN);

    final long unit;
    final String unitText = matcher.group(2);
    if (Strings.isNullOrEmpty(unitText)) {
        unit = 1L;
    } else {
        switch (Ascii.toLowerCase(unitText)) {
            case "b":
                unit = 1L;
                break;
            case "kb":
                unit = 1024L;
                break;
            case "mb":
                unit = 1024L * 1024L;
                break;
            case "gb":
                unit = 1024L * 1024L * 1024L;
                break;
            case "tb":
                unit = 1024L * 1024L * 1024L * 1024L;
                break;
            default:
                throw new IllegalArgumentException("Invalid data size text: " + dataSizeText +
                                                   " (expected: " + DATA_SIZE_PATTERN + ')');
        }
    }
    try {
        final long amount = Long.parseLong(matcher.group(1));
        return LongMath.checkedMultiply(amount, unit);
    } catch (Exception e) {
        throw new IllegalArgumentException("Invalid data size text: " + dataSizeText +
                                           " (expected: " + DATA_SIZE_PATTERN + ')', e);
    }
}
 
Example 14
Source File: Altcoin.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public Altcoin multiply(final long factor) {
    return new Altcoin(currencyCode, LongMath.checkedMultiply(this.value, factor));
}
 
Example 15
Source File: GuavaLongMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenCheckedMultiplyTwoLongValues_shouldMultiplyThemAndReturnTheResultIfNotOverflow() {
    long result = LongMath.checkedMultiply(3L, 2L);
    assertEquals(6L, result);
}
 
Example 16
Source File: GuavaLongMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test(expected = ArithmeticException.class)
public void whenCheckedMultiplyTwoLongValues_shouldThrowArithmeticExceptionIfOverflow() {
    LongMath.checkedMultiply(Long.MAX_VALUE, 100L);
}