1 package ch.qos.logback.access.filter; 2 3 4 abstract public class PeriodicStats { 5 6 private long nextPeriodBegins = 0; 7 private long lastTotal = 0; 8 private long lastCount = 0; 9 10 private double average; 11 private int n; 12 13 PeriodicStats() { 14 this(System.currentTimeMillis()); 15 } 16 17 PeriodicStats(long now) { 18 nextPeriodBegins = computeStartOfNextPeriod(now); 19 } 20 21 void update(long now, long total) { 22 if (now > nextPeriodBegins) { 23 lastCount = total - lastTotal; 24 lastTotal = total; 25 average = (average * n + lastCount) / (++n); 26 nextPeriodBegins = computeStartOfNextPeriod(now); 27 } 28 } 29 30 public double getAverage() { 31 return average; 32 } 33 34 public long getLastCount() { 35 return lastCount; 36 } 37 38 void reset(long now) { 39 nextPeriodBegins = computeStartOfNextPeriod(now); 40 lastTotal = 0; 41 lastCount = 0; 42 average = 0.0; 43 n = 0; 44 } 45 46 void reset() { 47 reset(System.currentTimeMillis()); 48 } 49 50 abstract long computeStartOfNextPeriod(long now); 51 52 }