/*
 * Copyright (c) 2012 Philipp Meinen <philipp@bind.ch>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software
 * is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
 * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package ch.bind.philib.util;

import ch.bind.philib.math.Calc;

/** A simple counter where values can be added or the whole counter be reset. */
public final class Counter {

	private final String name;

	private long counts;

	private long total;

	private long min = -1;

	private long max = -1;

	// TODO
	// private double _50percent;
	// private double _80percent;
	// private double _90percent;
	// private double _95percent;
	// private double _99percent;
	// private double _999promile;
	// private double _999percent

	Counter(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void count(long value) {
		if (value < 0) {
			return;
		}
		synchronized (this) {
			if (counts == 0) {
				counts = 1;
				min = value;
				max = value;
				total = value;
			}
			else {
				counts++;
				min = Math.min(min, value);
				max = Math.max(max, value);
				total = Calc.unsignedAdd(total, value);
			}
		}
	}

	public synchronized void reset() {
		counts = 0;
		total = 0;
		min = -1;
		max = -1;
	}

	@Override
	public String toString() {
		long c, mi, ma, to;
		synchronized (this) {
			c = counts;
			mi = min;
			ma = max;
			to = total;
		}

		if (c == 0) {
			return String.format("%s[counts=0, total=0, min=N/A, max=N/A, avg=N/A]", name);
		}
		double avg = ((double) to) / c;
		return String.format("%s[counts=%d, total=%d, min=%d, max=%d, avg=%.3f]", name, c, to, mi, ma, avg);
	}
}

@@ -1164,16 +1164,106 @@
 .util;%0A%0A
+import java.util.concurrent.locks.Lock;%0Aimport java.util.concurrent.locks.ReentrantLock;%0A%0A
 import c
@@ -1287,16 +1287,16 @@
 h.Calc;%0A
-
 %0A/** A s
@@ -1527,236 +1527,8 @@
 1;%0A%0A
-%09// TODO%0A%09// private double _50percent;%0A%09// private double _80percent;%0A%09// private double _90percent;%0A%09// private double _95percent;%0A%09// private double _99percent;%0A%09// private double _999promile;%0A%09// private double _999percent%0A%0A
 %09Cou
@@ -1690,33 +1690,32 @@
 %7D%0A%09%09synchronized
- 
 (this) %7B%0A%09%09%09if (
@@ -1714,42 +1714,43 @@
 %0A%09%09%09
-if (counts == 0) %7B%0A%09%09%09%09counts = 1;
+long c = counts++;%0A%09%09%09if (c == 0) %7B
 %0A%09%09%09
@@ -1804,16 +1804,16 @@
 e;%0A%09%09%09%7D%0A
+
 %09%09%09else 
@@ -1818,22 +1818,8 @@
 e %7B%0A
-%09%09%09%09counts++;%0A
 %09%09%09%09

simplified counting