package org.metaborg.util.time;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;

/**
 * Simple time measurement functionality.
 */
public class Timer {
	/** ThreadMXBean for measuring CPU time. **/
	private final ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();

	/** If precise CPU time measurements are available. **/
	private final boolean canLogCPUTime;

	/** Last starting time since start was called. **/
	private long startTime = 0;

	
	public Timer() {
	    this(false);
	}
	
	public Timer(boolean start) {
		canLogCPUTime = mxBean.isThreadCpuTimeSupported();
		if(canLogCPUTime)
			mxBean.setThreadCpuTimeEnabled(true);
		if(start)
		    start();
	}


	public void start() {
		startTime = time();
	}

	public long stop() {
		return time() - startTime;
	}

	public void reset() {
		startTime = 0;
	}

	private long time() {
		if(canLogCPUTime)
			return mxBean.getCurrentThreadCpuTime();
		else
			return System.nanoTime();
	}
}

@@ -187,17 +187,20 @@
 Timer %7B%0A
-%09
+    
 /** Thre
@@ -232,25 +232,28 @@
 U time. **/%0A
-%09
+    
 private fina
@@ -314,17 +314,20 @@
 ean();%0A%0A
-%09
+    
 /** If p
@@ -370,25 +370,28 @@
 ilable. **/%0A
-%09
+    
 private fina
@@ -416,17 +416,20 @@
 UTime;%0A%0A
-%09
+    
 /** Last
@@ -471,17 +471,20 @@
 ed. **/%0A
-%09
+    
 private 
@@ -504,19 +504,21 @@
 e = 0;%0A%0A
-%09%0A%09
+%0A    
 public T
@@ -526,17 +526,20 @@
 mer() %7B%0A
-%09
+    
     this
@@ -551,14 +551,19 @@
 e);%0A
-%09%7D%0A%09%0A%09
+    %7D%0A%0A    
 publ
@@ -588,18 +588,24 @@
 tart) %7B%0A
-%09%09
+        
 canLogCP
@@ -647,18 +647,24 @@
 rted();%0A
-%09%09
+        
 if(canLo
@@ -673,19 +673,28 @@
 PUTime)%0A
-%09%09%09
+            
 mxBean.s
@@ -723,18 +723,24 @@
 (true);%0A
-%09%09
+        
 if(start
@@ -741,18 +741,24 @@
 (start)%0A
-%09%09
+        
     star
@@ -766,14 +766,86 @@
 ();%0A
-%09%7D%0A%0A%0A%09
+    %7D%0A%0A%0A    /**%0A     * Starts the timer, noting the current time.%0A     */%0A    
 publ
@@ -858,26 +858,32 @@
 d start() %7B%0A
-%09%09
+        
 startTime = 
@@ -890,21 +890,241 @@
 time();%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    /**%0A     * @return The duration, in nanoseconds, between the call to %7B@link #start()%7D and this invocation. This method can%0A     *         be called multiple times after one %7B@link #start()%7D invocation.%0A     */%0A    
 public l
@@ -1136,18 +1136,24 @@
 top() %7B%0A
-%09%09
+        
 return t
@@ -1171,21 +1171,128 @@
 rtTime;%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    /**%0A     * Resets the timer, forgetting the time noted when %7B@link #start()%7D was called.%0A     */%0A    
 public v
@@ -1305,18 +1305,24 @@
 set() %7B%0A
-%09%09
+        
 startTim
@@ -1332,13 +1332,20 @@
  0;%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A%0A    
 priv
@@ -1362,18 +1362,24 @@
 ime() %7B%0A
-%09%09
+        
 if(canLo
@@ -1388,19 +1388,28 @@
 PUTime)%0A
-%09%09%09
+            
 return m
@@ -1445,18 +1445,33 @@
 ();%0A
-%09%09else%0A%09%09%09
+        else%0A            
 retu
@@ -1496,9 +1496,12 @@
 ();%0A
-%09
+    
 %7D%0A%7D%0A

Add some documentation.