/*
***************************************************************************************
* Copyright (C) 2006 EsperTech, Inc. All rights reserved. *
* http://www.espertech.com/esper *
* http://www.espertech.com *
* ---------------------------------------------------------------------------------- *
* The software in this package is published under the terms of the GPL license *
* a copy of which has been included with this distribution in the license.txt file. *
***************************************************************************************
*/
/*
***************************************************************************************
* Copyright (C) 2006 EsperTech, Inc. All rights reserved. *
* http://www.espertech.com/esper *
* http://www.espertech.com *
* ---------------------------------------------------------------------------------- *
* The software in this package is published under the terms of the GPL license *
* a copy of which has been included with this distribution in the license.txt file. *
***************************************************************************************
*/
/***************************************************************************************
* Attribution Notice
*
* This file is imported from Metrics (https://github.com/codahale/metrics subproject metrics-core).
* Metrics is Copyright (c) 2010-2012 Coda Hale, Yammer.com
* Metrics is Published under Apache Software License 2.0, see LICENSE in root folder.
*
* Thank you for the Metrics developers efforts in making their library available under an Apache license.
* EsperTech incorporates Metrics version 0.2.2 in source code form since Metrics depends on SLF4J
* and this dependency is not possible to introduce for Esper.
* *************************************************************************************
*/
package com.espertech.esper.metrics.codahale_metrics.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 <T> void processWith(MetricProcessor<T> processor, MetricName name, T context) throws Exception {
processor.processCounter(name, this, context);
}
}