LongDelta.java 1.46 KB
package se.lil.jm;

class LongDelta {
	public String name = null;
	public Long curValue = null;
	public Long lastValue = null;
	public Long lastFetch = null;
	public Long curFetch = null;
	
	public LongDelta() {
	}
	
	public LongDelta(Long timestamp, Long value) {
		this.curFetch = timestamp;
		this.curValue = value;
	}
	
	public void update(Long timestamp, Long value) {
		this.lastFetch = this.curFetch;
		this.curFetch = timestamp;
		this.lastValue = this.curValue;
		this.curValue = value;
	}
	public double getPerSecondValue() {
		if(this.curValue != null && this.lastValue != null && this.lastFetch != null && this.curFetch != null) {
			// We have values, calculate the number of gets per second
			double numMilliSeconds = this.curFetch - this.lastFetch;
			double delta = this.curValue - this.lastValue;
			double perMilli = delta/numMilliSeconds;
			return perMilli * 1000;
		} else {
			return 0;
		}
	}
	public long getDelta() {
		if(lastValue != null && curValue != null) {
			return curValue - lastValue;
		} else {
			return 0;
		}
	}
	public long getCurrentValue() {
		if(this.curValue != null) return this.curValue;
		else return 0;
	}
	public double getSeconds() {
		if(this.lastFetch != null && this.curFetch != null) {
			double numSeconds = this.curFetch - this.lastFetch;
			return numSeconds/1000;
		}
		return 0;
	}
	public long getMilliSeconds() {
		if(this.lastFetch != null && this.curFetch != null) {
			return this.curFetch - this.lastFetch;
		}
		return 0;
	}
}