001 /*
002 * Java Genetic Algorithm Library (jenetics-6.3.0).
003 * Copyright (c) 2007-2021 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 */
020 package io.jenetics.util;
021
022 import static java.util.Objects.requireNonNull;
023
024 import java.io.Serializable;
025 import java.time.Clock;
026 import java.time.Instant;
027 import java.time.ZoneId;
028 import java.time.ZoneOffset;
029
030 /**
031 * Clock implementation with <i>nano</i> second precision.
032 *
033 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
034 * @since 3.1
035 * @version 3.1
036 */
037 public final class NanoClock extends Clock implements Serializable {
038
039 private static final long serialVersionUID = 1L;
040
041 private static final long EPOCH_NANOS = System.currentTimeMillis()*1_000_000;
042 private static final long NANO_START = System.nanoTime();
043 private static final long OFFSET_NANOS = EPOCH_NANOS - NANO_START;
044
045 private static final NanoClock UTC_INSTANCE = new NanoClock(ZoneOffset.UTC);
046
047 private static final NanoClock DEFAULT_INSTANCE =
048 new NanoClock(ZoneId.systemDefault());
049
050 /**
051 * This constants holds the number of nano seconds of one second.
052 */
053 public static final long NANOS_PER_SECOND = 1_000_000_000;
054
055 private final ZoneId _zone;
056
057 private NanoClock(final ZoneId zone) {
058 _zone = requireNonNull(zone, "zone");
059 }
060
061 @Override
062 public ZoneId getZone() {
063 return _zone;
064 }
065
066 @Override
067 public NanoClock withZone(final ZoneId zone) {
068 return zone.equals(_zone) ? this : new NanoClock(zone);
069 }
070
071 @Override
072 public long millis() {
073 return System.currentTimeMillis();
074 }
075
076 /**
077 * This returns the nanosecond-based instant, measured from
078 * 1970-01-01T00:00Z (UTC). This method will return valid values till the
079 * year 2262.
080 *
081 * @return the nanosecond-based instant, measured from 1970-01-01T00:00Z (UTC)
082 */
083 public long nanos() {
084 return System.nanoTime() + OFFSET_NANOS;
085 }
086
087 @Override
088 public Instant instant() {
089 final long now = nanos();
090 return Instant.ofEpochSecond(now/NANOS_PER_SECOND, now%NANOS_PER_SECOND);
091 }
092
093 @Override
094 public int hashCode() {
095 return _zone.hashCode() + 11;
096 }
097
098 @Override
099 public boolean equals(final Object obj) {
100 return obj == this ||
101 obj instanceof NanoClock &&
102 ((NanoClock)obj)._zone.equals(_zone);
103 }
104
105 @Override
106 public String toString() {
107 return "NanoClock[" + _zone + "]";
108 }
109
110 /**
111 * This clock is based on the <i>nano</i> system clock. It uses
112 * {@link System#nanoTime()} resolution
113 * <p>
114 * Conversion from instant to date or time uses the specified time-zone.
115 * <p>
116 * The returned implementation is immutable, thread-safe and
117 * {@code Serializable}.
118 *
119 * @param zone the time-zone to use to convert the instant to date-time
120 * @return a clock that uses the best available system clock in the
121 * specified zone
122 * @throws java.lang.NullPointerException if the given {@code zone} is
123 * {@code null}
124 */
125 public static NanoClock system(final ZoneId zone) {
126 return new NanoClock(zone);
127 }
128
129 /**
130 * This clock is based on the <i>nano</i> system clock. It uses
131 * {@link System#nanoTime()} resolution
132 * <p>
133 * Conversion from instant to date or time uses the specified time-zone.
134 * <p>
135 * The returned implementation is immutable, thread-safe and
136 * {@code Serializable}.
137 *
138 * @return a clock that uses the best available system clock in the
139 * UTC zone
140 * @throws java.lang.NullPointerException if the given {@code zone} is
141 * {@code null}
142 */
143 public static NanoClock systemUTC() {
144 return UTC_INSTANCE;
145 }
146
147 /**
148 * This clock is based on the <i>nano</i> system clock. It uses
149 * {@link System#nanoTime()} resolution
150 * <p>
151 * Conversion from instant to date or time uses the specified time-zone.
152 * <p>
153 * The returned implementation is immutable, thread-safe and
154 * {@code Serializable}.
155 *
156 * @return a clock that uses the best available system clock in the
157 * default zone
158 * @throws java.lang.NullPointerException if the given {@code zone} is
159 * {@code null}
160 */
161 public static NanoClock systemDefaultZone() {
162 return DEFAULT_INSTANCE;
163 }
164
165 }
|