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

com.xqbase.metric.common.MetricValue Maven / Gradle / Ivy

Go to download

a lightweight metric framework for aggregating, collecting and showing metric data - common part

There is a newer version: 0.2.13
Show newest version
package com.xqbase.metric.common;

public class MetricValue implements Cloneable {
	private long count;
	private double sum, max, min, sqr;

	public MetricValue(long count, double sum, double max, double min, double sqr) {
		this.count = count;
		this.sum = sum;
		this.max = max;
		this.min = min;
		this.sqr = sqr;
	}

	public MetricValue(double value) {
		this(1, value, value, value, value * value);
	}

	public MetricValue(MetricValue old, double value) {
		this(old.count + 1, old.sum + value, Math.max(old.max, value),
				Math.min(old.min, value), old.sqr + value * value);
	}

	@Override
	public MetricValue clone() {
		return new MetricValue(count, sum, max, min, sqr);
	}

	public void add(MetricValue value) {
		count += value.count;
		sum += value.sum;
		max = Math.max(max, value.max);
		min = Math.min(min, value.min);
		sqr += value.sqr;
	}

	public long getCount() {
		return count;
	}

	public double getSum() {
		return sum;
	}

	public double getMax() {
		return max;
	}

	public double getMin() {
		return min;
	}

	public double getSqr() {
		return sqr;
	}

	public double getAvg() {
		return sum / count;
	}

	public double getStd() {
		if (count == 0) {
			return 0;
		}
		double base = sqr * count - sum * sum;
		return base < 0 ? 0 : Math.sqrt(base) / count;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy