當前位置: 首頁>>代碼示例>>Java>>正文


Java IntMath.checkedAdd方法代碼示例

本文整理匯總了Java中com.google.common.math.IntMath.checkedAdd方法的典型用法代碼示例。如果您正苦於以下問題:Java IntMath.checkedAdd方法的具體用法?Java IntMath.checkedAdd怎麽用?Java IntMath.checkedAdd使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.google.common.math.IntMath的用法示例。


在下文中一共展示了IntMath.checkedAdd方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: noAddOverflow

import com.google.common.math.IntMath; //導入方法依賴的package包/類
@Override
public boolean noAddOverflow(int a, int b) {
  try {
    IntMath.checkedAdd(a, b);
    return true;
  } catch (ArithmeticException e) {
    return false;
  }
}
 
開發者ID:sander120786,項目名稱:guava-libraries,代碼行數:10,代碼來源:ApacheBenchmark.java

示例2: binop

import com.google.common.math.IntMath; //導入方法依賴的package包/類
static Integer binop(Kind kind, int lhs, int rhs) {
  switch (kind) {
    case MULTIPLY:
      return IntMath.checkedMultiply(lhs, rhs);
    case DIVIDE:
      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;
  }
}
 
開發者ID:google,項目名稱:error-prone,代碼行數:29,代碼來源:ConstantOverflow.java

示例3: size

import com.google.common.math.IntMath; //導入方法依賴的package包/類
@Override
public int size() {
	if (cycles < 0) return 0;
	try {
		return IntMath.checkedAdd(IntMath.checkedMultiply(cycles, bufferSize), (bufferSize - index));
	} catch (ArithmeticException overflow) {
		return Integer.MAX_VALUE;
	}
}
 
開發者ID:jbosboom,項目名稱:streamjit,代碼行數:10,代碼來源:Datasets.java

示例4: grow

import com.google.common.math.IntMath; //導入方法依賴的package包/類
static final public byte[] grow(byte[] src, int amount) {
    if (amount < 0) {
        throw new IllegalArgumentException("amount must be greater than zero");
    }
    if (src == null) {
        return new byte[amount];
    }
    byte[] newSrc = new byte[IntMath.checkedAdd(src.length, amount)];
    System.arraycopy(src, 0, newSrc, 0, src.length);
    return newSrc;
}
 
開發者ID:jivesoftware,項目名稱:amza,代碼行數:12,代碼來源:HeapFiler.java

示例5: add

import com.google.common.math.IntMath; //導入方法依賴的package包/類
/**
 * Adds a number of occurrences of the specified element to this multiset.
 *
 * @param element the element to add
 * @param occurrences the number of occurrences to add
 * @return the previous count of the element before the operation; possibly zero
 * @throws IllegalArgumentException if {@code occurrences} is negative, or if
 *     the resulting amount would exceed {@link Integer#MAX_VALUE}
 */
