/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.druid.segment.serde;

import org.apache.druid.java.util.common.ISE;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;

/**
 */
public class ComplexMetrics
{
  private static final Map<String, ComplexMetricSerde> COMPLEX_SERIALIZERS = new HashMap<>();

  @Nullable
  public static ComplexMetricSerde getSerdeForType(String type)
  {
    return COMPLEX_SERIALIZERS.get(type);
  }

  public static void registerSerde(String type, ComplexMetricSerde serde)
  {
    if (COMPLEX_SERIALIZERS.containsKey(type)) {
      if (!COMPLEX_SERIALIZERS.get(type).getClass().getName().equals(serde.getClass().getName())) {
        throw new ISE(
            "Incompatible serializer for type[%s] already exists. Expected [%s], found [%s].",
            type,
            serde.getClass().getName(),
            COMPLEX_SERIALIZERS.get(type).getClass().getName()
        );
      }
    } else {
      COMPLEX_SERIALIZERS.put(type, serde);
    }
  }
}

@@ -945,43 +945,135 @@
 til.
-HashMap;%0Aimport java.util.Map;%0A%0A/**
+concurrent.ConcurrentHashMap;%0A%0A/**%0A *  ComplexMetrics houses a mapping of serde names to affiliated ComplexMetricSerde objects.
 %0A */
@@ -1126,16 +1126,30 @@
 c final 
+ConcurrentHash
 Map%3CStri
@@ -1198,16 +1198,26 @@
 S = new 
+Concurrent
 HashMap%3C
@@ -1349,16 +1349,453 @@
 );%0A  %7D%0A%0A
+  /**%0A   * Register a serde name -%3E ComplexMetricSerde mapping.%0A   *%0A   * %3Cp%3E%0A   * If the specified serde key string is already used and the supplied ComplexMetricSerde is not of the same%0A   * type as the existing value in the map for said key, an ISE is thrown.%0A   * %3C/p%3E%0A   *%0A   * @param type The serde name used as the key in the map.%0A   * @param serde The ComplexMetricSerde object to be associated with the 'type' in the map.%0A   */%0A
   public
@@ -1868,20 +1868,16 @@
   %7B%0A    
-if (
 COMPLEX_
@@ -1894,67 +1894,120 @@
 S.co
-ntainsKey(type)) %7B%0A      if (!COMPLEX_SERIALIZERS.get(type)
+mpute(type, (key, value) -%3E %7B%0A      if (value == null) %7B%0A        return serde;%0A      %7D else %7B%0A        if (!value
 .get
@@ -2070,16 +2070,18 @@
         
+  
 throw ne
@@ -2099,16 +2099,18 @@
         
+ 
+ 
 %22Incompa
@@ -2200,14 +2200,17 @@
     
-type,%0A
+  key,%0A  
     
@@ -2261,37 +2261,15 @@
     
+  
-COMPLEX_SERIALIZERS.get(type)
+value
 .get
@@ -2298,19 +2298,17 @@
     
+ 
+ 
 );%0A    
-  %7D%0A
     
@@ -2326,51 +2326,51 @@
     
-COMPLEX_SERIALIZERS.put(type, serde);%0A    %7D
+    return value;%0A        %7D%0A      %7D%0A    %7D);
 %0A  %7D

Fix concurrency of ComplexMetrics.java (#9134)