clime.messadmin.model.stats.MinMaxTracker Maven / Gradle / Ivy
/**
*
*/
package clime.messadmin.model.stats;
import java.io.Serializable;
import java.util.Date;
/**
* Used for collecting data and building basic statistics.
*
* @author Cédrik LIME
*/
public class MinMaxTracker extends HitsCounter implements Serializable {
protected volatile long lastValue = 0;
protected volatile long min = Long.MAX_VALUE;
/** The time this object was updated for a min */
protected volatile long minAccessTime = 0;
protected volatile long max = Long.MIN_VALUE;
/** The time this object was updated for a max */
protected volatile long maxAccessTime = 0;
public MinMaxTracker() {
super();
}
public MinMaxTracker(long min, long max, long current) {
super();
this.min = min;
this.max = max;
this.lastValue = current;
}
public void addValue(long value) {
addValue(value, System.currentTimeMillis());
}
/** Calculate aggregate stats (min, max, etc.) */
public void addValue(long value, long currentTimeMillis) {
lastValue += value;
registerValue(lastValue, currentTimeMillis);
}
public void registerValue(long value) {
registerValue(value, System.currentTimeMillis());
}
/** Calculate aggregate stats (min, max, etc.) */
public void registerValue(long value, long currentTimeMillis) {
lastValue = value;
if (value < min) {
min = value;
minAccessTime = currentTimeMillis;
}
if (value > max) {
max = value;
maxAccessTime = currentTimeMillis;
}
super.hit(currentTimeMillis);
}
public long getLastValue() {
return lastValue;
}
public long getMin() {
return min;
}
public long getMax() {
return max;
}
public Date getMinAccessTime() {
return new Date(minAccessTime);
}
public Date getMaxAccessTime() {
return new Date(maxAccessTime);
}
protected StringBuffer toStringBuffer() {
StringBuffer buffer = super.toStringBuffer().append(',');
buffer.append("lastValue=").append(getLastValue()).append(',');
buffer.append("min=").append(getMin()).append(',');
buffer.append("minAccessTime=").append(getMinAccessTime()).append(',');
buffer.append("max=").append(getMax()).append(',');
buffer.append("maxAccessTime=").append(getMaxAccessTime());
return buffer;
}
}