/** * Atomically adds the given metric to the set of metrics. * * A side effect of this method is the calling of {@link #onMetricAdded(MetricId, Metric)} if * a new * metric is added. * * This method should only be used on non-{@code SemanticMetricSet} metrics. * * @param name Name of the metric to atomically add if absent. * @param metric The metric to atomically add if absent. * @return {@code null} if the metric was added, or the previously mapped metric. */ protected Metric addIfAbsent(final MetricId name, final Metric metric) { final Metric previous = metrics.putIfAbsent(name, metric); if (previous == null) { onMetricAdded(name, metric); return null; } return previous; }
/** * Atomically adds the given metric to the set of metrics. * * A side effect of this method is the calling of {@link #onMetricAdded(MetricId, Metric)} if * a new * metric is added. * * This method should only be used on non-{@code SemanticMetricSet} metrics. * * @param name Name of the metric to atomically add if absent. * @param metric The metric to atomically add if absent. * @return {@code null} if the metric was added, or the previously mapped metric. */ protected Metric addIfAbsent(final MetricId name, final Metric metric) { final Metric previous = metrics.putIfAbsent(name, metric); if (previous == null) { onMetricAdded(name, metric); return null; } return previous; }
/** * Given a {@link Metric}, registers it under the given name. * * @param name the name of the metric * @param metric the metric * @param <T> the type of the metric * @return {@code metric} * @throws IllegalArgumentException if the name is already registered */ public <T extends Metric> T register(final MetricId name, final T metric) throws IllegalArgumentException { if (metric instanceof SemanticMetricSet) { registerAll(name, (SemanticMetricSet) metric); } else { final Metric existing = metrics.putIfAbsent(name, metric); if (existing == null) { onMetricAdded(name, metric); } else { throw new IllegalArgumentException("A metric named " + name + " already exists"); } } return metric; }
/** * Given a {@link Metric}, registers it under the given name. * * @param name the name of the metric * @param metric the metric * @param <T> the type of the metric * @return {@code metric} * @throws IllegalArgumentException if the name is already registered */ public <T extends Metric> T register(final MetricId name, final T metric) throws IllegalArgumentException { if (metric instanceof SemanticMetricSet) { registerAll(name, (SemanticMetricSet) metric); } else { final Metric existing = metrics.putIfAbsent(name, metric); if (existing == null) { onMetricAdded(name, metric); } else { throw new IllegalArgumentException("A metric named " + name + " already exists"); } } return metric; }