| 253 | * @throws ArithmeticException if int-range overflows |
| 254 | */ |
| 255 | public static int safeMultiply( |
| 256 | int op1, |
| 257 | int op2 |
| 258 | ) { |
| 259 | |
| 260 | if (op2 == 1) { |
| 261 | return op1; |
| 262 | } |
| 263 | |
| 264 | long result = ((long) op1) * ((long) op2); |
| 265 | |
| 266 | if ((result < Integer.MIN_VALUE) || (result > Integer.MAX_VALUE)) { |
| 267 | StringBuilder sb = new StringBuilder(32); |
| 268 | sb.append("Integer overflow: ("); |
| 269 | sb.append(op1); |
| 270 | sb.append(','); |
| 271 | sb.append(op2); |
| 272 | sb.append(')'); |
| 273 | throw new ArithmeticException(sb.toString()); |
| 274 | } else { |
| 275 | return (int) result; |
| 276 | } |
| 277 | |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * <p>Multiplies the numbers with range check. </p> |