/**
 * Copyright 2013-present febit.org (support@febit.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.febit.util;

import java.io.Closeable;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author zqq90
 */
public class Stopwatch implements Closeable, AutoCloseable {

    public static Stopwatch startNew() {
        return new Stopwatch().start();
    }

    private boolean isStarted;
    private long startNanos;
    private long elapsedNanos;

    public Stopwatch() {
    }

    public boolean isRunning() {
        return isStarted;
    }

    public Stopwatch start() {
        if (isStarted) {
            throw new IllegalStateException("Stopwatch is already running.");
        }
        isStarted = true;
        startNanos = System.nanoTime();
        return this;
    }

    public Stopwatch stop() {
        if (!isStarted) {
            throw new IllegalStateException("Stopwatch is already stopped.");
        }
        isStarted = false;
        elapsedNanos += System.nanoTime() - startNanos;
        return this;
    }

    public Stopwatch reset() {
        elapsedNanos = 0;
        isStarted = false;
        return this;
    }

    public Stopwatch restart() {
        return reset().start();
    }

    public long now() {
        return isStarted
                ? System.nanoTime() - startNanos + elapsedNanos
                : elapsedNanos;
    }

    public long now(TimeUnit timeUnit) {
        return timeUnit.convert(now(), TimeUnit.NANOSECONDS);
    }

    public long nowInDays() {
        return now(TimeUnit.DAYS);
    }

    public long nowInHours() {
        return now(TimeUnit.HOURS);
    }

    public long nowInMinutes() {
        return now(TimeUnit.MINUTES);
    }

    public long nowInSeconds() {
        return now(TimeUnit.SECONDS);
    }

    public long nowInMillis() {
        return now(TimeUnit.MILLISECONDS);
    }

    public long nowInMicros() {
        return now(TimeUnit.MICROSECONDS);
    }

    public long nowInNanos() {
        return now();
    }

    @Override
    public String toString() {
        return Long.toString(nowInNanos());
    }

    @Override
    public void close() {
        if (isStarted) {
            stop();
        }
    }
}

@@ -644,34 +644,8 @@
 l;%0A%0A
-import java.io.Closeable;%0A
 impo
@@ -734,44 +734,8 @@
 tch 
-implements Closeable, AutoCloseable 
 %7B%0A%0A 
@@ -1496,32 +1496,128 @@
 rn this;%0A    %7D%0A%0A
+    public void stopIfRunning() %7B%0A        if (isStarted) %7B%0A            stop();%0A        %7D%0A    %7D%0A%0A
     public Stopw
@@ -2672,107 +2672,6 @@
  %7D%0A%0A
-    @Override%0A    public void close() %7B%0A        if (isStarted) %7B%0A            stop();%0A        %7D%0A    %7D%0A
 %7D%0A

update Stopwatch, remove close method, implement of Closeable or AutoCloseable means close call must be made in a finally block