public T getOrAdd()

in core/src/main/java/com/spotify/metrics/core/SemanticMetricRegistry.java [438:477]


    public <T extends Metric> T getOrAdd(
        final MetricId name, final SemanticMetricBuilder<T> builder
    ) {
        final Metric metric = metrics.get(name);

        if (metric != null) {
            if (!builder.isInstance(metric)) {
                throw new IllegalArgumentException(
                    name + " is already used for a different type of metric");
            }

            return (T) metric;
        }

        final T addition = builder.newMetric();

        if (addition instanceof SemanticMetricSet) {
            // XXX: getOrAdd has really bad behaviour in supporting both Metric, and
            // SemanticMetricSet,
            //      it effectively causes the method to behave in two different ways depending on
            // the type
            //      of the created metric.
            //      solution: implement getOrAddSet specifically for adding a set of metrics.
            registerAll(name, (SemanticMetricSet) addition);
            return addition;
        }

        final Metric previous = addIfAbsent(name, addition);

        if (previous == null) {
            return addition;
        }

        if (!builder.isInstance(previous)) {
            throw new IllegalArgumentException(
                name + " is already used for a different type of metric");
        }

        return (T) previous;
    }