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

io.opentelemetry.contrib.metrics.micrometer.internal.MemoizingSupplier Maven / Gradle / Ivy

The newest version!
/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.contrib.metrics.micrometer.internal;

import java.util.function.Supplier;
import javax.annotation.Nullable;

/**
 * Delegating implementation of {@link Supplier Supplier} that ensures that the {@link
 * Supplier#get()} method is called at most once and the result is memoized for subsequent
 * invocations.
 *
 * 

This class is internal and is hence not for public use. Its APIs are unstable and can change * at any time. */ public final class MemoizingSupplier implements Supplier { private final Supplier delegate; private volatile boolean initialized; @Nullable private volatile T cachedResult; public MemoizingSupplier(Supplier delegate) { this.delegate = delegate; } @Override @SuppressWarnings("NullAway") public T get() { T result = cachedResult; if (!initialized) { synchronized (this) { if (!initialized) { result = delegate.get(); cachedResult = result; initialized = true; return result; } } } return result; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy