TopNMetricPublicationController { @SuppressWarnings("unchecked") public static <T extends Comparable> TopNMetricPublicationController<T> create(int maxPermittedRank) { Preconditions.checkState(maxPermittedRank > 0, "maxPermittedRank must be positive", SafeArg.of("maxPermittedRank", maxPermittedRank)); return new TopNMetricPublicationController<T>(Comparator.naturalOrder(), maxPermittedRank, REFRESH_INTERVAL); } @VisibleForTesting  TopNMetricPublicationController(
            Comparator<T> comparator,
            int maxPermittedRank,
            Duration refreshInterval); @SuppressWarnings("unchecked") // Guaranteed correct static TopNMetricPublicationController<T> create(int maxPermittedRank); MetricPublicationFilter registerAndCreateFilter(Gauge<T> gauge);  }
@Test public void canSelectivelyPublishMultipleGaugeResults() { List<Gauge<Long>> atomicLongs = LongStream.range(0, 100) .mapToObj(AtomicLong::new) .<Gauge<Long>>map(atomicLong -> atomicLong::get) .collect(Collectors.toList()); TopNMetricPublicationController<Long> controller = TopNMetricPublicationController.create(7); List<MetricPublicationFilter> filtersInOrder = atomicLongs.stream() .map(controller::registerAndCreateFilter) .collect(Collectors.toList()); assertThat(filtersInOrder.get(0).shouldPublish()).isFalse(); assertThat(filtersInOrder.get(50).shouldPublish()).isFalse(); assertThat(filtersInOrder.get(92).shouldPublish()).isFalse(); assertThat(filtersInOrder.get(93).shouldPublish()).isTrue(); assertThat(filtersInOrder.get(94).shouldPublish()).isTrue(); assertThat(filtersInOrder.get(99).shouldPublish()).isTrue(); }
@Test public void throwsWhenCreatingControllerWithNonPositiveArguments() { assertThatThrownBy(() -> TopNMetricPublicationController.create(0)) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("maxPermittedRank must be positive"); assertThatThrownBy(() -> TopNMetricPublicationController.create(-29359)) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("maxPermittedRank must be positive"); }