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

org.gridkit.jvmtool.event.SimpleCounterCollection Maven / Gradle / Ivy

There is a newer version: 0.23
Show newest version
package org.gridkit.jvmtool.event;

import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import org.gridkit.jvmtool.stacktrace.CounterCollection;

public class SimpleCounterCollection implements CounterCollection {

    private Map counters = new TreeMap();

    @Override
    public Iterator iterator() {
        return counters.keySet().iterator();
    }

    @Override
    public long getValue(String key) {
        Long n  = counters.get(key);
        return  n == null ? Long.MIN_VALUE : n;
    }

    public void set(String key, long value) {
        counters.put(key, value);
    }

    public void setAll(CounterCollection that) {
        for(String key: that) {
            set(key, that.getValue(key));
        }
    }

    public SimpleCounterCollection clone() {
        try {
            SimpleCounterCollection that = (SimpleCounterCollection) super.clone();
            that.counters = new TreeMap(counters);
            return that;
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }

    public void clear() {
        counters.clear();
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for(String key: this) {
            if (sb.length() > 1) {
                sb.append(", ");
            }
            sb.append(key).append(": ").append(getValue(key));
        }
        sb.append(']');
        return sb.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy