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

com.yammer.metrics.core.Counter Maven / Gradle / Ivy

There is a newer version: 3.0.0-BETA1
Show newest version
package com.yammer.metrics.core;

import java.util.concurrent.atomic.AtomicLong;

/**
 * An incrementing and decrementing counter metric.
 */
public class Counter implements Metric {
    private final AtomicLong count;

    Counter() {
        this.count = new AtomicLong(0);
    }

    /**
     * Increment the counter by one.
     */
    public void inc() {
        inc(1);
    }

    /**
     * Increment the counter by {@code n}.
     *
     * @param n the amount by which the counter will be increased
     */
    public void inc(long n) {
        count.addAndGet(n);
    }

    /**
     * Decrement the counter by one.
     */
    public void dec() {
        dec(1);
    }

    /**
     * Decrement the counter by {@code n}.
     *
     * @param n the amount by which the counter will be increased
     */
    public void dec(long n) {
        count.addAndGet(0 - n);
    }

    /**
     * Returns the counter's current value.
     *
     * @return the counter's current value
     */
    public long count() {
        return count.get();
    }

    /**
     * Resets the counter to 0.
     */
    public void clear() {
        count.set(0);
    }

    @Override
    public  void processWith(MetricProcessor processor, MetricName name, T context) throws Exception {
        processor.processCounter(name, this, context);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy