Skip to content

Use Map::computeIfAbsent where feasible #33824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,7 @@ protected CacheOperationMetadata getCacheOperationMetadata(
CacheOperation operation, Method method, Class<?> targetClass) {

CacheOperationCacheKey cacheKey = new CacheOperationCacheKey(operation, method, targetClass);
CacheOperationMetadata metadata = this.metadataCache.get(cacheKey);
if (metadata == null) {
return this.metadataCache.computeIfAbsent(cacheKey, ignored -> {
KeyGenerator operationKeyGenerator;
if (StringUtils.hasText(operation.getKeyGenerator())) {
operationKeyGenerator = getBean(operation.getKeyGenerator(), KeyGenerator.class);
Expand All @@ -363,11 +362,9 @@ else if (StringUtils.hasText(operation.getCacheManager())) {
operationCacheResolver = getCacheResolver();
Assert.state(operationCacheResolver != null, "No CacheResolver/CacheManager set");
}
metadata = new CacheOperationMetadata(operation, method, targetClass,
return new CacheOperationMetadata(operation, method, targetClass,
operationKeyGenerator, operationCacheResolver);
this.metadataCache.put(cacheKey, metadata);
}
return metadata;
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -273,13 +273,11 @@ public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDe
"Expected [" + this.annotationType.getName() + "] to be present on " + sourceType);
}
AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, sourceType.getObjectType());
GenericConverter converter = cachedPrinters.get(converterKey);
if (converter == null) {
GenericConverter converter = cachedPrinters.computeIfAbsent(converterKey, key -> {
Printer<?> printer = this.annotationFormatterFactory.getPrinter(
converterKey.getAnnotation(), converterKey.getFieldType());
converter = new PrinterConverter(this.fieldType, printer, FormattingConversionService.this);
cachedPrinters.put(converterKey, converter);
}
key.getAnnotation(), key.getFieldType());
return new PrinterConverter(this.fieldType, printer, FormattingConversionService.this);
});
return converter.convert(source, sourceType, targetType);
}

