Skip to content

Commit a6c0a03

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

File tree

2 files changed

+6
-16
lines changed

2 files changed

+6
-16
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-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)