package com.devicehive.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Calendar;

/**
 * This class is used to log execution time of methods
 */
public class Timer {

    private long start;

    private static final Logger logger = LoggerFactory.getLogger(Timer.class);

    private static final int MAX_METHOD_EXECUTION_TIME = 50000000; // 50ms

    /**
     * Creates new instance
     */
    public static Timer newInstance() {
        return new Timer();
    }

    private Timer() {
        start = System.nanoTime();
    }

    /**
     * @return number of milliseconds, passed from creation, or reset call
     */
    public long click() {
        return System.nanoTime() - start;
    }

    /**
     * reset counter
     */
    public long reset() {
        long end = Calendar.getInstance().getTimeInMillis() - start;
        start = Calendar.getInstance().getTimeInMillis();
        return end;
    }

    /**
     * logs message like "Execution of methodName  took 100 milliseconds"
     * if execution time is less or equal MAX_METHOD_EXECUTION_TIME will log with debug priority,
     * Will log with warning priority otherwise
     *
     * @param methodName executed method name
     */
    public void logMethodExecuted(String methodName) {
        long time = click() / 1000;
        String formatted = new DecimalFormat("###,###").format(time);
        if (time > MAX_METHOD_EXECUTION_TIME) {
            logger.warn("Execution of {} took {} microseconds", methodName, formatted);
        }else{
            logger.debug("Execution of {} took {} microseconds", methodName, formatted);
        }

    }

}

@@ -1389,78 +1389,8 @@
 00;%0A
-        String formatted = new DecimalFormat(%22###,###%22).format(time);%0A
     
@@ -1505,33 +1505,28 @@
 methodName, 
-formatted
+time
 );%0A        %7D
@@ -1529,12 +1529,14 @@
    %7D
+ 
 else
+ 
 %7B%0A  
@@ -1614,17 +1614,12 @@
 me, 
-formatted
+time
 );%0A 

Format is removed