diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/jdbc/DataSourcePoolMetrics.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/jdbc/DataSourcePoolMetrics.java index caf29f30ff..f22023a1ef 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/jdbc/DataSourcePoolMetrics.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/jdbc/DataSourcePoolMetrics.java @@ -110,12 +110,8 @@ public class DataSourcePoolMetrics implements MeterBinder { @Override public DataSourcePoolMetadata getDataSourcePoolMetadata(DataSource dataSource) { - DataSourcePoolMetadata metadata = cache.get(dataSource); - if (metadata == null) { - metadata = this.metadataProvider.getDataSourcePoolMetadata(dataSource); - cache.put(dataSource, metadata); - } - return metadata; + return cache.computeIfAbsent(dataSource, + (key) -> this.metadataProvider.getDataSourcePoolMetadata(dataSource)); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionEvaluationReport.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionEvaluationReport.java index cb987b1af3..7712bc2847 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionEvaluationReport.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionEvaluationReport.java @@ -80,10 +80,7 @@ public final class ConditionEvaluationReport { Assert.notNull(condition, "Condition must not be null"); Assert.notNull(outcome, "Outcome must not be null"); this.unconditionalClasses.remove(source); - if (!this.outcomes.containsKey(source)) { - this.outcomes.put(source, new ConditionAndOutcomes()); - } - this.outcomes.get(source).add(condition, outcome); + this.outcomes.computeIfAbsent(source, (key) -> new ConditionAndOutcomes()).add(condition, outcome); this.addedAncestorOutcomes = false; } diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/classloader/ClassLoaderFiles.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/classloader/ClassLoaderFiles.java index e611e5f398..06e7d8f3f9 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/classloader/ClassLoaderFiles.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/classloader/ClassLoaderFiles.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2023 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. @@ -108,12 +108,7 @@ public class ClassLoaderFiles implements ClassLoaderFileRepository, Serializable * @return an existing or newly added {@link SourceDirectory} */ protected final SourceDirectory getOrCreateSourceDirectory(String name) { - SourceDirectory sourceDirectory = this.sourceDirectories.get(name); - if (sourceDirectory == null) { - sourceDirectory = new SourceDirectory(name); - this.sourceDirectories.put(name, sourceDirectory); - } - return sourceDirectory; + return this.sourceDirectories.computeIfAbsent(name, (key) -> new SourceDirectory(name)); } /** diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/SimpleConfigurationMetadataRepository.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/SimpleConfigurationMetadataRepository.java index bdfc91cb20..c196afb5e2 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/SimpleConfigurationMetadataRepository.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/SimpleConfigurationMetadataRepository.java @@ -54,11 +54,8 @@ public class SimpleConfigurationMetadataRepository implements ConfigurationMetad public void add(Collection sources) { for (ConfigurationMetadataSource source : sources) { String groupId = source.getGroupId(); - ConfigurationMetadataGroup group = this.allGroups.get(groupId); - if (group == null) { - group = new ConfigurationMetadataGroup(groupId); - this.allGroups.put(groupId, group); - } + ConfigurationMetadataGroup group = this.allGroups.computeIfAbsent(groupId, + (key) -> new ConfigurationMetadataGroup(groupId)); String sourceType = source.getType(); if (sourceType != null) { addOrMergeSource(group.getSources(), sourceType, source); @@ -74,9 +71,9 @@ public class SimpleConfigurationMetadataRepository implements ConfigurationMetad */ public void add(ConfigurationMetadataProperty property, ConfigurationMetadataSource source) { if (source != null) { - putIfAbsent(source.getProperties(), property.getId(), property); + source.getProperties().putIfAbsent(property.getId(), property); } - putIfAbsent(getGroup(source).getProperties(), property.getId(), property); + getGroup(source).getProperties().putIfAbsent(property.getId(), property); } /** @@ -91,7 +88,7 @@ public class SimpleConfigurationMetadataRepository implements ConfigurationMetad } else { // Merge properties - group.getProperties().forEach((name, value) -> putIfAbsent(existingGroup.getProperties(), name, value)); + group.getProperties().forEach((name, value) -> existingGroup.getProperties().putIfAbsent(name, value)); // Merge sources group.getSources().forEach((name, value) -> addOrMergeSource(existingGroup.getSources(), name, value)); } @@ -101,12 +98,7 @@ public class SimpleConfigurationMetadataRepository implements ConfigurationMetad private ConfigurationMetadataGroup getGroup(ConfigurationMetadataSource source) { if (source == null) { - ConfigurationMetadataGroup rootGroup = this.allGroups.get(ROOT_GROUP); - if (rootGroup == null) { - rootGroup = new ConfigurationMetadataGroup(ROOT_GROUP); - this.allGroups.put(ROOT_GROUP, rootGroup); - } - return rootGroup; + return this.allGroups.computeIfAbsent(ROOT_GROUP, (key) -> new ConfigurationMetadataGroup(ROOT_GROUP)); } return this.allGroups.get(source.getGroupId()); } @@ -118,13 +110,7 @@ public class SimpleConfigurationMetadataRepository implements ConfigurationMetad sources.put(name, source); } else { - source.getProperties().forEach((k, v) -> putIfAbsent(existingSource.getProperties(), k, v)); - } - } - - private void putIfAbsent(Map map, String key, V value) { - if (!map.containsKey(key)) { - map.put(key, value); + source.getProperties().forEach((k, v) -> existingSource.getProperties().putIfAbsent(k, v)); } }