462 /**
463 * A hash code for this clock.
464 * <p>
465 * Clocks should override this method based on
466 * their state and to meet the contract of {@link Object#hashCode}.
467 * If not overridden, the behavior is defined by {@link Object#hashCode}
468 *
469 * @return a suitable hash code
470 */
471 @Override
472 public int hashCode() {
473 return super.hashCode();
474 }
475
476 //-----------------------------------------------------------------------
477 /**
478 * Implementation of a clock that always returns the latest time from
479 * {@link System#currentTimeMillis()}.
480 */
481 static final class SystemClock extends Clock implements Serializable {
482 private static final long serialVersionUID = 6740630888130243051L;
483 private static final long OFFSET_SEED =
484 System.currentTimeMillis()/1000 - 1024; // initial offest
485 static final SystemClock UTC = new SystemClock(ZoneOffset.UTC);
486
487 private final ZoneId zone;
488 // We don't actually need a volatile here.
489 // We don't care if offset is set or read concurrently by multiple
490 // threads - we just need a value which is 'recent enough' - in other
491 // words something that has been updated at least once in the last
492 // 2^32 secs (~136 years). And even if we by chance see an invalid
493 // offset, the worst that can happen is that we will get a -1 value
494 // from getNanoTimeAdjustment, forcing us to update the offset
495 // once again.
496 private transient long offset;
497
498 SystemClock(ZoneId zone) {
499 this.zone = zone;
500 this.offset = OFFSET_SEED;
501 }
555 offset = localOffset;
556 }
557 }
558 return Instant.ofEpochSecond(localOffset, adjustment);
559 }
560 @Override
561 public boolean equals(Object obj) {
562 if (obj instanceof SystemClock) {
563 return zone.equals(((SystemClock) obj).zone);
564 }
565 return false;
566 }
567 @Override
568 public int hashCode() {
569 return zone.hashCode() + 1;
570 }
571 @Override
572 public String toString() {
573 return "SystemClock[" + zone + "]";
574 }
575 private void readObject(ObjectInputStream is)
576 throws IOException, ClassNotFoundException {
577 // ensure that offset is initialized
578 is.defaultReadObject();
579 offset = OFFSET_SEED;
580 }
581 }
582
583 //-----------------------------------------------------------------------
584 /**
585 * Implementation of a clock that always returns the same instant.
586 * This is typically used for testing.
587 */
588 static final class FixedClock extends Clock implements Serializable {
589 private static final long serialVersionUID = 7430389292664866958L;
590 private final Instant instant;
591 private final ZoneId zone;
592
593 FixedClock(Instant fixedInstant, ZoneId zone) {
594 this.instant = fixedInstant;
595 this.zone = zone;
596 }
597 @Override
598 public ZoneId getZone() {
599 return zone;
600 }
601 @Override
602 public Clock withZone(ZoneId zone) {
603 if (zone.equals(this.zone)) { // intentional NPE
604 return this;
605 }
606 return new FixedClock(instant, zone);
607 }
608 @Override
619 FixedClock other = (FixedClock) obj;
620 return instant.equals(other.instant) && zone.equals(other.zone);
621 }
622 return false;
623 }
624 @Override
625 public int hashCode() {
626 return instant.hashCode() ^ zone.hashCode();
627 }
628 @Override
629 public String toString() {
630 return "FixedClock[" + instant + "," + zone + "]";
631 }
632 }
633
634 //-----------------------------------------------------------------------
635 /**
636 * Implementation of a clock that adds an offset to an underlying clock.
637 */
638 static final class OffsetClock extends Clock implements Serializable {
639 private static final long serialVersionUID = 2007484719125426256L;
640 private final Clock baseClock;
641 private final Duration offset;
642
643 OffsetClock(Clock baseClock, Duration offset) {
644 this.baseClock = baseClock;
645 this.offset = offset;
646 }
647 @Override
648 public ZoneId getZone() {
649 return baseClock.getZone();
650 }
651 @Override
652 public Clock withZone(ZoneId zone) {
653 if (zone.equals(baseClock.getZone())) { // intentional NPE
654 return this;
655 }
656 return new OffsetClock(baseClock.withZone(zone), offset);
657 }
658 @Override
669 OffsetClock other = (OffsetClock) obj;
670 return baseClock.equals(other.baseClock) && offset.equals(other.offset);
671 }
672 return false;
673 }
674 @Override
675 public int hashCode() {
676 return baseClock.hashCode() ^ offset.hashCode();
677 }
678 @Override
679 public String toString() {
680 return "OffsetClock[" + baseClock + "," + offset + "]";
681 }
682 }
683
684 //-----------------------------------------------------------------------
685 /**
686 * Implementation of a clock that adds an offset to an underlying clock.
687 */
688 static final class TickClock extends Clock implements Serializable {
689 private static final long serialVersionUID = 6504659149906368850L;
690 private final Clock baseClock;
691 private final long tickNanos;
692
693 TickClock(Clock baseClock, long tickNanos) {
694 this.baseClock = baseClock;
695 this.tickNanos = tickNanos;
696 }
697 @Override
698 public ZoneId getZone() {
699 return baseClock.getZone();
700 }
701 @Override
702 public Clock withZone(ZoneId zone) {
703 if (zone.equals(baseClock.getZone())) { // intentional NPE
704 return this;
705 }
706 return new TickClock(baseClock.withZone(zone), tickNanos);
707 }
708 @Override
|
462 /**
463 * A hash code for this clock.
464 * <p>
465 * Clocks should override this method based on
466 * their state and to meet the contract of {@link Object#hashCode}.
467 * If not overridden, the behavior is defined by {@link Object#hashCode}
468 *
469 * @return a suitable hash code
470 */
471 @Override
472 public int hashCode() {
473 return super.hashCode();
474 }
475
476 //-----------------------------------------------------------------------
477 /**
478 * Implementation of a clock that always returns the latest time from
479 * {@link System#currentTimeMillis()}.
480 */
481 static final class SystemClock extends Clock implements Serializable {
482 @java.io.Serial
483 private static final long serialVersionUID = 6740630888130243051L;
484 private static final long OFFSET_SEED =
485 System.currentTimeMillis()/1000 - 1024; // initial offest
486 static final SystemClock UTC = new SystemClock(ZoneOffset.UTC);
487
488 private final ZoneId zone;
489 // We don't actually need a volatile here.
490 // We don't care if offset is set or read concurrently by multiple
491 // threads - we just need a value which is 'recent enough' - in other
492 // words something that has been updated at least once in the last
493 // 2^32 secs (~136 years). And even if we by chance see an invalid
494 // offset, the worst that can happen is that we will get a -1 value
495 // from getNanoTimeAdjustment, forcing us to update the offset
496 // once again.
497 private transient long offset;
498
499 SystemClock(ZoneId zone) {
500 this.zone = zone;
501 this.offset = OFFSET_SEED;
502 }
556 offset = localOffset;
557 }
558 }
559 return Instant.ofEpochSecond(localOffset, adjustment);
560 }
561 @Override
562 public boolean equals(Object obj) {
563 if (obj instanceof SystemClock) {
564 return zone.equals(((SystemClock) obj).zone);
565 }
566 return false;
567 }
568 @Override
569 public int hashCode() {
570 return zone.hashCode() + 1;
571 }
572 @Override
573 public String toString() {
574 return "SystemClock[" + zone + "]";
575 }
576 @java.io.Serial
577 private void readObject(ObjectInputStream is)
578 throws IOException, ClassNotFoundException {
579 // ensure that offset is initialized
580 is.defaultReadObject();
581 offset = OFFSET_SEED;
582 }
583 }
584
585 //-----------------------------------------------------------------------
586 /**
587 * Implementation of a clock that always returns the same instant.
588 * This is typically used for testing.
589 */
590 static final class FixedClock extends Clock implements Serializable {
591 @java.io.Serial
592 private static final long serialVersionUID = 7430389292664866958L;
593 private final Instant instant;
594 private final ZoneId zone;
595
596 FixedClock(Instant fixedInstant, ZoneId zone) {
597 this.instant = fixedInstant;
598 this.zone = zone;
599 }
600 @Override
601 public ZoneId getZone() {
602 return zone;
603 }
604 @Override
605 public Clock withZone(ZoneId zone) {
606 if (zone.equals(this.zone)) { // intentional NPE
607 return this;
608 }
609 return new FixedClock(instant, zone);
610 }
611 @Override
622 FixedClock other = (FixedClock) obj;
623 return instant.equals(other.instant) && zone.equals(other.zone);
624 }
625 return false;
626 }
627 @Override
628 public int hashCode() {
629 return instant.hashCode() ^ zone.hashCode();
630 }
631 @Override
632 public String toString() {
633 return "FixedClock[" + instant + "," + zone + "]";
634 }
635 }
636
637 //-----------------------------------------------------------------------
638 /**
639 * Implementation of a clock that adds an offset to an underlying clock.
640 */
641 static final class OffsetClock extends Clock implements Serializable {
642 @java.io.Serial
643 private static final long serialVersionUID = 2007484719125426256L;
644 private final Clock baseClock;
645 private final Duration offset;
646
647 OffsetClock(Clock baseClock, Duration offset) {
648 this.baseClock = baseClock;
649 this.offset = offset;
650 }
651 @Override
652 public ZoneId getZone() {
653 return baseClock.getZone();
654 }
655 @Override
656 public Clock withZone(ZoneId zone) {
657 if (zone.equals(baseClock.getZone())) { // intentional NPE
658 return this;
659 }
660 return new OffsetClock(baseClock.withZone(zone), offset);
661 }
662 @Override
673 OffsetClock other = (OffsetClock) obj;
674 return baseClock.equals(other.baseClock) && offset.equals(other.offset);
675 }
676 return false;
677 }
678 @Override
679 public int hashCode() {
680 return baseClock.hashCode() ^ offset.hashCode();
681 }
682 @Override
683 public String toString() {
684 return "OffsetClock[" + baseClock + "," + offset + "]";
685 }
686 }
687
688 //-----------------------------------------------------------------------
689 /**
690 * Implementation of a clock that adds an offset to an underlying clock.
691 */
692 static final class TickClock extends Clock implements Serializable {
693 @java.io.Serial
694 private static final long serialVersionUID = 6504659149906368850L;
695 private final Clock baseClock;
696 private final long tickNanos;
697
698 TickClock(Clock baseClock, long tickNanos) {
699 this.baseClock = baseClock;
700 this.tickNanos = tickNanos;
701 }
702 @Override
703 public ZoneId getZone() {
704 return baseClock.getZone();
705 }
706 @Override
707 public Clock withZone(ZoneId zone) {
708 if (zone.equals(baseClock.getZone())) { // intentional NPE
709 return this;
710 }
711 return new TickClock(baseClock.withZone(zone), tickNanos);
712 }
713 @Override
|