Expand Down Expand Up @@ -328,13 +326,11 @@ public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDe
"Expected [" + this.annotationType.getName() + "] to be present on " + targetType);
}
AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, targetType.getObjectType());
GenericConverter converter = cachedParsers.get(converterKey);
if (converter == null) {
GenericConverter converter = cachedParsers.computeIfAbsent(converterKey, key -> {
Parser<?> parser = this.annotationFormatterFactory.getParser(
converterKey.getAnnotation(), converterKey.getFieldType());
converter = new ParserConverter(this.fieldType, parser, FormattingConversionService.this);
cachedParsers.put(converterKey, converter);
}
key.getAnnotation(), key.getFieldType());
return new ParserConverter(this.fieldType, parser, FormattingConversionService.this);
});
return converter.convert(source, sourceType, targetType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,11 +534,7 @@ private Object invokeOperation(Method method, Object[] args) throws JMException,

String[] signature;
synchronized (this.signatureCache) {
signature = this.signatureCache.get(method);
if (signature == null) {
signature = JmxUtils.getMethodSignature(method);
this.signatureCache.put(method, signature);
}
signature = this.signatureCache.computeIfAbsent(method, JmxUtils::getMethodSignature);
}

return this.serverToUse.invoke(this.objectName, method.getName(), args, signature);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -143,17 +143,16 @@ public Theme getTheme(String themeName) {
Theme theme = this.themeCache.get(themeName);
if (theme == null) {
synchronized (this.themeCache) {
theme = this.themeCache.get(themeName);
if (theme == null) {
String basename = this.basenamePrefix + themeName;
theme = this.themeCache.computeIfAbsent(themeName, key -> {
String basename = this.basenamePrefix + key;
MessageSource messageSource = createMessageSource(basename);
theme = new SimpleTheme(themeName, messageSource);
initParent(theme);
this.themeCache.put(themeName, theme);
Theme themeToUse = new SimpleTheme(key, messageSource);
initParent(themeToUse);
if (logger.isDebugEnabled()) {
logger.debug("Theme created: name '" + themeName + "', basename [" + basename + "]");
logger.debug("Theme created: name '" + key + "', basename [" + basename + "]");
}
}
return themeToUse;
});
}
}
return theme;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,6 +47,7 @@
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.function.ThrowingFunction;

/**
* {@link SingleConnectionFactory} subclass that adds {@link Session} caching as well as
Expand Down Expand Up @@ -428,19 +429,13 @@ else if (isCacheConsumers()) {

private MessageProducer getCachedProducer(@Nullable Destination dest) throws JMSException {
DestinationCacheKey cacheKey = (dest != null ? new DestinationCacheKey(dest) : null);
MessageProducer producer = this.cachedProducers.get(cacheKey);
if (producer != null) {
if (logger.isTraceEnabled()) {
logger.trace("Found cached JMS MessageProducer for destination [" + dest + "]: " + producer);
}
}
else {
producer = this.target.createProducer(dest);
MessageProducer producer = this.cachedProducers.computeIfAbsent(cacheKey, (ThrowingFunction<DestinationCacheKey, MessageProducer>) ignored -> {
MessageProducer producerToUse = this.target.createProducer(dest);
if (logger.isDebugEnabled()) {
logger.debug("Registering cached JMS MessageProducer for destination [" + dest + "]: " + producer);
logger.debug("Registering cached JMS MessageProducer for destination [" + dest + "]: " + producerToUse);
}
this.cachedProducers.put(cacheKey, producer);
}
return producerToUse;
});
return new CachedMessageProducer(producer);
}

Expand All @@ -449,33 +444,28 @@ private MessageConsumer getCachedConsumer(Destination dest, @Nullable String sel
@Nullable Boolean noLocal, @Nullable String subscription, boolean durable) throws JMSException {

ConsumerCacheKey cacheKey = new ConsumerCacheKey(dest, selector, noLocal, subscription, durable);
MessageConsumer consumer = this.cachedConsumers.get(cacheKey);
if (consumer != null) {
if (logger.isTraceEnabled()) {
logger.trace("Found cached JMS MessageConsumer for destination [" + dest + "]: " + consumer);
}
}
else {
MessageConsumer consumer = this.cachedConsumers.computeIfAbsent(cacheKey, (ThrowingFunction<ConsumerCacheKey, MessageConsumer>) ignored -> {
MessageConsumer consumerToUse;
if (dest instanceof Topic topic) {
if (noLocal == null) {
consumer = (durable ?
consumerToUse = (durable ?
this.target.createSharedDurableConsumer(topic, subscription, selector) :
this.target.createSharedConsumer(topic, subscription, selector));
}
else {
consumer = (durable ?
consumerToUse = (durable ?
this.target.createDurableSubscriber(topic, subscription, selector, noLocal) :
this.target.createConsumer(dest, selector, noLocal));
}
}
else {
consumer = this.target.createConsumer(dest, selector);
consumerToUse = this.target.createConsumer(dest, selector);
}
if (logger.isDebugEnabled()) {
logger.debug("Registering cached JMS MessageConsumer for destination [" + dest + "]: " + consumer);
logger.debug("Registering cached JMS MessageConsumer for destination [" + dest + "]: " + consumerToUse);
}
this.cachedConsumers.put(cacheKey, consumer);
}
return consumerToUse;
});
return new CachedMessageConsumer(consumer);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -86,13 +86,10 @@ public void afterPropertiesSet() {
*/
@Override
public D resolveDestination(String name) throws DestinationResolutionException {
D destination = this.resolvedDestinationCache.get(name);
if (destination == null) {
return this.resolvedDestinationCache.computeIfAbsent(name, key -> {
Assert.state(this.targetDestinationResolver != null, "No target DestinationResolver set");
destination = this.targetDestinationResolver.resolveDestination(name);
this.resolvedDestinationCache.put(name, destination);
}
return destination;
return this.targetDestinationResolver.resolveDestination(key);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,7 @@ else if (namedValueInfo.required && !nestedParameter.isOptional()) {
* Obtain the named value for the given method parameter.
*/
private NamedValueInfo getNamedValueInfo(MethodParameter parameter) {
NamedValueInfo namedValueInfo = this.namedValueInfoCache.get(parameter);
if (namedValueInfo == null) {
namedValueInfo = createNamedValueInfo(parameter);
namedValueInfo = updateNamedValueInfo(parameter, namedValueInfo);
this.namedValueInfoCache.put(parameter, namedValueInfo);
}
return namedValueInfo;
return this.namedValueInfoCache.computeIfAbsent(parameter, key -> updateNamedValueInfo(key, createNamedValueInfo(key)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,7 @@ else if (namedValueInfo.required && !nestedParameter.isOptional()) {
* Obtain the named value for the given method parameter.
*/
private NamedValueInfo getNamedValueInfo(MethodParameter parameter) {
NamedValueInfo namedValueInfo = this.namedValueInfoCache.get(parameter);
if (namedValueInfo == null) {
namedValueInfo = createNamedValueInfo(parameter);
namedValueInfo = updateNamedValueInfo(parameter, namedValueInfo);
this.namedValueInfoCache.put(parameter, namedValueInfo);
}
return namedValueInfo;
return this.namedValueInfoCache.computeIfAbsent(parameter, key -> updateNamedValueInfo(key, createNamedValueInfo(key)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -120,11 +120,7 @@ public Method resolveMethod(Throwable exception) {
*/
@Nullable
public Method resolveMethodByExceptionType(Class<? extends Throwable> exceptionType) {
Method method = this.exceptionLookupCache.get(exceptionType);
if (method == null) {
method = getMappedMethod(exceptionType);
this.exceptionLookupCache.put(exceptionType, method);
}
Method method = this.exceptionLookupCache.computeIfAbsent(exceptionType, this::getMappedMethod);
return (method != NO_MATCHING_EXCEPTION_HANDLER_METHOD ? method : null);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -634,11 +634,7 @@ protected InvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handler
logger.debug("Searching methods to handle " + exception.getClass().getSimpleName());
}
Class<?> beanType = handlerMethod.getBeanType();
AbstractExceptionHandlerMethodResolver resolver = this.exceptionHandlerCache.get(beanType);
if (resolver == null) {
resolver = createExceptionHandlerMethodResolverFor(beanType);
this.exceptionHandlerCache.put(beanType, resolver);
}
AbstractExceptionHandlerMethodResolver resolver = this.exceptionHandlerCache.computeIfAbsent(beanType, this::createExceptionHandlerMethodResolverFor);
Method method = resolver.resolveMethod(exception);
if (method != null) {
return new InvocableHandlerMethod(handlerMethod.getBean(), method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,7 @@ public InvocableHandlerMethod initExceptionHandlerMethod(HandlerMethod handlerMe
logger.debug("Searching for methods to handle " + ex.getClass().getSimpleName());
}
Class<?> beanType = handlerMethod.getBeanType();
AbstractExceptionHandlerMethodResolver resolver = this.exceptionHandlerCache.get(beanType);
if (resolver == null) {
resolver = this.exceptionMethodResolverFactory.apply(beanType);
this.exceptionHandlerCache.put(beanType, resolver);
}
AbstractExceptionHandlerMethodResolver resolver = this.exceptionHandlerCache.computeIfAbsent(beanType, this.exceptionMethodResolverFactory);
InvocableHandlerMethod exceptionHandlerMethod = null;
Method method = resolver.resolveMethod(ex);
if (method != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -101,11 +101,7 @@ public <B> ContainedBean<B> getBean(

SpringContainedBean<?> bean;
if (lifecycleOptions.canUseCachedReferences()) {
bean = this.beanCache.get(beanType);
if (bean == null) {
bean = createBean(beanType, lifecycleOptions, fallbackProducer);
this.beanCache.put(beanType, bean);
}
bean = this.beanCache.computeIfAbsent(beanType, ignored -> createBean(beanType, lifecycleOptions, fallbackProducer));
}
else {
bean = createBean(beanType, lifecycleOptions, fallbackProducer);
Expand All @@ -120,11 +116,7 @@ public <B> ContainedBean<B> getBean(

SpringContainedBean<?> bean;
if (lifecycleOptions.canUseCachedReferences()) {
bean = this.beanCache.get(name);
if (bean == null) {
bean = createBean(name, beanType, lifecycleOptions, fallbackProducer);
this.beanCache.put(name, bean);
}
bean = this.beanCache.computeIfAbsent(name, ignored -> createBean(name, beanType, lifecycleOptions, fallbackProducer));
}
else {
bean = createBean(name, beanType, lifecycleOptions, fallbackProducer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,7 @@ else if (namedValueInfo.required && !nestedParameter.isOptional()) {
* Obtain the named value for the given method parameter.
*/
private NamedValueInfo getNamedValueInfo(MethodParameter parameter) {
NamedValueInfo namedValueInfo = this.namedValueInfoCache.get(parameter);
if (namedValueInfo == null) {
namedValueInfo = createNamedValueInfo(parameter);
namedValueInfo = updateNamedValueInfo(parameter, namedValueInfo);
this.namedValueInfoCache.put(parameter, namedValueInfo);
}
return namedValueInfo;
return this.namedValueInfoCache.computeIfAbsent(parameter, key -> updateNamedValueInfo(key, createNamedValueInfo(key)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,13 @@ public boolean resolve(

@Nullable
private NamedValueInfo getNamedValueInfo(MethodParameter parameter, HttpRequestValues.Metadata requestValues) {
NamedValueInfo info = this.namedValueInfoCache.get(parameter);
if (info == null) {
info = createNamedValueInfo(parameter, requestValues);
if (info == null) {
return this.namedValueInfoCache.computeIfAbsent(parameter, key -> {
NamedValueInfo infoToUse = createNamedValueInfo(key, requestValues);
if (infoToUse == null) {
return null;
}
info = updateNamedValueInfo(parameter, info);
this.namedValueInfoCache.put(parameter, info);
}
return info;
return updateNamedValueInfo(key, infoToUse);
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,7 @@ public Mono<Object> resolveArgument(
* Obtain the named value for the given method parameter.
*/
private NamedValueInfo getNamedValueInfo(MethodParameter parameter) {
NamedValueInfo namedValueInfo = this.namedValueInfoCache.get(parameter);
if (namedValueInfo == null) {
namedValueInfo = createNamedValueInfo(parameter);
namedValueInfo = updateNamedValueInfo(parameter, namedValueInfo);
this.namedValueInfoCache.put(parameter, namedValueInfo);
}
return namedValueInfo;
return this.namedValueInfoCache.computeIfAbsent(parameter, key -> updateNamedValueInfo(key, createNamedValueInfo(key)));
}

/**
Expand Down
Loading
Loading