com.alachisoft.ncache.common.caching.statistics.customcounters.InstantaneousCounter Maven / Gradle / Ivy
Show all versions of nc-common Show documentation
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.alachisoft.ncache.common.caching.statistics.customcounters;
/**
* @author Muneeb Shahid
*
* Instantaneous counter is the base class for all types of instantaneous counters. An instantaneous counters values must change after each second i.e. on per second basis. All
* values calculated in a time interval of one seconds becomes invalid for the next interval and it is re-initialized.
*/
public abstract class InstantaneousCounter extends PerformanceCounterBase {
InstantaneousFlip currentFlip;
public InstantaneousCounter(String name, String instance) {
super(name, instance);
currentFlip = FlipManager.flip.clone();
}
public InstantaneousCounter(String category, String name, String instance) {
super(category, name, instance);
currentFlip = FlipManager.flip.clone();
}
@Override
public void increment() {
incrementBy(1);
}
@Override
public void incrementBy(double value) {
synchronized (this) {
if (!currentFlip.equals(FlipManager.flip)) {
if (FlipManager.flip.getFlip() == currentFlip.getFlip() + 1) {
_lastValue = _value;
} else {
_lastValue = 0;
}
_value = 0;
currentFlip = FlipManager.flip.clone();
flipChanged();
}
calculate(value);
}
}
protected abstract void calculate(double value);
protected abstract void flipChanged();
@Override
public double getValue() {
updateIfFlipChanged();
return _lastValue;
}
protected void updateIfFlipChanged() {
synchronized (this) {
if (!currentFlip.equals(FlipManager.flip)) {
if (FlipManager.flip.getFlip() == currentFlip.getFlip() + 1) {
_lastValue = _value;
} else {
_lastValue = 0;
}
_value = 0;
currentFlip = FlipManager.flip.clone();
flipChanged();
}
}
}
}