package speiger.src.coreengine.utils.counters.averager;

import speiger.src.collections.longs.queues.LongArrayFIFOQueue;
import speiger.src.collections.longs.queues.LongPriorityQueue;

public class Averager {
	LongPriorityQueue values;
	long totalValue;
	int totalCount;
	
	public Averager() {
		this(200);
	}
	
	public Averager(int totalCount) {
		this.totalCount = totalCount;
		values = new LongArrayFIFOQueue(totalCount + 1);
	}
	
	public void addEntry(long time) {
		totalValue += time;
		if(values.size() >= totalCount) totalValue -= values.dequeue();
		values.enqueue(time);
	}
	
	public void clear() {
		values.clear();
		totalValue = 0L;
	}
	
	public long getAverage() { return (values.isEmpty() ? 0 : totalValue / values.size()); }
	public boolean isEmpty() { return values.isEmpty(); }
	public long getTotal() { return totalValue; }
	
	@Override
	public String toString() {
		return "Stamper[total="+totalValue+", average="+getAverage()+", count="+values.size()+", max="+totalCount+"]";
	}
}