/*
 * 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.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
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 ConcurrentMap<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 component = registry.get(instrumentationLibraryInfo);
    if (component != null) {
      return component;
    }

    registry.putIfAbsent(instrumentationLibraryInfo, newComponent(instrumentationLibraryInfo));
    return registry.get(instrumentationLibraryInfo);
  }

  public abstract V newComponent(InstrumentationLibraryInfo instrumentationLibraryInfo);
}

@@ -2342,56 +2342,24 @@
     
-registry.putIfAbsent(instrumentationLibraryInfo,
+V newComponent =
  new
@@ -2399,62 +2399,156 @@
 nfo)
-)
 ;%0A    
-return registry.get(instrumentationLibraryInfo)
+V oldComponent = registry.putIfAbsent(instrumentationLibraryInfo, newComponent);%0A    return oldComponent != null ? oldComponent : newComponent
 ;%0A  

Use a more clean pattern for putIfAbsent (#827)