001/* 002 * Java Genetic Algorithm Library (jenetics-8.1.0). 003 * Copyright (c) 2007-2024 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 == this || 103 obj instanceof NanoClock other && 104 other._zone.equals(_zone); 105 } 106 107 @Override 108 public String toString() { 109 return "NanoClock[" + _zone + "]"; 110 } 111 112 /** 113 * This clock is based on the <i>nano</i> system clock. It uses 114 * {@link System#nanoTime()} resolution 115 * <p> 116 * Conversion from instant to date or time uses the specified time-zone. 117 * <p> 118 * The returned implementation is immutable, thread-safe and 119 * {@code Serializable}. 120 * 121 * @param zone the time-zone to use to convert the instant to date-time 122 * @return a clock that uses the best available system clock in the 123 * specified zone 124 * @throws java.lang.NullPointerException if the given {@code zone} is 125 * {@code null} 126 */ 127 public static NanoClock system(final ZoneId zone) { 128 return new NanoClock(zone); 129 } 130 131 /** 132 * This clock is based on the <i>nano</i> system clock. It uses 133 * {@link System#nanoTime()} resolution 134 * <p> 135 * Conversion from instant to date or time uses the specified time-zone. 136 * <p> 137 * The returned implementation is immutable, thread-safe and 138 * {@code Serializable}. 139 * 140 * @return a clock that uses the best available system clock in the 141 * UTC zone 142 * @throws java.lang.NullPointerException if the given {@code zone} is 143 * {@code null} 144 */ 145 public static NanoClock systemUTC() { 146 return UTC_INSTANCE; 147 } 148 149 /** 150 * This clock is based on the <i>nano</i> system clock. It uses 151 * {@link System#nanoTime()} resolution 152 * <p> 153 * Conversion from instant to date or time uses the specified time-zone. 154 * <p> 155 * The returned implementation is immutable, thread-safe and 156 * {@code Serializable}. 157 * 158 * @return a clock that uses the best available system clock in the 159 * default zone 160 * @throws java.lang.NullPointerException if the given {@code zone} is 161 * {@code null} 162 */ 163 public static NanoClock systemDefaultZone() { 164 return DEFAULT_INSTANCE; 165 } 166 167}