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

com.xqbase.metric.common.Metric 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;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

public class Metric {
	private static ConcurrentHashMap
			map = new ConcurrentHashMap<>();

	private static void put(MetricKey key, double value) {
		MetricValue current;
		do {
			current = map.get(key);
		} while (current == null ? map.putIfAbsent(key, new MetricValue(value)) != null :
				!map.replace(key, current, new MetricValue(current, value)));
	}

	public static void put(String name, double value, HashMap tagMap) {
		put(new MetricKey(name, tagMap), value);
	}

	public static void put(String name, double value, String... tagPairs) {
		put(new MetricKey(name, tagPairs), value);
	}

	public static ArrayList removeAll() {
		ArrayList metrics = new ArrayList<>();
		ArrayList keys = new ArrayList<>(map.keySet());
		for (MetricKey key : keys) {
			MetricValue value = map.remove(key);
			if (value != null) {
				metrics.add(new MetricEntry(key, value));
			}
		}
		return metrics;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy