(
int op1,
int op2
)
| 169 | * @throws ArithmeticException if int-range overflows |
| 170 | */ |
| 171 | public static int safeSubtract( |
| 172 | int op1, |
| 173 | int op2 |
| 174 | ) { |
| 175 | |
| 176 | if (op2 == 0) { |
| 177 | return op1; |
| 178 | } |
| 179 | |
| 180 | long result = ((long) op1) - ((long) op2); |
| 181 | |
| 182 | if ((result < Integer.MIN_VALUE) || (result > Integer.MAX_VALUE)) { |
| 183 | StringBuilder sb = new StringBuilder(32); |
| 184 | sb.append("Integer overflow: ("); |
| 185 | sb.append(op1); |
| 186 | sb.append(','); |
| 187 | sb.append(op2); |
| 188 | sb.append(')'); |
| 189 | throw new ArithmeticException(sb.toString()); |
| 190 | } else { |
| 191 | return (int) result; |
| 192 | } |
| 193 | |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * <p>Subtracts the numbers from each other with range check. </p> |
no test coverage detected