NanoClock.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.8.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@gmx.at)
019  */
020 package org.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 import org.jenetics.internal.util.Equality;
031 
032 /**
033  * Clock implementation with <i>nano</i> second precision.
034  *
035  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
036  @since 3.1
037  @version 3.1
038  */
039 public final class NanoClock extends Clock implements Serializable {
040 
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 
046     private static final NanoClock UTC_INSTANCE = new NanoClock(ZoneOffset.UTC);
047 
048     private static final NanoClock DEFAULT_INSTANCE =
049         new NanoClock(ZoneId.systemDefault());
050 
051     /**
052      * This constants holds the number of nano seconds of one second.
053      */
054     public static final long NANOS_PER_SECOND = 1_000_000_000;
055 
056     private final ZoneId _zone;
057 
058     private NanoClock(final ZoneId zone)  {
059         _zone = requireNonNull(zone, "zone");
060     }
061 
062     @Override
063     public ZoneId getZone() {
064         return _zone;
065     }
066 
067     @Override
068     public NanoClock withZone(final ZoneId zone) {
069         return zone.equals(_zonethis new NanoClock(zone);
070     }
071 
072     @Override
073     public long millis() {
074         return System.currentTimeMillis();
075     }
076 
077     /**
078      * This returns the nanosecond-based instant, measured from
079      * 1970-01-01T00:00Z (UTC). This method will return valid values till the
080      * year 2262.
081      *
082      @return the nanosecond-based instant, measured from 1970-01-01T00:00Z (UTC)
083      */
084     public long nanos() {
085         return System.nanoTime() - NANO_START + EPOCH_NANOS;
086     }
087 
088     @Override
089     public Instant instant() {
090         final long now = nanos();
091         return Instant.ofEpochSecond(now/NANOS_PER_SECOND, now%NANOS_PER_SECOND);
092     }
093 
094     @Override
095     public int hashCode() {
096         return _zone.hashCode() 11;
097     }
098 
099     @Override
100     public boolean equals(Object obj) {
101         return Equality.of(this, obj)
102             .test(clock -> _zone.equals(clock._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 }