Skip to content

Commit

Permalink
[MRESOLVER-586] Avoid copying the type default properties map
Browse files Browse the repository at this point in the history
When we don't have specific properties for a DefaultArtifact, we can
just reuse the type default properties map as it's readonly and
immutable.
The patch contains some additional changes as I renamed the method
and parameters to make them more specific so that the method
wouldn't be used for things that wouldn't be safe.
  • Loading branch information
gsmet committed Jul 13, 2024
1 parent d13019c commit e519e54
Showing 1 changed file with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public DefaultArtifact(String coords, Map<String, String> properties, ArtifactTy
classifier = get(m.group(6), type == null ? "" : type.getClassifier());
this.version = emptify(m.group(7));
this.path = null;
this.properties = merge(properties, (type != null) ? type.getProperties() : null);
this.properties = mergeArtifactProperties(properties, (type != null) ? type.getProperties() : null);
}

private static String get(String value, String defaultValue) {
Expand Down Expand Up @@ -207,21 +207,27 @@ public DefaultArtifact(
}
this.version = emptify(version);
this.path = null;
this.properties = merge(properties, (type != null) ? type.getProperties() : null);
this.properties = mergeArtifactProperties(properties, (type != null) ? type.getProperties() : null);
}

private static Map<String, String> merge(Map<String, String> dominant, Map<String, String> recessive) {
private static Map<String, String> mergeArtifactProperties(
Map<String, String> artifactProperties, Map<String, String> typeDefaultProperties) {
Map<String, String> properties;

if ((dominant == null || dominant.isEmpty()) && (recessive == null || recessive.isEmpty())) {
properties = Collections.emptyMap();
if (artifactProperties == null || artifactProperties.isEmpty()) {
if (typeDefaultProperties == null || typeDefaultProperties.isEmpty()) {
properties = Collections.emptyMap();
} else {
// type default properties are already unmodifiable
return typeDefaultProperties;
}
} else {
properties = new HashMap<>();
if (recessive != null) {
properties.putAll(recessive);
if (typeDefaultProperties != null) {
properties.putAll(typeDefaultProperties);
}
if (dominant != null) {
properties.putAll(dominant);
if (artifactProperties != null) {
properties.putAll(artifactProperties);
}
properties = Collections.unmodifiableMap(properties);
}
Expand Down

0 comments on commit e519e54

Please sign in to comment.