/** * Instantiates a new compound operation impl. * * @param source the source * @param type the type * @param averagePeriod the average period * @param averageUnit the average unit * @param executor the executor * @param historySize the history size * @param historyPeriod the history period * @param historyUnit the history unit */ public CompoundOperationImpl(OperationStatistic<T> source, Class<T> type, long averagePeriod, TimeUnit averageUnit, ScheduledExecutorService executor, int historySize, long historyPeriod, TimeUnit historyUnit) { this.type = type; this.source = source; this.averageNanos = averageUnit.toNanos(averagePeriod); this.executor = executor; this.historySize = historySize; this.historyNanos = historyUnit.toNanos(historyPeriod); this.operations = new EnumMap(type); for (T result : type.getEnumConstants()) { operations.put(result, new OperationImpl(source, EnumSet.of(result), averageNanos, executor, historySize, historyNanos)); } }
/** * 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(); } }
@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 Result compound(Set<T> results) { if (results.size() == 1) { return component(results.iterator().next()); } else { Set<T> key = EnumSet.copyOf(results); OperationImpl<T> existing = compounds.get(key); if (existing == null) { OperationImpl<T> created = new OperationImpl(source, key, averageNanos, executor, historySize, historyNanos); OperationImpl<T> racer = compounds.putIfAbsent(key, created); if (racer == null) { return created; } else { return racer; } } else { return existing; } } }
@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); } }
/** * 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(); } }
/** * Instantiates a new compound operation impl. * * @param source the source * @param type the type * @param averagePeriod the average period * @param averageUnit the average unit * @param executor the executor * @param historySize the history size * @param historyPeriod the history period * @param historyUnit the history unit */ public CompoundOperationImpl(OperationStatistic<T> source, Class<T> type, long averagePeriod, TimeUnit averageUnit, ScheduledExecutorService executor, int historySize, long historyPeriod, TimeUnit historyUnit) { this.type = type; this.source = source; this.averageNanos = averageUnit.toNanos(averagePeriod); this.executor = executor; this.historySize = historySize; this.historyNanos = historyUnit.toNanos(historyPeriod); this.operations = new EnumMap(type); for (T result : type.getEnumConstants()) { operations.put(result, new OperationImpl(source, EnumSet.of(result), averageNanos, executor, historySize, historyNanos)); } }
@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); } }
/** * 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(); } }
/** * Instantiates a new compound operation impl. * * @param source the source * @param type the type * @param averagePeriod the average period * @param averageUnit the average unit * @param executor the executor * @param historySize the history size * @param historyPeriod the history period * @param historyUnit the history unit */ public CompoundOperationImpl(OperationStatistic<T> source, Class<T> type, long averagePeriod, TimeUnit averageUnit, ScheduledExecutorService executor, int historySize, long historyPeriod, TimeUnit historyUnit) { this.type = type; this.source = source; this.averageNanos = averageUnit.toNanos(averagePeriod); this.executor = executor; this.historySize = historySize; this.historyNanos = historyUnit.toNanos(historyPeriod); this.operations = new EnumMap(type); for (T result : type.getEnumConstants()) { operations.put(result, new OperationImpl(source, EnumSet.of(result), averageNanos, executor, historySize, historyNanos)); } }
@Override public Result compound(Set<T> results) { if (results.size() == 1) { return component(results.iterator().next()); } else { Set<T> key = EnumSet.copyOf(results); OperationImpl<T> existing = compounds.get(key); if (existing == null) { OperationImpl<T> created = new OperationImpl(source, key, averageNanos, executor, historySize, historyNanos); OperationImpl<T> racer = compounds.putIfAbsent(key, created); if (racer == null) { return created; } else { return racer; } } else { return existing; } } }
@Override public Result compound(Set<T> results) { if (results.size() == 1) { return component(results.iterator().next()); } else { Set<T> key = EnumSet.copyOf(results); OperationImpl<T> existing = compounds.get(key); if (existing == null) { OperationImpl<T> created = new OperationImpl(source, key, averageNanos, executor, historySize, historyNanos); OperationImpl<T> racer = compounds.putIfAbsent(key, created); if (racer == null) { return created; } else { return racer; } } else { return existing; } } }