@CanIgnoreReturnValue
@Override
public int add(E element, int occurrences) {
  checkNotNull(element);
  if (occurrences == 0) {
    return count(element);
  }
  CollectPreconditions.checkPositive(occurrences, "occurences");

  while (true) {
    AtomicInteger existingCounter = Maps.safeGet(countMap, element);
    if (existingCounter == null) {
      existingCounter = countMap.putIfAbsent(element, new AtomicInteger(occurrences));
      if (existingCounter == null) {
        return 0;
      }
      // existingCounter != null: fall through to operate against the existing AtomicInteger
    }

    while (true) {
      int oldValue = existingCounter.get();
      if (oldValue != 0) {
        try {
          int newValue = IntMath.checkedAdd(oldValue, occurrences);
          if (existingCounter.compareAndSet(oldValue, newValue)) {
            // newValue can't == 0, so no need to check & remove
            return oldValue;
          }
        } catch (ArithmeticException overflow) {
          throw new IllegalArgumentException(
              "Overflow adding " + occurrences + " occurrences to a count of " + oldValue);
        }
      } else {
        // In the case of a concurrent remove, we might observe a zero value, which means another
        // thread is about to remove (element, existingCounter) from the map. Rather than wait,
        // we can just do that work here.
        AtomicInteger newCounter = new AtomicInteger(occurrences);
        if ((countMap.putIfAbsent(element, newCounter) == null)
            || countMap.replace(element, existingCounter, newCounter)) {
          return 0;
        }
        break;
      }
    }

    // If we're still here, there was a race, so just try again.
  }
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:58,代碼來源:ConcurrentHashMultiset.java

示例6: add

import com.google.common.math.IntMath; //導入方法依賴的package包/類
/**
 * Adds a number of occurrences of the specified element to this multiset.
 *
 * @param element the element to add
 * @param occurrences the number of occurrences to add
 * @return the previous count of the element before the operation; possibly zero
 * @throws IllegalArgumentException if {@code occurrences} is negative, or if
 *     the resulting amount would exceed {@link Integer#MAX_VALUE}
 */

@CanIgnoreReturnValue
@Override
public int add(E element, int occurrences) {
  checkNotNull(element);
  if (occurrences == 0) {
    return count(element);
  }
  CollectPreconditions.checkPositive(occurrences, "occurences");
  while (true) {
    AtomicInteger existingCounter = Maps.safeGet(countMap, element);
    if (existingCounter == null) {
      existingCounter = countMap.putIfAbsent(element, new AtomicInteger(occurrences));
      if (existingCounter == null) {
        return 0;
      }
      // existingCounter != null: fall through to operate against the existing AtomicInteger
    }
    while (true) {
      int oldValue = existingCounter.get();
      if (oldValue != 0) {
        try {
          int newValue = IntMath.checkedAdd(oldValue, occurrences);
          if (existingCounter.compareAndSet(oldValue, newValue)) {
            // newValue can't == 0, so no need to check & remove
            return oldValue;
          }
        } catch (ArithmeticException overflow) {
          throw new IllegalArgumentException("Overflow adding " + occurrences + " occurrences to a count of " + oldValue);
        }
      } else {
        // In the case of a concurrent remove, we might observe a zero value, which means another
        // thread is about to remove (element, existingCounter) from the map. Rather than wait,
        // we can just do that work here.
        AtomicInteger newCounter = new AtomicInteger(occurrences);
        if ((countMap.putIfAbsent(element, newCounter) == null)
            || countMap.replace(element, existingCounter, newCounter)) {
          return 0;
        }
        break;
      }
    }

    // If we're still here, there was a race, so just try again.
  }
}
 
開發者ID:antlr,項目名稱:codebuff,代碼行數:56,代碼來源:ConcurrentHashMultiset.java

示例7: add

import com.google.common.math.IntMath; //導入方法依賴的package包/類
/**
 * Adds a number of occurrences of the specified element to this multiset.
 *
 * @param element the element to add
 * @param occurrences the number of occurrences to add
 * @return the previous count of the element before the operation; possibly zero
 * @throws IllegalArgumentException if {@code occurrences} is negative, or if
 *     the resulting amount would exceed {@link Integer#MAX_VALUE}
 */
@Override public int add(E element, int occurrences) {
  checkNotNull(element);
  if (occurrences == 0) {
    return count(element);
  }
  checkArgument(occurrences > 0, "Invalid occurrences: %s", occurrences);

  while (true) {
    AtomicInteger existingCounter = Maps.safeGet(countMap, element);
    if (existingCounter == null) {
      existingCounter = countMap.putIfAbsent(element, new AtomicInteger(occurrences));
      if (existingCounter == null) {
        return 0;
      }
      // existingCounter != null: fall through to operate against the existing AtomicInteger
    }

    while (true) {
      int oldValue = existingCounter.get();
      if (oldValue != 0) {
        try {
          int newValue = IntMath.checkedAdd(oldValue, occurrences);
          if (existingCounter.compareAndSet(oldValue, newValue)) {
            // newValue can't == 0, so no need to check & remove
            return oldValue;
          }
        } catch (ArithmeticException overflow) {
          throw new IllegalArgumentException("Overflow adding " + occurrences
              + " occurrences to a count of " + oldValue);
        }
      } else {
        // In the case of a concurrent remove, we might observe a zero value, which means another
        // thread is about to remove (element, existingCounter) from the map. Rather than wait,
        // we can just do that work here.
        AtomicInteger newCounter = new AtomicInteger(occurrences);
        if ((countMap.putIfAbsent(element, newCounter) == null)
            || countMap.replace(element, existingCounter, newCounter)) {
          return 0;
        }
        break;
      }
    }

    // If we're still here, there was a race, so just try again.
  }
}
 
開發者ID:cplutte,項目名稱:bts,代碼行數:56,代碼來源:ConcurrentHashMultiset.java

示例8: add

import com.google.common.math.IntMath; //導入方法依賴的package包/類
/**
 * Adds a number of occurrences of the specified element to this multiset.
 *
 * @param element the element to add
 * @param occurrences the number of occurrences to add
 * @return the previous count of the element before the operation; possibly zero
 * @throws IllegalArgumentException if {@code occurrences} is negative, or if
 *     the resulting amount would exceed {@link Integer#MAX_VALUE}
 */
@Override public int add(E element, int occurrences) {
  checkNotNull(element);
  if (occurrences == 0) {
    return count(element);
  }
  checkArgument(occurrences > 0, "Invalid occurrences: %s", occurrences);

  while (true) {
    AtomicInteger existingCounter = safeGet(element);
    if (existingCounter == null) {
      existingCounter = countMap.putIfAbsent(element, new AtomicInteger(occurrences));
      if (existingCounter == null) {
        return 0;
      }
      // existingCounter != null: fall through to operate against the existing AtomicInteger
    }

    while (true) {
      int oldValue = existingCounter.get();
      if (oldValue != 0) {
        try {
          int newValue = IntMath.checkedAdd(oldValue, occurrences);
          if (existingCounter.compareAndSet(oldValue, newValue)) {
            // newValue can't == 0, so no need to check & remove
            return oldValue;
          }
        } catch (ArithmeticException overflow) {
          throw new IllegalArgumentException("Overflow adding " + occurrences
              + " occurrences to a count of " + oldValue);
        }
      } else {
        // In the case of a concurrent remove, we might observe a zero value, which means another
        // thread is about to remove (element, existingCounter) from the map. Rather than wait,
        // we can just do that work here.
        AtomicInteger newCounter = new AtomicInteger(occurrences);
        if ((countMap.putIfAbsent(element, newCounter) == null)
            || countMap.replace(element, existingCounter, newCounter)) {
          return 0;
        }
        break;
      }
    }

    // If we're still here, there was a race, so just try again.
  }
}
 
開發者ID:10xEngineer,項目名稱:My-Wallet-Android,代碼行數:56,代碼來源:ConcurrentHashMultiset.java

示例9: add

import com.google.common.math.IntMath; //導入方法依賴的package包/類
/**
 * Adds a number of occurrences of the specified element to this multiset.
 *
 * @param element the element to add
 * @param occurrences the number of occurrences to add
 * @return the previous count of the element before the operation; possibly zero
 * @throws IllegalArgumentException if {@code occurrences} is negative, or if the resulting amount
 *     would exceed {@link Integer#MAX_VALUE}
 */
@CanIgnoreReturnValue
@Override
public int add(E element, int occurrences) {
  checkNotNull(element);
  if (occurrences == 0) {
    return count(element);
  }
  CollectPreconditions.checkPositive(occurrences, "occurences");

  while (true) {
    AtomicInteger existingCounter = Maps.safeGet(countMap, element);
    if (existingCounter == null) {
      existingCounter = countMap.putIfAbsent(element, new AtomicInteger(occurrences));
      if (existingCounter == null) {
        return 0;
      }
      // existingCounter != null: fall through to operate against the existing AtomicInteger
    }

    while (true) {
      int oldValue = existingCounter.get();
      if (oldValue != 0) {
        try {
          int newValue = IntMath.checkedAdd(oldValue, occurrences);
          if (existingCounter.compareAndSet(oldValue, newValue)) {
            // newValue can't == 0, so no need to check & remove
            return oldValue;
          }
        } catch (ArithmeticException overflow) {
          throw new IllegalArgumentException(
              "Overflow adding " + occurrences + " occurrences to a count of " + oldValue);
        }
      } else {
        // In the case of a concurrent remove, we might observe a zero value, which means another
        // thread is about to remove (element, existingCounter) from the map. Rather than wait,
        // we can just do that work here.
        AtomicInteger newCounter = new AtomicInteger(occurrences);
        if ((countMap.putIfAbsent(element, newCounter) == null)
            || countMap.replace(element, existingCounter, newCounter)) {
          return 0;
        }
        break;
      }
    }

    // If we're still here, there was a race, so just try again.
  }
}
 
開發者ID:google,項目名稱:guava,代碼行數:58,代碼來源:ConcurrentHashMultiset.java

示例10: add

import com.google.common.math.IntMath; //導入方法依賴的package包/類
@Override
public IntegerCost add(Cost cost) {
    IntegerCost other = (IntegerCost) cost;
    return new IntegerCost(IntMath.checkedAdd(this.value, other.value));
}
 
開發者ID:DieterDePaepe,項目名稱:JSearch,代碼行數:6,代碼來源:IntegerCost.java


注:本文中的com.google.common.math.IntMath.checkedAdd方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。