Replace contains/put/get pattern with computeIfAbsent

pull/36455/head
Moritz Halbritter 1 year ago
parent 7bb337aeb1
commit 62fb45f75f

@ -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));
}
}

@ -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;
}

@ -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));
}
/**

@ -54,11 +54,8 @@ public class SimpleConfigurationMetadataRepository implements ConfigurationMetad
public void add(Collection<ConfigurationMetadataSource> 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 <V> void putIfAbsent(Map<String, V> map, String key, V value) {
if (!map.containsKey(key)) {
map.put(key, value);
source.getProperties().forEach((k, v) -> existingSource.getProperties().putIfAbsent(k, v));
}
}

Loading…
Cancel
Save