package com.levelup.java.date;

import static org.junit.Assert.assertTrue;

import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.time.StopWatch;
import org.apache.log4j.Logger;
import org.junit.Test;

import com.google.common.base.Stopwatch;

/**
 * This java snippet will demonstrate calculating elapsed time in java.
 * 
 * @author Justin Musgrove
 * @see <a href='http://www.leveluplunch.com/java/'></a>
 * 
 */
public class MeasureElapsedTime {

	// keywords = calculate date and time difference,

	private static final Logger logger = Logger.getLogger(MeasureElapsedTime.class);
	
	@Test
	public void calculate_elapsed_time_in_java () {
		
		long startTime = System.currentTimeMillis();
		
		// do something
		
		long estimatedTime = System.currentTimeMillis() - startTime;

		logger.info("time: " + estimatedTime); // <time: 4.000>

		assertTrue(estimatedTime >= 0);
	}
	
	@Test
	public void calculate_elapsed_time_nano_in_java () {
		
		long startTime = System.nanoTime();

		// do something
		
		long estimatedTime = System.nanoTime() - startTime;

		logger.info("time: " + estimatedTime); // <time: 4.000>
		
		assertTrue(estimatedTime >= 0);
	}
	
	@Test
	public void calculate_elapsed_time_in_guava() {
		Stopwatch stopwatch = Stopwatch.createStarted();
		stopwatch.stop(); // optional

		long millis = stopwatch.elapsed(TimeUnit.MILLISECONDS);

		logger.info("time: " + stopwatch); // <time: 4.000 μs>
		
		assertTrue(millis >= 0);

	}

	@Test
	public void calculate_elapsed_time_in_apache_commons () {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		
		// do something
		
		stopWatch.stop();
		long millis = stopWatch.getNanoTime();

		logger.info("time: " + millis); // <time: 4.000>
		
		assertTrue(millis >= 0);
		
			
	}

}

@@ -642,32 +642,60 @@
 _time_in_java ()
+ throws InterruptedException
  %7B%0A%09%09%0A%09%09long sta
@@ -732,39 +732,57 @@
 lis();%0A%09%09%0A%09%09
-// do something
+Thread.sleep(2); // simulate work
 %0A%09%09%0A%09%09long e
@@ -993,16 +993,44 @@
 _java ()
+ throws InterruptedException
  %7B%0A%09%09%0A%09%09
@@ -1068,31 +1068,49 @@
 e();%0A%0A%09%09
-// do something
+Thread.sleep(2); // simulate work
 %0A%09%09%0A%09%09lo
@@ -1314,17 +1314,46 @@
 guava() 
-%7B
+throws InterruptedException %7B%0A
 %0A%09%09Stopw
@@ -1392,24 +1392,64 @@
 eStarted();%0A
+%09%09%0A%09%09Thread.sleep(2); // simulate work%0A%0A
 %09%09stopwatch.
@@ -1683,16 +1683,44 @@
 mmons ()
+ throws InterruptedException
  %7B%0A%09%09Sto
@@ -1785,23 +1785,41 @@
 %09%0A%09%09
-// do something
+Thread.sleep(2); // simulate work
 %0A%09%09%0A
@@ -1966,16 +1966,754 @@
 );%0A%09
+%7D%0A
 %09%0A%09
-%09%09
+@Test%0A%09public void calculate_elapsed_time_in_spring () throws InterruptedException %7B%0A%09%09org.springframework.util.StopWatch stopWatch = new org.springframework.util.StopWatch();%0A%09%09%0A%09%09stopWatch.start(%22step 1%22);%0A%09%09Thread.sleep(2); // simulate work%0A%09%09stopWatch.stop();%0A%0A%09%09stopWatch.start(%22step 2%22);%0A%09%09Thread.sleep(5); // simulate work%0A%09%09stopWatch.stop();%0A%0A%09%09stopWatch.start(%22step 3%22);%0A%09%09Thread.sleep(3); // simulate work%0A%09%09stopWatch.stop();%0A%0A%09%09logger.info(%22time: %22 + stopWatch.prettyPrint()); %0A%09%09%0A//%09%09-----------------------------------------%0A//%09%09ms     %25     Task name%0A//%09%09-----------------------------------------%0A//%09%0902001  020%25  step 1%0A//%09%0905001  050%25  step 2%0A//%09%0903001  030%25  step 3%0A%09%09%0A%09%09assertTrue(stopWatch.getTotalTimeMillis() %3E= 0);
 %0A%09%7D
+%0A%09
 %0A%0A%7D%0A

add spring stop watch