package net.minecraft.util.profiling.metrics; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.WeakHashMap; import java.util.stream.Collectors; import org.jetbrains.annotations.Nullable; public class MetricsRegistry { public static final MetricsRegistry INSTANCE = new MetricsRegistry(); private final WeakHashMap measuredInstances = new WeakHashMap(); private MetricsRegistry() { } public void add(ProfilerMeasured key) { this.measuredInstances.put(key, null); } public List getRegisteredSamplers() { Map> map = (Map>)this.measuredInstances .keySet() .stream() .flatMap(profilerMeasured -> profilerMeasured.profiledMetrics().stream()) .collect(Collectors.groupingBy(MetricSampler::getName)); return aggregateDuplicates(map); } private static List aggregateDuplicates(Map> samplers) { return (List)samplers.entrySet().stream().map(entry -> { String string = (String)entry.getKey(); List list = (List)entry.getValue(); return (MetricSampler)(list.size() > 1 ? new MetricsRegistry.AggregatedMetricSampler(string, list) : (MetricSampler)list.get(0)); }).collect(Collectors.toList()); } static class AggregatedMetricSampler extends MetricSampler { private final List delegates; AggregatedMetricSampler(String name, List delegates) { super( name, ((MetricSampler)delegates.get(0)).getCategory(), () -> averageValueFromDelegates(delegates), () -> beforeTick(delegates), thresholdTest(delegates) ); this.delegates = delegates; } private static MetricSampler.ThresholdTest thresholdTest(List samplers) { return d -> samplers.stream().anyMatch(metricSampler -> metricSampler.thresholdTest != null ? metricSampler.thresholdTest.test(d) : false); } private static void beforeTick(List samplers) { for (MetricSampler metricSampler : samplers) { metricSampler.onStartTick(); } } private static double averageValueFromDelegates(List samplers) { double d = 0.0; for (MetricSampler metricSampler : samplers) { d += metricSampler.getSampler().getAsDouble(); } return d / samplers.size(); } @Override public boolean equals(@Nullable Object object) { if (this == object) { return true; } else if (object == null || this.getClass() != object.getClass()) { return false; } else if (!super.equals(object)) { return false; } else { MetricsRegistry.AggregatedMetricSampler aggregatedMetricSampler = (MetricsRegistry.AggregatedMetricSampler)object; return this.delegates.equals(aggregatedMetricSampler.delegates); } } @Override public int hashCode() { return Objects.hash(new Object[]{super.hashCode(), this.delegates}); } } }