public static <T extends Metric> T getOrRegister(MetricRegistry metricRegistry, String name, T newMetric) {
        final Metric metric = metricRegistry.getMetrics().get(name);
        if (metric != null) {
            //noinspection unchecked
            return (T) metric;
        }
        try {
            return metricRegistry.register(name, newMetric);
        } catch (IllegalArgumentException ignored) {
            //noinspection unchecked
            return (T) metricRegistry.getMetrics().get(name);
        }
    }
@Test
    public void getOrRegister() {
        final MetricRegistry metricRegistry = new MetricRegistry();

        final Counter newMetric1 = new Counter();
        final Counter newMetric2 = new Counter();

        assertThat(MetricUtils.getOrRegister(metricRegistry, "test1", newMetric1)).isEqualTo(newMetric1);
        assertThat(MetricUtils.getOrRegister(metricRegistry, "test1", newMetric2)).isEqualTo(newMetric1);
    }