All Downloads are FREE. Search and download functionalities are using the official Maven repository.

clime.messadmin.model.stats.MinMaxTracker Maven / Gradle / Ivy

Go to download

Notification system and Session administration for J2EE Web Applications

There is a newer version: 4.1.1
Show newest version
/**
 * 
 */
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;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy