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 @Override
public DataSourcePoolMetadata getDataSourcePoolMetadata(DataSource dataSource) { public DataSourcePoolMetadata getDataSourcePoolMetadata(DataSource dataSource) {
DataSourcePoolMetadata metadata = cache.get(dataSource); return cache.computeIfAbsent(dataSource,
if (metadata == null) { (key) -> this.metadataProvider.getDataSourcePoolMetadata(dataSource));
metadata = this.metadataProvider.getDataSourcePoolMetadata(dataSource);
cache.put(dataSource, metadata);
}
return metadata;
} }
} }

@ -80,10 +80,7 @@ public final class ConditionEvaluationReport {
Assert.notNull(condition, "Condition must not be null"); Assert.notNull(condition, "Condition must not be null");
Assert.notNull(outcome, "Outcome must not be null"); Assert.notNull(outcome, "Outcome must not be null");
this.unconditionalClasses.remove(source); this.unconditionalClasses.remove(source);
if (!this.outcomes.containsKey(source)) { this.outcomes.computeIfAbsent(source, (key) -> new ConditionAndOutcomes()).add(condition, outcome);
this.outcomes.put(source, new ConditionAndOutcomes());
}
this.outcomes.get(source).add(condition, outcome);
this.addedAncestorOutcomes = false; 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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} * @return an existing or newly added {@link SourceDirectory}
*/ */
protected final SourceDirectory getOrCreateSourceDirectory(String name) { protected final SourceDirectory getOrCreateSourceDirectory(String name) {
SourceDirectory sourceDirectory = this.sourceDirectories.get(name); return this.sourceDirectories.computeIfAbsent(name, (key) -> new SourceDirectory(name));
if (sourceDirectory == null) {
sourceDirectory = new SourceDirectory(name);
this.sourceDirectories.put(name, sourceDirectory);
}
return sourceDirectory;
} }
/** /**

@ -54,11 +54,8 @@ public class SimpleConfigurationMetadataRepository implements ConfigurationMetad
public void add(Collection<ConfigurationMetadataSource> sources) { public void add(Collection<ConfigurationMetadataSource> sources) {
for (ConfigurationMetadataSource source : sources) { for (ConfigurationMetadataSource source : sources) {
String groupId = source.getGroupId(); String groupId = source.getGroupId();
ConfigurationMetadataGroup group = this.allGroups.get(groupId); ConfigurationMetadataGroup group = this.allGroups.computeIfAbsent(groupId,
if (group == null) { (key) -> new ConfigurationMetadataGroup(groupId));
group = new ConfigurationMetadataGroup(groupId);
this.allGroups.put(groupId, group);
}
String sourceType = source.getType(); String sourceType = source.getType();
if (sourceType != null) { if (sourceType != null) {
addOrMergeSource(group.getSources(), sourceType, source); addOrMergeSource(group.getSources(), sourceType, source);
@ -74,9 +71,9 @@ public class SimpleConfigurationMetadataRepository implements ConfigurationMetad
*/ */
public void add(ConfigurationMetadataProperty property, ConfigurationMetadataSource source) { public void add(ConfigurationMetadataProperty property, ConfigurationMetadataSource source) {
if (source != null) { 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 { else {
// Merge properties // Merge properties
group.getProperties().forEach((name, value) -> putIfAbsent(existingGroup.getProperties(), name, value)); group.getProperties().forEach((name, value) -> existingGroup.getProperties().putIfAbsent(name, value));
// Merge sources // Merge sources
group.getSources().forEach((name, value) -> addOrMergeSource(existingGroup.getSources(), name, value)); group.getSources().forEach((name, value) -> addOrMergeSource(existingGroup.getSources(), name, value));
} }
@ -101,12 +98,7 @@ public class SimpleConfigurationMetadataRepository implements ConfigurationMetad
private ConfigurationMetadataGroup getGroup(ConfigurationMetadataSource source) { private ConfigurationMetadataGroup getGroup(ConfigurationMetadataSource source) {
if (source == null) { if (source == null) {
ConfigurationMetadataGroup rootGroup = this.allGroups.get(ROOT_GROUP); return this.allGroups.computeIfAbsent(ROOT_GROUP, (key) -> new ConfigurationMetadataGroup(ROOT_GROUP));
if (rootGroup == null) {
rootGroup = new ConfigurationMetadataGroup(ROOT_GROUP);
this.allGroups.put(ROOT_GROUP, rootGroup);
}
return rootGroup;
} }
return this.allGroups.get(source.getGroupId()); return this.allGroups.get(source.getGroupId());
} }
@ -118,13 +110,7 @@ public class SimpleConfigurationMetadataRepository implements ConfigurationMetad
sources.put(name, source); sources.put(name, source);
} }
else { else {
source.getProperties().forEach((k, v) -> putIfAbsent(existingSource.getProperties(), k, v)); source.getProperties().forEach((k, v) -> existingSource.getProperties().putIfAbsent(k, v));
}
}
private <V> void putIfAbsent(Map<String, V> map, String key, V value) {
if (!map.containsKey(key)) {
map.put(key, value);
} }
} }

Loading…
Cancel
Save