/*
 * Copyright 2020, OpenTelemetry Authors
 *
 * 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.opentelemetry.sdk.internal;

import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nullable;

/**
 * Base class for all the registry classes (Tracer, Meter, etc.).
 *
 * @param <V> the type of the registered value.
 */
public abstract class ComponentRegistry<V> {
  private final Object lock = new Object();
  private final Map<InstrumentationLibraryInfo, V> registry = new ConcurrentHashMap<>();

  /**
   * Returns the registered value associated with this name and {@code null} version if any,
   * otherwise creates a new instance and associates it with the given name and {@code null}
   * version.
   *
   * @param instrumentationName the name of the instrumentation library.
   * @return the registered value associated with this name and {@code null} version.
   */
  public V get(String instrumentationName) {
    return get(instrumentationName, null);
  }

  /**
   * Returns the registered value associated with this name and version if any, otherwise creates a
   * new instance and associates it with the given name and version.
   *
   * @param instrumentationName the name of the instrumentation library.
   * @param instrumentationVersion the version of the instrumentation library.
   * @return the registered value associated with this name and version.
   */
  public V get(String instrumentationName, @Nullable String instrumentationVersion) {
    InstrumentationLibraryInfo instrumentationLibraryInfo =
        InstrumentationLibraryInfo.create(instrumentationName, instrumentationVersion);
    V tracer = registry.get(instrumentationLibraryInfo);
    if (tracer == null) {
      synchronized (lock) {
        // Re-check if the value was added since the previous check, this can happen if multiple
        // threads try to access the same named tracer during the same time. This way we ensure that
        // we create only one TracerSdk per name.
        tracer = registry.get(instrumentationLibraryInfo);
        if (tracer != null) {
          // A different thread already added the named Tracer, just reuse.
          return tracer;
        }
        tracer = newComponent(instrumentationLibraryInfo);
        registry.put(instrumentationLibraryInfo, tracer);
      }
    }
    return tracer;
  }

  public abstract V newComponent(InstrumentationLibraryInfo instrumentationLibraryInfo);
}

@@ -720,16 +720,41 @@
 va.util.
+concurrent.ConcurrentHash
 Map;%0Aimp
@@ -780,36 +780,32 @@
 rrent.Concurrent
-Hash
 Map;%0Aimport java
@@ -998,16 +998,17 @@
 ry%3CV%3E %7B%0A
+%0A
   privat
@@ -1019,52 +1019,18 @@
 nal 
-Object lock = new Object();%0A  private final 
+Concurrent
 Map%3C
@@ -2217,20 +2217,24 @@
 n);%0A
+%0A
     V 
-tracer
+component
  = r
@@ -2282,24 +2282,27 @@
     if (
-tracer =
+component !
 = null) 
@@ -2313,494 +2313,85 @@
     
-synchronized (lock) %7B%0A        // Re-check if the value was added since the previous check, this can happen if multiple%0A        // threads try to access the same named tracer during the same time. This way we ensure that%0A        // we create only one TracerSdk per name.%0A        tracer = registry.get(instrumentationLibraryInfo);%0A        if (tracer != null) %7B%0A          // A different thread already added the named Tracer, just reuse.%0A          return tracer;%0A        %7D%0A        tracer =
+return component;%0A    %7D%0A%0A    registry.putIfAbsent(instrumentationLibraryInfo,
  new
@@ -2431,17 +2431,21 @@
 nfo)
+)
 ;%0A    
-   
+return
  reg
@@ -2450,18 +2450,18 @@
 egistry.
-pu
+ge
 t(instru
@@ -2484,50 +2484,9 @@
 Info
-, tracer);%0A      %7D%0A    %7D%0A    return tracer
+)
 ;%0A  

remove the explicit lock and delegate the thread-safety to the ConcurrentMap implementation. (#805)