/**
 * Easy timing-logging class by Luke Bryan
 * Similar to the JS console.time(), console.timeEnd().
 */

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.HashMap;

public class SmartTimer {
    
    private static HashMap<String, Long> times;
    private static File outFile;
    
    public SmartTimer(File logFile) {
        this.times = new HashMap<String, Long>();
        this.outFile = logFile;
    }
    
    public void start(String item)
    {
        times.put(item, System.currentTimeMillis());
    }
    
    public void end(String item)
    {
        if (times.containsKey(item))
        {
            long duration = System.currentTimeMillis() - times.get(item);
            //System.out.println(String.valueOf(duration) + " ms "+item);
            try {
                FileWriter writer = new FileWriter(outFile, true);
                BufferedWriter buffer = new BufferedWriter(writer);
                buffer.write(item + "\t" + times.get(item) + "\t" + System.currentTimeMillis() + "\n");
                buffer.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        } else {
            throw new InvalidParameterException();
        }
    }
}