Returns the sum of a and b unless it would overflow or underflow in which case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively. Copy of com.google.common.math.LongMath#saturatedAdd.
(long a, long b)
| 420 | * |
| 421 | */ |
| 422 | @SuppressWarnings("ShortCircuitBoolean") |
| 423 | private static long saturatedAdd(long a, long b) { |
| 424 | long naiveSum = a + b; |
| 425 | if ((a ^ b) < 0 | (a ^ naiveSum) >= 0) { |
| 426 | // If a and b have different signs or a has the same sign as the result then there was no |
| 427 | // overflow, return. |
| 428 | return naiveSum; |
| 429 | } |
| 430 | // we did over/under flow, if the sign is negative we should return MAX otherwise MIN |
| 431 | return Long.MAX_VALUE + ((naiveSum >>> (Long.SIZE - 1)) ^ 1); |
| 432 | } |
| 433 | } |
no outgoing calls
no test coverage detected