io.smallrye.faulttolerance.internal.StrategyCache Maven / Gradle / Ivy
package io.smallrye.faulttolerance.internal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import io.smallrye.faulttolerance.SpecCompatibility;
import io.smallrye.faulttolerance.core.FaultToleranceStrategy;
@Singleton
public class StrategyCache {
private final Map> strategies = new ConcurrentHashMap<>();
private final Map fallbackMethods = new ConcurrentHashMap<>();
private final Map beforeRetryMethods = new ConcurrentHashMap<>();
private final SpecCompatibility specCompatibility;
@Inject
public StrategyCache(SpecCompatibility specCompatibility) {
this.specCompatibility = specCompatibility;
}
@SuppressWarnings("unchecked")
public FaultToleranceStrategy getStrategy(InterceptionPoint point,
Supplier> producer) {
return (FaultToleranceStrategy) strategies.computeIfAbsent(point, ignored -> producer.get());
}
public FallbackMethodCandidates getFallbackMethodCandidates(InterceptionPoint point, String fallbackMethodName) {
return fallbackMethods.computeIfAbsent(point, ignored -> FallbackMethodCandidates.create(
point, fallbackMethodName, specCompatibility.allowFallbackMethodExceptionParameter()));
}
public BeforeRetryMethod getBeforeRetryMethod(InterceptionPoint point, String fallbackMethodName) {
return beforeRetryMethods.computeIfAbsent(point, ignored -> BeforeRetryMethod.find(point, fallbackMethodName));
}
}