package com.spiddekauga.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Measures how long things take.
 * @author Matteus Magnusson <matteus.magnusson@spiddekauga.com>
 */
public class Profiler {

	/**
	 * Start measuring the time. This automatically resets the profiler
	 */
	public void start() {
		reset();
		mStartTime = System.currentTimeMillis();
	}

	/**
	 * Stop measuring the time
	 */
	public void stop() {
		mEndTime = System.currentTimeMillis();
	}

	/**
	 * Add an intermediate stop
	 * @param name the name of the point
	 */
	public void addIntermediateTime(String name) {
		mIntermediateTimes.add(new TimeWrapper(name));
	}

	/**
	 * Get string with all information (used for printing)
	 * @param header optional header for the information (can be null)
	 * @return string with all information
	 */
	public String toString(String header) {
		StringBuilder stringBuilder = new StringBuilder();

		// Header
		if (header != null) {
			stringBuilder.append(header).append('\n');
		}


		// Total time
		long totalTime = mEndTime - mStartTime;
		stringBuilder.append("Total Time: ").append(totalTime).append("ms\n");


		// Intermediate times
		long previousTime = mStartTime;
		for (TimeWrapper intermediateTime : mIntermediateTimes) {
			// Time
			long diffTime = intermediateTime.time - previousTime;
			previousTime = intermediateTime.time;

			String diffTimeString = Strings.padRight(Long.toString(diffTime) + "ms", mPadding);
			stringBuilder.append(diffTimeString);

			// Percentage
			double percentageTime = ((double) diffTime) / totalTime;
			String percentageTimeString = String.format("%.2f", percentageTime);
			stringBuilder.append(percentageTimeString).append("%    ");

			// Name
			stringBuilder.append(intermediateTime.name).append("\n");
		}


		return stringBuilder.toString();
	}

	/**
	 * Same as {@link #toString(String)} with null as the header argument
	 */
	@Override
	public String toString() {
		return toString(null);
	}

	/**
	 * Reset the profiler
	 */
	public void reset() {
		mStartTime = 0;
		mEndTime = 0;
		mIntermediateTimes.clear();
	}

	/**
	 * Change the padding (total number of characters) in the intermediate times
	 * @param padding total number of characters in intermediate times before the name
	 */
	public void setPadding(int padding) {
		mPadding = padding;
	}

	/**
	 * Wrapper for time and name
	 */
	private static class TimeWrapper {
		private TimeWrapper(String name) {
			this.name = name;
		}

		private String name;
		private long time = System.currentTimeMillis();
	}

	private int mPadding = 10;
	private long mStartTime;
	private long mEndTime;
	private List<TimeWrapper> mIntermediateTimes = new ArrayList<>();
}

@@ -102,16 +102,21 @@
 ow long 
+time 
 things t
@@ -122,102 +122,178 @@
 take
-.
 %0A *
- @author Matteus Magnusson %3Cmatteus.magnusson@spiddekauga.com%3E%0A */%0Apublic class Profiler %7B
+/%0Apublic class Profiler %7B%0A%0Aprivate int mPadding = 10;%0Aprivate long mStartTime;%0Aprivate long mEndTime;%0Aprivate List%3CTimeWrapper%3E mIntermediateTimes = new ArrayList%3C%3E();
 %0A%0A%09/
@@ -451,24 +451,147 @@
 );%0A%09%7D%0A%0A%09/**%0A
+%09 * Reset the profiler%0A%09 */%0A%09public void reset() %7B%0A%09%09mStartTime = 0;%0A%09%09mEndTime = 0;%0A%09%09mIntermediateTimes.clear();%0A%09%7D%0A%0A/**%0A
 %09 * Stop mea
@@ -857,24 +857,172 @@
 );%0A%09%7D%0A%0A%09/**%0A
+%09 * Same as %7B@link #toString(String)%7D with null as the header argument%0A%09 */%0A%09@Override%0A%09public String toString() %7B%0A%09%09return toString(null);%0A%09%7D%0A%0A/**%0A
 %09 * Get stri
@@ -2185,281 +2185,8 @@
 %09%7D%0A%0A
-%09/**%0A%09 * Same as %7B@link #toString(String)%7D with null as the header argument%0A%09 */%0A%09@Override%0A%09public String toString() %7B%0A%09%09return toString(null);%0A%09%7D%0A%0A%09/**%0A%09 * Reset the profiler%0A%09 */%0A%09public void reset() %7B%0A%09%09mStartTime = 0;%0A%09%09mEndTime = 0;%0A%09%09mIntermediateTimes.clear();%0A%09%7D%0A%0A
 %09/**
@@ -2498,71 +2498,8 @@
 r %7B%0A
-%09%09private TimeWrapper(String name) %7B%0A%09%09%09this.name = name;%0A%09%09%7D%0A%0A
 %09%09pr
@@ -2568,19 +2568,16 @@
 lis();%0A%09
-%7D%0A%0A
 %09private
@@ -2581,142 +2581,61 @@
 ate 
-int mPadding = 10;%0A%09private long mStartTime;%0A%09private long mEndTime;%0A%09private List%3CTimeWrapper%3E mIntermediateTi
+TimeWrapper(String name) %7B%0A%09%09%09this.na
 me
-s
  = n
-ew ArrayList%3C%3E();
+ame;%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Fix javadoc