/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.trino.spi.metrics;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import io.trino.spi.Mergeable;

import java.util.HashMap;
import java.util.Map;

import static java.util.Objects.requireNonNull;

public class Metrics
        implements Mergeable<Metrics>
{
    public static final Metrics EMPTY = new Metrics(Map.of());

    private final Map<String, Metric> metrics;

    @JsonCreator
    public Metrics(Map<String, Metric> metrics)
    {
        this.metrics = Map.copyOf(requireNonNull(metrics, "metrics is null"));
    }

    @JsonValue
    public Map<String, Metric> getMetrics()
    {
        return metrics;
    }

    @Override
    public Metrics mergeWith(Metrics other)
    {
        return accumulator().add(this).add(other).get();
    }

    public static Accumulator accumulator()
    {
        return new Accumulator();
    }

    public static class Accumulator
    {
        private final Map<String, Metric> merged = new HashMap<>();

        private Accumulator()
        {
        }

        public Accumulator add(Metrics metrics)
        {
            metrics.getMetrics().forEach((key, value) ->
                    merged.merge(key, value, (left, right) -> (Metric) left.mergeWith(right)));
            return this;
        }

        public Metrics get()
        {
            return new Metrics(merged);
        }
    }
}

@@ -973,32 +973,35 @@
 p%3CString, Metric
+%3C?%3E
 %3E metrics;%0A%0A    
@@ -995,16 +995,16 @@
 etrics;%0A
-
 %0A    @Js
@@ -1046,24 +1046,27 @@
 ring, Metric
+%3C?%3E
 %3E metrics)%0A 
@@ -1200,16 +1200,19 @@
 , Metric
+%3C?%3E
 %3E getMet
@@ -1556,16 +1556,19 @@
 , Metric
+%3C?%3E
 %3E merged
@@ -1803,82 +1803,250 @@
 ue, 
-(left, right) -%3E (Metric) left.mergeWith(right)));%0A            return this
+Accumulator::merge));%0A            return this;%0A        %7D%0A%0A        @SuppressWarnings(%7B%22rawtypes%22, %22unchecked%22%7D)%0A        private static Metric%3C?%3E merge(Metric%3C?%3E a, Metric%3C?%3E b)%0A        %7B%0A            return (Metric%3C?%3E) ((Metric) a).mergeWith(b)
 ;%0A  

Fix raw types use in Metrics