001/*
002 * Java Genetic Algorithm Library (jenetics-8.2.0).
003 * Copyright (c) 2007-2025 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020package io.jenetics.util;
021
022import static java.util.Objects.requireNonNull;
023
024import java.io.Serial;
025import java.io.Serializable;
026import java.time.Clock;
027import java.time.Instant;
028import java.time.ZoneId;
029import java.time.ZoneOffset;
030
031/**
032 * Clock implementation with <i>nano</i> second precision.
033 *
034 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
035 * @since 3.1
036 * @version 3.1
037 */
038public final class NanoClock extends Clock implements Serializable {
039
040        @Serial
041        private static final long serialVersionUID = 1L;
042
043        private static final long EPOCH_NANOS = System.currentTimeMillis()*1_000_000;
044        private static final long NANO_START = System.nanoTime();
045        private static final long OFFSET_NANOS = EPOCH_NANOS - NANO_START;
046
047        private static final NanoClock UTC_INSTANCE = new NanoClock(ZoneOffset.UTC);
048
049        private static final NanoClock DEFAULT_INSTANCE =
050                new NanoClock(ZoneId.systemDefault());
051
052        /**
053         * These constants hold the number of nanoseconds of one second.
054         */
055        public static final long NANOS_PER_SECOND = 1_000_000_000;
056
057        private final ZoneId _zone;
058
059        private NanoClock(final ZoneId zone)  {
060                _zone = requireNonNull(zone, "zone");
061        }
062
063        @Override
064        public ZoneId getZone() {
065                return _zone;
066        }
067
068        @Override
069        public NanoClock withZone(final ZoneId zone) {
070                return zone.equals(_zone) ? this : new NanoClock(zone);
071        }
072
073        @Override
074        public long millis() {
075                return System.currentTimeMillis();
076        }
077
078        /**
079         * This returns the nanosecond-based instant, measured from
080         * 1970-01-01T00:00Z (UTC). This method will return valid values till the
081         * year 2262.
082         *
083         * @return the nanosecond-based instant, measured from 1970-01-01T00:00Z (UTC)
084         */
085        public long nanos() {
086                return System.nanoTime() + OFFSET_NANOS;
087        }
088
089        @Override
090        public Instant instant() {
091                final long now = nanos();
092                return Instant.ofEpochSecond(now/NANOS_PER_SECOND, now%NANOS_PER_SECOND);
093        }
094
095        @Override
096        public int hashCode() {
097                return _zone.hashCode() + 11;
098        }
099
100        @Override
101        public boolean equals(final Object obj) {
102                return obj instanceof NanoClock other &&
103                        other._zone.equals(_zone);
104        }
105
106        @Override
107        public String toString() {
108                return "NanoClock[" + _zone + "]";
109        }
110
111        /**
112         * This clock is based on the <i>nano</i> system clock. It uses
113         * {@link System#nanoTime()} resolution
114         * <p>
115         * Conversion from instant to date or time uses the specified time-zone.
116         * <p>
117         * The returned implementation is immutable, thread-safe and
118         * {@code Serializable}.
119         *
120         * @param zone  the time-zone to use to convert the instant to date-time
121         * @return a clock that uses the best available system clock in the
122         *         specified zone
123         * @throws java.lang.NullPointerException if the given {@code zone} is
124         *         {@code null}
125         */
126        public static NanoClock system(final ZoneId zone) {
127                return new NanoClock(zone);
128        }
129
130        /**
131         * This clock is based on the <i>nano</i> system clock. It uses
132         * {@link System#nanoTime()} resolution
133         * <p>
134         * Conversion from instant to date or time uses the specified time-zone.
135         * <p>
136         * The returned implementation is immutable, thread-safe and
137         * {@code Serializable}.
138         *
139         * @return a clock that uses the best available system clock in the
140         *         UTC zone
141         * @throws java.lang.NullPointerException if the given {@code zone} is
142         *         {@code null}
143         */
144        public static NanoClock systemUTC() {
145                return UTC_INSTANCE;
146        }
147
148        /**
149         * This clock is based on the <i>nano</i> system clock. It uses
150         * {@link System#nanoTime()} resolution
151         * <p>
152         * Conversion from instant to date or time uses the specified time-zone.
153         * <p>
154         * The returned implementation is immutable, thread-safe and
155         * {@code Serializable}.
156         *
157         * @return a clock that uses the best available system clock in the
158         *         default zone
159         * @throws java.lang.NullPointerException if the given {@code zone} is
160         *         {@code null}
161         */
162        public static NanoClock systemDefaultZone() {
163                return DEFAULT_INSTANCE;
164        }
165
166}