MetricPublicationArbiter implements Predicate<MetricName> { public void registerMetricsFilter(MetricName metricName, MetricPublicationFilter filter) { singleMetricFilters.merge(metricName, ImmutableSet.of(ImmutableDeduplicatingFilterHolder.of(filter)), (oldFilters, newFilter) -> ImmutableSet.<DeduplicatingFilterHolder>builder().addAll(oldFilters).addAll(newFilter).build()); } @VisibleForTesting  MetricPublicationArbiter(Map<MetricName, Set<DeduplicatingFilterHolder>> singleMetricFilters); static MetricPublicationArbiter create(); @Override boolean test(MetricName metricName); void registerMetricsFilter(MetricName metricName, MetricPublicationFilter filter);  }
@Test public void deduplicatesFilters() { Map<MetricName, Set<MetricPublicationArbiter.DeduplicatingFilterHolder>> filters = new HashMap<>(); MetricPublicationArbiter arbiter = new MetricPublicationArbiter(filters); for (int i = 0; i < 100; i++) { int index = i; arbiter.registerMetricsFilter(METRIC_NAME_1, () -> index == 1); arbiter.registerMetricsFilter(METRIC_NAME_1, () -> index == 2); } assertThat(filters.get(METRIC_NAME_1).size()).isEqualTo(2); }