Skip to content

Commit ed3a681

Browse files
authored
fix: make PackageURLBuilder::getQualifiers return Map (#157)
The return type of the method `PackageURLBuilder::getQualifiers` should be an interface, such as `Map`, rather than the implementation `TreeMap`. This aligns with the return type of the method `PackageURL::getQualifiers`. Also, make the return types of this method match the return types of `PackageURL::getQualifiers`. Note that this a potentially breaking API change.
1 parent 9bf2d2b commit ed3a681

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

src/main/java/com/github/packageurl/PackageURLBuilder.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
package com.github.packageurl;
2323

24+
import java.util.Collections;
25+
import java.util.Map;
2426
import java.util.TreeMap;
2527

2628
/**
@@ -132,7 +134,9 @@ public PackageURLBuilder withQualifier(final String key, final String value) {
132134
public PackageURLBuilder withoutQualifier(final String key) {
133135
if (qualifiers != null) {
134136
qualifiers.remove(key);
135-
if (qualifiers.isEmpty()) { qualifiers = null; }
137+
if (qualifiers.isEmpty()) {
138+
qualifiers = null;
139+
}
136140
}
137141
return this;
138142
}
@@ -191,9 +195,11 @@ public String getSubpath() {
191195
* An empty map is returned if no qualifiers is set.
192196
* @return all qualifiers set in this builder, or an empty map if none are set.
193197
*/
194-
public TreeMap<String, String> getQualifiers() {
195-
if (qualifiers == null) { return new TreeMap<>(); }
196-
return new TreeMap<>(qualifiers);
198+
public Map<String, String> getQualifiers() {
199+
if (qualifiers == null) {
200+
return null;
201+
}
202+
return Collections.unmodifiableMap(qualifiers);
197203
}
198204

199205
/**
@@ -202,7 +208,9 @@ public TreeMap<String, String> getQualifiers() {
202208
* @return qualifier value or {@code null} if one is not set.
203209
*/
204210
public String getQualifier(String key) {
205-
if (qualifiers == null) { return null; }
211+
if (qualifiers == null) {
212+
return null;
213+
}
206214
return qualifiers.get(key);
207215
}
208216

src/test/java/com/github/packageurl/PackageURLBuilderTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,13 @@ private void assertBuilderMatch(PackageURL expected, PackageURLBuilder actual) t
201201
Map<String, String> eQualifiers = expected.getQualifiers();
202202
Map<String, String> aQualifiers = actual.getQualifiers();
203203

204-
if (eQualifiers != null) {
205-
eQualifiers.forEach((k,v)-> {
206-
Assert.assertEquals(v, aQualifiers.remove(k));
204+
assertEquals(eQualifiers, aQualifiers);
205+
206+
if (eQualifiers != null && aQualifiers != null) {
207+
eQualifiers.forEach((k,v) -> {
207208
Assert.assertEquals(v, actual.getQualifier(k));
208209
});
209210
}
210-
211-
Assert.assertTrue(aQualifiers.isEmpty());
212-
213211
}
214212

215213
}

0 commit comments

Comments
 (0)