@Override public Statistic<Double> ratioOf(Set<T> numerator, Set<T> denominator) { List<Set<T>> key = Arrays.<Set<T>> asList(EnumSet.copyOf(numerator), EnumSet.copyOf(denominator)); ExpiringStatistic<Double> existing = ratios.get(key); if (existing == null) { final Statistic<Double> numeratorRate = compound(numerator).rate(); final Statistic<Double> denominatorRate = compound(denominator).rate(); ExpiringStatistic<Double> created = new ExpiringStatistic(new ValueStatistic<Double>() { @Override public Double value() { return numeratorRate.value() / denominatorRate.value(); } }, executor, historySize, historyNanos); ExpiringStatistic<Double> racer = ratios.putIfAbsent(key, created); if (racer == null) { return created; } else { return racer; } } else { return existing; } }
@Override public boolean active() { return delegate.active(); }
/** * Check the statistic for expiry. * * @param expiry expiry threshold * @return {@code true} if expired */ protected boolean expire(long expiry) { return delegate.expire(expiry); } }
/** * Set the sample history parameters. * * @param historySize history sample size * @param historyNanos history sample period */ protected void setHistory(int historySize, long historyNanos) { delegate.setHistory(historySize, historyNanos); }
/** * Start sampling. */ protected void start() { delegate.start(); }
@Override protected void stopStatistic() { super.stopStatistic(); source.removeDerivedStatistic(observer); }
@Override public List<Timestamped<Double>> history() { return delegate.history(); }
@Override protected void startStatistic() { super.startStatistic(); source.addDerivedStatistic(observer); } };
@Override public T value() { touch(); return super.value(); } }
@Override public Double value() { return delegate.value(); }
@Override public void setHistory(int samples, long time, TimeUnit unit) { historySize = samples; historyNanos = unit.toNanos(time); for (OperationImpl<T> op : operations.values()) { op.setHistory(historySize, historyNanos); } for (OperationImpl<T> op : compounds.values()) { op.setHistory(historySize, historyNanos); } for (ExpiringStatistic<Double> ratio : ratios.values()) { ratio.setHistory(historySize, historyNanos); } }
@Override public void setAlwaysOn(boolean enable) { alwaysOn = enable; if (enable) { for (OperationImpl<T> op : operations.values()) { op.start(); } for (OperationImpl<T> op : compounds.values()) { op.start(); } for (ExpiringStatistic<Double> ratio : ratios.values()) { ratio.start(); } } }
@Override protected void stopStatistic() { super.stopStatistic(); source.removeDerivedStatistic(observer); }
@Override public List<Timestamped<Double>> history() { return delegate.history(); }
@Override protected void startStatistic() { super.startStatistic(); source.addDerivedStatistic(observer); } };
@Override public T value() { touch(); return super.value(); } }
@Override public Double value() { return delegate.value(); }
/** * Set the sample history parameters. * * @param historySize history sample size * @param historyNanos history sample period */ protected void setHistory(int historySize, long historyNanos) { delegate.setHistory(historySize, historyNanos); }
/** * Start sampling. */ protected void start() { delegate.start(); }
/** * Expire. * * @param expiryTime the expiry time * @return true, if successful */ boolean expire(long expiryTime) { if (alwaysOn) { return false; } else { boolean expired = true; for (OperationImpl<?> o : operations.values()) { expired &= o.expire(expiryTime); } for (Iterator<OperationImpl<T>> it = compounds.values().iterator(); it.hasNext();) { if (it.next().expire(expiryTime)) { it.remove(); } } for (Iterator<ExpiringStatistic<Double>> it = ratios.values().iterator(); it.hasNext();) { if (it.next().expire(expiryTime)) { it.remove(); } } return expired & compounds.isEmpty() & ratios.isEmpty(); } }