io.opentelemetry.sdk.autoconfigure.internal.NamedSpiManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opentelemetry-sdk-extension-autoconfigure Show documentation
Show all versions of opentelemetry-sdk-extension-autoconfigure Show documentation
OpenTelemetry SDK Auto-configuration
/*
* 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());
}
}