001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  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 */
017package org.apache.camel.util;
018
019import java.time.Duration;
020
021/**
022 * A very simple stop watch.
023 * <p/>
024 * This implementation is not thread safe and can only time one task at any given time.
025 */
026public final class StopWatch {
027
028    private long start;
029
030    /**
031     * Starts the stop watch
032     */
033    public StopWatch() {
034        this.start = System.nanoTime();
035    }
036
037    /**
038     * Starts the stop watch from the given timestamp
039     */
040    public StopWatch(long timeMillis) {
041        start = timeMillis;
042    }
043
044    /**
045     * Creates the stop watch
046     *
047     * @param start whether it should start immediately
048     */
049    public StopWatch(boolean start) {
050        if (start) {
051            this.start = System.nanoTime();
052        }
053    }
054
055    /**
056     * Starts or restarts the stop watch
057     */
058    public void restart() {
059        start = System.nanoTime();
060    }
061
062    /**
063     * Whether the watch is started
064     */
065    public boolean isStarted() {
066        return start > 0;
067    }
068
069    /**
070     * Returns the time taken in millis.
071     *
072     * @return time in millis, or <tt>0</tt> if not started yet.
073     */
074    public long taken() {
075        if (start > 0) {
076            long delta = System.nanoTime() - start;
077            return Duration.ofNanos(delta).toMillis();
078        }
079        return 0;
080    }
081
082    /**
083     * Returns the time taken in millis and restarts the timer.
084     *
085     * @return time in millis, or <tt>0</tt> if not started yet.
086     */
087    public long takenAndRestart() {
088        long answer = taken();
089        start = System.nanoTime();
090        return answer;
091    }
092
093    /**
094     * Utility method to provide the elapsed time using milliseconds since epoch. This serves as an alternative for the
095     * former constructor based on a Date argument. This should be used only when converting old code that relies on
096     * that constructor as it can provide incorrect measurements in rare circumstances
097     *
098     * @param  start the timestamp in milliseconds since epoch
099     * @return       the elapsed time in milliseconds
100     */
101    @Deprecated
102    public static long elapsedMillisSince(long start) {
103        return Duration.ofMillis(System.currentTimeMillis()).minusMillis(start).toMillis();
104    }
105
106}