| 1365 | * @throws ArithmeticException in case of overflow |
| 1366 | */ |
| 1367 | public Moment plus( |
| 1368 | long amount, |
| 1369 | SI unit |
| 1370 | ) { |
| 1371 | |
| 1372 | Moment.check1972(this); |
| 1373 | |
| 1374 | if (amount == 0) { |
| 1375 | return this; |
| 1376 | } |
| 1377 | |
| 1378 | Moment result; |
| 1379 | |
| 1380 | try { |
| 1381 | switch (unit) { |
| 1382 | case SECONDS: |
| 1383 | if (LeapSeconds.getInstance().isEnabled()) { |
| 1384 | result = new Moment( |
| 1385 | Math.addExact(this.getElapsedTimeUTC(), amount), |
| 1386 | this.getNanosecond(), |
| 1387 | UTC); |
| 1388 | } else { |
| 1389 | result = Moment.of( |
| 1390 | Math.addExact(this.posixTime, amount), |
| 1391 | this.getNanosecond(), |
| 1392 | POSIX |
| 1393 | ); |
| 1394 | } |
| 1395 | break; |
| 1396 | case NANOSECONDS: |
| 1397 | long sum = |
| 1398 | Math.addExact(this.getNanosecond(), amount); |
| 1399 | int nano = (int) Math.floorMod(sum, MRD); |
| 1400 | long second = Math.floorDiv(sum, MRD); |
| 1401 | |
| 1402 | if (LeapSeconds.getInstance().isEnabled()) { |
| 1403 | result = new Moment( |
| 1404 | Math.addExact(this.getElapsedTimeUTC(), second), |
| 1405 | nano, |
| 1406 | UTC |
| 1407 | ); |
| 1408 | } else { |
| 1409 | result = Moment.of( |
| 1410 | Math.addExact(this.posixTime, second), |
| 1411 | nano, |
| 1412 | POSIX |
| 1413 | ); |
| 1414 | } |
| 1415 | break; |
| 1416 | default: |
| 1417 | throw new UnsupportedOperationException(); |
| 1418 | } |
| 1419 | } catch (IllegalArgumentException iae) { |
| 1420 | ArithmeticException ex = |
| 1421 | new ArithmeticException( |
| 1422 | "Result beyond boundaries of time axis."); |
| 1423 | ex.initCause(iae); |
| 1424 | throw ex; |