package me.dslztx.assist.util.metric;

import java.util.HashMap;
import java.util.Map;

public class TimerAssist {

    private static ThreadLocal<Map<String, TimerInterval>> timer = new ThreadLocal<Map<String, TimerInterval>>() {
        public Map<String, TimerInterval> initialValue() {
            return new HashMap<String, TimerInterval>();
        }
    };

    public static void timerStart(String name) {
        Map<String, TimerInterval> timerDetail = timer.get();

        if (timerDetail.get(name) == null) {
            timerDetail.put(name, new TimerInterval());
        }

        timerDetail.get(name).start();
    }

    public static void timerStop(String name) {
        Map<String, TimerInterval> timerDetail = timer.get();

        if (timerDetail.get(name) == null) {
            timerDetail.put(name, new TimerInterval());
        }

        timerDetail.get(name).stop();
    }

    public static long timerValueAndClear(String name) {
        Map<String, TimerInterval> timerDetail = timer.get();

        if (timerDetail.get(name) == null) {
            return 0L;
        }

        Long value = timerDetail.get(name).value();

        timerDetail.remove(name);

        return value;
    }

}

class TimerInterval {
    long start = -1;
    long stop = -1;

    public void start() {
        this.start = System.currentTimeMillis();
    }

    public void stop() {
        this.stop = System.currentTimeMillis();
    }

    public Long value() {
        if (start == -1 || stop == -1) {
            return null;
        } else {
            return stop - start;
        }
    }
}

@@ -888,32 +888,74 @@
 .stop();%0A    %7D%0A%0A
+    /**%0A     * %E8%8E%B7%E5%8F%96%E5%80%BC%E5%B9%B6clear%E7%89%88%E6%9C%AC%EF%BC%8C%E9%98%B2%E6%AD%A2%E5%86%85%E5%AD%98%E6%B3%84%E6%BC%8F%0A     */%0A
     public stati
@@ -987,32 +987,32 @@
 (String name) %7B%0A
-
         Map%3CStri
@@ -1145,17 +1145,17 @@
         
-L
+l
 ong valu
@@ -1254,16 +1254,259 @@
 %0A    %7D%0A%0A
+    public static long timerValue(String name) %7B%0A        Map%3CString, TimerInterval%3E timerDetail = timer.get();%0A%0A        if (timerDetail.get(name) == null) %7B%0A            return 0L;%0A        %7D%0A%0A        return timerDetail.get(name).value();%0A    %7D%0A
 %7D%0A%0Aclass
@@ -1737,17 +1737,17 @@
  public 
-L
+l
 ong valu
@@ -1812,20 +1812,18 @@
  return 
-null
+0L
 ;%0A      

add timerValue