Timer.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.5.0).
003  * Copyright (c) 2007-2016 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.engine;
021 
022 import static java.util.Objects.requireNonNull;
023 
024 import java.time.Clock;
025 import java.time.Duration;
026 import java.time.Instant;
027 import java.util.function.LongSupplier;
028 
029 import org.jenetics.util.NanoClock;
030 
031 /**
032  * Timer implementation for measuring execution durations.
033  *
034  * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
035  * @since 3.0
036  * @version 3.1
037  */
038 final class Timer {
039 
040     private final LongSupplier _nanoClock;
041 
042     private long _start;
043     private long _stop;
044 
045     private Timer(final LongSupplier nanoClock) {
046         _nanoClock = requireNonNull(nanoClock);
047     }
048 
049     /**
050      * Start the timer.
051      *
052      * @return {@code this} timer, for method chaining
053      */
054     public Timer start() {
055         _start = _nanoClock.getAsLong();
056         return this;
057     }
058 
059     /**
060      * Stop the timer.
061      *
062      * @return {@code this} timer, for method chaining
063      */
064     public Timer stop() {
065         _stop = _nanoClock.getAsLong();
066         return this;
067     }
068 
069     /**
070      * Return the duration between two consecutive {@link #start()} and
071      * {@link #stop()} calls.
072      *
073      * @return the duration between two {@code start} and {@code stop} calls
074      */
075     public Duration getTime() {
076         return Duration.ofNanos(_stop - _start);
077     }
078 
079     /**
080      * Return an new timer object which uses the given clock for measuring the
081      * execution time.
082      *
083      * @param clock the clock used for measuring the execution time
084      * @return a new timer
085      */
086     public static Timer of(final Clock clock) {
087         requireNonNull(clock);
088         return clock instanceof NanoClock ? of() : new Timer(() -> nanos(clock));
089     }
090 
091     private static long nanos(final Clock clock) {
092         final Instant now = clock.instant();
093         return now.getEpochSecond()*NanoClock.NANOS_PER_SECOND + now.getNano();
094     }
095 
096     /**
097      * Return an new timer object with the default clock implementation.
098      *
099      * @return a new timer
100      */
101     public static Timer of() {
102         return new Timer(System::nanoTime);
103     }
104 
105 }