Skip to content

Commit e882934

Browse files
committed
Use Map::computeIfAbsent where feasible
1 parent 23fe397 commit e882934

File tree

3 files changed

+11
-28
lines changed

3 files changed

+11
-28
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/sbom/SbomEndpointWebExtension.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.boot.actuate.sbom;
1818

1919
import java.io.IOException;
20-
import java.io.UncheckedIOException;
2120
import java.nio.charset.StandardCharsets;
2221
import java.util.Map;
2322
import java.util.concurrent.ConcurrentHashMap;
@@ -29,6 +28,7 @@
2928
import org.springframework.boot.actuate.sbom.SbomProperties.Sbom;
3029
import org.springframework.core.io.Resource;
3130
import org.springframework.util.MimeType;
31+
import org.springframework.util.function.ThrowingFunction;
3232

3333
/**
3434
* {@link EndpointWebExtension @EndpointWebExtension} for the {@link SbomEndpoint}.
@@ -68,14 +68,9 @@ private MimeType getMediaType(String id, Resource resource) {
6868
if (sbomProperties != null && sbomProperties.getMediaType() != null) {
6969
return sbomProperties.getMediaType();
7070
}
71-
return this.detectedMediaTypeCache.computeIfAbsent(id, (ignored) -> {
72-
try {
73-
return detectSbomType(resource);
74-
}
75-
catch (IOException ex) {
76-
throw new UncheckedIOException("Failed to detect type of resource '%s'".formatted(resource), ex);
77-
}
78-
}).getMediaType();
71+
return this.detectedMediaTypeCache
72+
.computeIfAbsent(id, (ThrowingFunction<String, SbomType>) (ignored) -> detectSbomType(resource))
73+
.getMediaType();
7974
}
8075

8176
private SbomType detectSbomType(Resource resource) throws IOException {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/type/classreading/ConcurrentReferenceCachingMetadataReaderFactory.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.core.type.classreading.MetadataReaderFactory;
2727
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
2828
import org.springframework.util.ConcurrentReferenceHashMap;
29+
import org.springframework.util.function.ThrowingFunction;
2930

3031
/**
3132
* Caching implementation of the {@link MetadataReaderFactory} interface backed by a
@@ -70,22 +71,14 @@ public ConcurrentReferenceCachingMetadataReaderFactory(ClassLoader classLoader)
7071

7172
@Override
7273
public MetadataReader getMetadataReader(String className) throws IOException {
73-
MetadataReader metadataReader = this.classNameCache.get(className);
74-
if (metadataReader == null) {
75-
metadataReader = super.getMetadataReader(className);
76-
this.classNameCache.put(className, metadataReader);
77-
}
78-
return metadataReader;
74+
return this.classNameCache.computeIfAbsent(className,
75+
(ThrowingFunction<String, MetadataReader>) super::getMetadataReader);
7976
}
8077

8178
@Override
8279
public MetadataReader getMetadataReader(Resource resource) throws IOException {
83-
MetadataReader metadataReader = this.resourceCache.get(resource);
84-
if (metadataReader == null) {
85-
metadataReader = createMetadataReader(resource);
86-
this.resourceCache.put(resource, metadataReader);
87-
}
88-
return metadataReader;
80+
return this.resourceCache.computeIfAbsent(resource,
81+
(ThrowingFunction<Resource, MetadataReader>) this::createMetadataReader);
8982
}
9083

9184
/**

spring-boot-tests/spring-boot-integration-tests/spring-boot-server-tests/src/intTest/java/org/springframework/boot/context/embedded/EmbeddedServerContainerInvocationContextProvider.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,8 @@ private void cleanupCaches() {
116116
private AbstractApplicationLauncher getAbstractApplicationLauncher(Application application,
117117
Class<? extends AbstractApplicationLauncher> launcherClass) {
118118
String cacheKey = application.getContainer() + ":" + application.getPackaging() + ":" + launcherClass.getName();
119-
if (this.launcherCache.containsKey(cacheKey)) {
120-
return this.launcherCache.get(cacheKey);
121-
}
122-
AbstractApplicationLauncher launcher = ReflectionUtils.newInstance(launcherClass, application,
123-
new File(buildOutput.getRootLocation(), "app-launcher-" + UUID.randomUUID()));
124-
this.launcherCache.put(cacheKey, launcher);
125-
return launcher;
119+
return this.launcherCache.computeIfAbsent(cacheKey, (ignored) -> ReflectionUtils.newInstance(launcherClass,
120+
application, new File(buildOutput.getRootLocation(), "app-launcher-" + UUID.randomUUID())));
126121
}
127122

128123
private Application getApplication(EmbeddedServletContainerTest annotation, String container) {

0 commit comments

Comments
 (0)