| 597 | * @since 2.0 |
| 598 | */ |
| 599 | public MachineTime<U> plus( |
| 600 | long amount, |
| 601 | U unit |
| 602 | ) { |
| 603 | |
| 604 | long s = this.seconds; |
| 605 | int f = this.nanos; |
| 606 | |
| 607 | if (this.scale == POSIX) { |
| 608 | TimeUnit u = TimeUnit.class.cast(unit); |
| 609 | |
| 610 | if (u.compareTo(TimeUnit.SECONDS) >= 0) { |
| 611 | s = |
| 612 | Math.addExact( |
| 613 | s, |
| 614 | Math.multiplyExact( |
| 615 | amount, |
| 616 | TimeUnit.SECONDS.convert(1, u)) |
| 617 | ); |
| 618 | } else { |
| 619 | long total = |
| 620 | Math.addExact( |
| 621 | f, |
| 622 | Math.multiplyExact( |
| 623 | amount, |
| 624 | TimeUnit.NANOSECONDS.convert(1, u)) |
| 625 | ); |
| 626 | s = Math.addExact(s, Math.floorDiv(total, MRD)); |
| 627 | f = (int) Math.floorMod(total, MRD); |
| 628 | } |
| 629 | } else { |
| 630 | switch (SI.class.cast(unit)) { |
| 631 | case SECONDS: |
| 632 | s = Math.addExact(s, amount); |
| 633 | break; |
| 634 | case NANOSECONDS: |
| 635 | long total = Math.addExact(f, amount); |
| 636 | s = Math.addExact(s, Math.floorDiv(total, MRD)); |
| 637 | f = (int) Math.floorMod(total, MRD); |
| 638 | break; |
| 639 | default: |
| 640 | throw new UnsupportedOperationException(unit.toString()); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | return new MachineTime<>(s, f, this.scale); |
| 645 | |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * <p>Add given temporal amount to this machine time. </p> |