| 2596 | } |
| 2597 | |
| 2598 | private static class TimeUnitRule |
| 2599 | implements UnitRule<Moment> { |
| 2600 | |
| 2601 | //~ Instanzvariablen ---------------------------------------------- |
| 2602 | |
| 2603 | private final TimeUnit unit; |
| 2604 | |
| 2605 | //~ Konstruktoren ------------------------------------------------- |
| 2606 | |
| 2607 | TimeUnitRule(TimeUnit unit) { |
| 2608 | super(); |
| 2609 | |
| 2610 | this.unit = unit; |
| 2611 | |
| 2612 | } |
| 2613 | |
| 2614 | //~ Methoden ------------------------------------------------------ |
| 2615 | |
| 2616 | @Override |
| 2617 | public Moment addTo( |
| 2618 | Moment context, |
| 2619 | long amount |
| 2620 | ) { |
| 2621 | |
| 2622 | if (this.unit.compareTo(TimeUnit.SECONDS) >= 0) { |
| 2623 | long secs = |
| 2624 | Math.multiplyExact(amount, this.unit.toSeconds(1)); |
| 2625 | return Moment.of( |
| 2626 | Math.addExact(context.getPosixTime(), secs), |
| 2627 | context.getNanosecond(), |
| 2628 | POSIX |
| 2629 | ); |
| 2630 | } else { // MILLIS, MICROS, NANOS |
| 2631 | long nanos = |
| 2632 | Math.multiplyExact(amount, this.unit.toNanos(1)); |
| 2633 | long sum = Math.addExact(context.getNanosecond(), nanos); |
| 2634 | int nano = (int) Math.floorMod(sum, MRD); |
| 2635 | long second = Math.floorDiv(sum, MRD); |
| 2636 | |
| 2637 | return Moment.of( |
| 2638 | Math.addExact(context.getPosixTime(), second), |
| 2639 | nano, |
| 2640 | POSIX |
| 2641 | ); |
| 2642 | } |
| 2643 | |
| 2644 | } |
| 2645 | |
| 2646 | @Override |
| 2647 | public long between( |
| 2648 | Moment start, |
| 2649 | Moment end |
| 2650 | ) { |
| 2651 | |
| 2652 | long delta; |
| 2653 | |
| 2654 | if (this.unit.compareTo(TimeUnit.SECONDS) >= 0) { |
| 2655 | delta = (end.getPosixTime() - start.getPosixTime()); |
nothing calls this directly
no outgoing calls
no test coverage detected