/**
  * Removes the metric with the given name.
  *
  * @param name the name of the metric
  */
 public void removeMetric(MetricName name) {
   final Metric metric = metrics.remove(name);
   if (metric != null) {
     if (metric instanceof Stoppable) {
       ((Stoppable) metric).stop();
     }
     notifyMetricRemoved(name);
   }
 }
  /**
   * Gets any existing metric with the given name or, if none exists, adds the given metric.
   *
   * @param name the metric's name
   * @param metric the new metric
   * @param <T> the type of the metric
   * @return either the existing metric or {@code metric}
   */
  @SuppressWarnings("unchecked")
  protected final <T extends Metric> T getOrAdd(MetricName name, T metric) {
    final Metric existingMetric = metrics.get(name);
    if (existingMetric == null) {
      final Metric justAddedMetric = metrics.putIfAbsent(name, metric);
      if (justAddedMetric == null) {
        notifyMetricAdded(name, metric);
        return metric;
      }

      if (metric instanceof Stoppable) {
        ((Stoppable) metric).stop();
      }

      return (T) justAddedMetric;
    }
    return (T) existingMetric;
  }
Example #3
0
 public void stop() {
   stopper.stop();
 }