org.zodiac.monitor.metrics.micrometer.DefaultConcurrentDoubleGauge Maven / Gradle / Ivy
package org.zodiac.monitor.metrics.micrometer;
import java.util.function.Supplier;
import org.zodiac.sdk.toolkit.concurrent.atomic.AtomicDouble;
public class DefaultConcurrentDoubleGauge extends AbstractConcurrentGauge implements ConcurrentDoubleGauge {
private Supplier incFunc;
private Supplier decFunc;
private final AtomicDouble count;
private final AtomicDouble maxPreviousMinute;
private final AtomicDouble minPreviousMinute;
private final AtomicDouble maxThisMinute;
private final AtomicDouble minThisMinute;
public DefaultConcurrentDoubleGauge(Id id, Supplier incFunc, Supplier decFunc) {
super(id);
this.incFunc = incFunc;
this.decFunc = decFunc;
count = new AtomicDouble(0);
maxPreviousMinute = new AtomicDouble(0);
minPreviousMinute = new AtomicDouble(0);
maxThisMinute = new AtomicDouble(0);
minThisMinute = new AtomicDouble(0);
}
@Override
public double value() {
return getCount();
}
@Override
protected void doStartNewMinute(final long newMinute) {
if (newMinute > getThisMinute()) {
setThisMinute(newMinute);
maxPreviousMinute.set(maxThisMinute.get());
minPreviousMinute.set(minThisMinute.get());
maxThisMinute.set(count.get());
minThisMinute.set(count.get());
}
}
@Override
protected Double doCount() {
syncMaybeStartNewMinute();
return count.get();
}
@Override
protected Double doGetMax() {
syncMaybeStartNewMinute();
return maxPreviousMinute.get();
}
@Override
protected Double doGetMin() {
syncMaybeStartNewMinute();
return minPreviousMinute.get();
}
@Override
protected void doInc() {
double newCount = count.addAndGet(this.incFunc.get());
if (newCount > maxThisMinute.get()) {
maxThisMinute.set(newCount);
}
}
@Override
protected void doInc(Double delta) {
double newCount = count.addAndGet(delta);
if (newCount > maxThisMinute.get()) {
maxThisMinute.set(newCount);
}
}
@Override
protected void doDec() {
double newCount = count.addAndGet(this.decFunc.get());
if (newCount < minThisMinute.get()) {
minThisMinute.set(newCount);
}
}
@Override
protected void doDec(Double delta) {
double newCount = count.addAndGet(delta);
if (newCount < minThisMinute.get()) {
minThisMinute.set(newCount);
}
}
}