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