All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.opentelemetry.sdk.autoconfigure.internal.NamedSpiManager Maven / Gradle / Ivy

There is a newer version: 1.44.1
Show newest version
/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.sdk.autoconfigure.internal;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;
import javax.annotation.Nullable;

/**
 * This class is internal and is hence not for public use. Its APIs are unstable and can change at
 * any time.
 */
public final class NamedSpiManager {

  private final Map> nameToProvider;
  private final ConcurrentMap> nameToImplementation = new ConcurrentHashMap<>();

  private NamedSpiManager(Map> nameToProvider) {
    this.nameToProvider = nameToProvider;
  }

  static  NamedSpiManager create(Map> nameToProvider) {
    return new NamedSpiManager<>(nameToProvider);
  }

  public static  NamedSpiManager createEmpty() {
    return create(Collections.emptyMap());
  }

  /** Return an implementation by name, invoking the supplier if not previously invoked. */
  @Nullable
  public T getByName(String name) {
    return nameToImplementation
        .computeIfAbsent(name, this::tryLoadImplementationForName)
        .orElse(null);
  }

  private Optional tryLoadImplementationForName(String name) {
    Supplier provider = nameToProvider.get(name);
    if (provider == null) {
      return Optional.empty();
    }

    return Optional.ofNullable(provider.get());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy