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

The following examples show how to use com.google.common.math.IntMath#saturatedMultiply() . 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: ArmeriaCentralDogma.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static String encodePathPattern(String pathPattern) {
    // We do not need full escaping because we validated the path pattern already and thus contains only
    // -, ' ', /, *, _, ., ',', a-z, A-Z, 0-9.
    // See Util.isValidPathPattern() for more information.
    int spacePos = pathPattern.indexOf(' ');
    if (spacePos < 0) {
        return pathPattern;
    }

    final StringBuilder buf = new StringBuilder(IntMath.saturatedMultiply(pathPattern.length(), 2));
    for (int pos = 0;;) {
        buf.append(pathPattern, pos, spacePos);
        buf.append("%20");
        pos = spacePos + 1;
        spacePos = pathPattern.indexOf(' ', pos);
        if (spacePos < 0) {
            buf.append(pathPattern, pos, pathPattern.length());
            break;
        }
    }

    return buf.toString();
}
 
Example 2
Source File: GuavaMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenProductOverflow_thenReturnMaxInteger() {
    int result = IntMath.saturatedMultiply(Integer.MAX_VALUE, 2);
    assertThat(result, equalTo(Integer.MAX_VALUE));
}
 
Example 3
Source File: GuavaMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenProductUnderflow_thenReturnMinInteger() {
    int result = IntMath.saturatedMultiply(Integer.MIN_VALUE, 2);
    assertThat(result, equalTo(Integer.MIN_VALUE));
}
 
Example 4
Source File: GuavaIntMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenSaturatedMultiplyTwoIntegerValues_shouldMultiplyThemAndReturnTheResult() {
    int result = IntMath.saturatedMultiply(6, 4);
    assertEquals(24, result);
}
 
Example 5
Source File: GuavaIntMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenSaturatedMultiplyTwoIntegerValues_shouldMultiplyThemAndReturnIntMaxIfOverflow() {
    int result = IntMath.saturatedMultiply(Integer.MAX_VALUE, 1000);
    assertEquals(Integer.MAX_VALUE, result);
}
 
Example 6
Source File: GuavaIntMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenSaturatedMultiplyTwoIntegerValues_shouldMultiplyThemAndReturnIntMinIfUnderflow() {
    int result = IntMath.saturatedMultiply(Integer.MIN_VALUE, 1000);
    assertEquals(Integer.MIN_VALUE, result);
}