package com.eactive.eai.adapter.socket2.common;


public class StopWatch {
    private long startTime = -1;
    private long stopTime = -1;
    private final int HIM = 60 * 60 * 1000;
    private final int MIM = 60 * 1000;
    
    public StopWatch() {
    	super();
    }

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

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

    public void reset() {
        this.startTime = -1;
        this.stopTime = -1;
    }

    public long getTime() {
        if (stopTime == -1) {
            return (System.currentTimeMillis() - this.startTime);
        } else {
            return (this.stopTime - this.startTime);
        }
    }

    public String toString() {
        return getTimeString();
    }

    protected String getTimeString() {
        int hours;
        int minutes;
        int seconds;
        int milliseconds;
        long time = getTime();
        hours = (int) (time / HIM);
        time = time - (hours * HIM);
        minutes = (int) (time / MIM);
        time = time - (minutes * MIM);
        seconds = (int) (time / 1000);
        time = time - (seconds * 1000);
        milliseconds = (int) time;

        return hours + "h:" + minutes + "m:" + seconds + "s:" + milliseconds + "ms";
    }

//	   public static void main(String args[]) throws Exception {
//	   test();
//}

// private static void test() throws Exception {
//        StopWatch stopWatch = new StopWatch();
//        stopWatch.start();
//
//        try {
//            Thread.currentThread().sleep(1500);
//        } catch (InterruptedException ie) {
//            System.out.println("error");
//        }
//
//        stopWatch.stop();
//        System.out.println(stopWatch);
//    }
}
