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

com.addc.commons.statistcs.collector.Counter Maven / Gradle / Ivy

package com.addc.commons.statistcs.collector;

import com.addc.commons.Mutex;
import com.addc.commons.jmx.JmxBeanImpl;

/**
 * The Counter supplies a statistcs collector that will count events.
 * 
 */
public class Counter extends JmxBeanImpl implements CounterMBean {

    private long count;
    private final String name;
    private final Mutex lock= new Mutex();

    /**
     * Create new Counter
     * @param name The unique name of the counter
     */
    public Counter(String name) {
        super();
        this.name= name;
    }

    /**
     * Increment the counter
     */
    public void increment() {
        synchronized (lock) {
            count++;
        }
    }

    /**
     * Increment the counter by by
     * @param by The amount to increment
     */
    public void increment(long by) {
        synchronized (lock) {
            count+= by;
        }
    }

    @Override
    public long getCount() {
        long ret;
        synchronized (lock) {
            ret= count;
        }
        return ret;
    }

    @Override
    public String getName() {
        return name;
    }

    /**
     * Clear the accumulator setting counters and totals to 0
     */
    public void clear() {
        synchronized(lock) {
            count= 0L;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy