Skip to content

Commit 1b7ae52

Browse files
dwalluckjeremylong
andauthored
feat: add withQualifiers(Map) to PackageURLBuilder (#158)
* Add `withQualifiers(Map)` to `PackageURLBuilder` This allows passing a map of qualifiers directly to `PackageURLBuilder` instead of having to call `withQualifier` for every key-value pair. This aligns with the `PackageURL` constructor which allows passing such a map. * Add `withoutQualifiers(Set)` and `withoutQualifiers()` * Use consistent formatting --------- Co-authored-by: Jeremy Long <jeremy.long@gmail.com>
1 parent a0f623e commit 1b7ae52

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

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

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

24+
import java.util.Map;
25+
import java.util.Set;
2426
import java.util.Collections;
2527
import java.util.Map;
2628
import java.util.TreeMap;
@@ -37,7 +39,7 @@ public final class PackageURLBuilder {
3739
private TreeMap<String, String> qualifiers = null;
3840

3941
private PackageURLBuilder() {
40-
//empty constructor for utility class
42+
// empty constructor for utility class
4143
}
4244

4345
/**
@@ -126,6 +128,26 @@ public PackageURLBuilder withQualifier(final String key, final String value) {
126128
return this;
127129
}
128130

131+
/**
132+
* Adds the package qualifiers.
133+
*
134+
* @param qualifiers the package qualifiers
135+
* @return a reference to the builder
136+
* @see PackageURL#getQualifiers()
137+
*/
138+
public PackageURLBuilder withQualifiers(final Map<String, String> qualifiers) {
139+
if (qualifiers == null) {
140+
this.qualifiers = null;
141+
} else {
142+
if (this.qualifiers == null) {
143+
this.qualifiers = new TreeMap<>(qualifiers);
144+
} else {
145+
this.qualifiers.putAll(qualifiers);
146+
}
147+
}
148+
return this;
149+
}
150+
129151
/**
130152
* Removes a package qualifier. This is a no-op if the qualifier is not present.
131153
* @param key the package qualifier key to remove
@@ -141,6 +163,31 @@ public PackageURLBuilder withoutQualifier(final String key) {
141163
return this;
142164
}
143165

166+
/**
167+
* Removes a package qualifier. This is a no-op if the qualifier is not present.
168+
* @param keys the package qualifier keys to remove
169+
* @return a reference to the builder
170+
*/
171+
public PackageURLBuilder withoutQualifiers(final Set<String> keys) {
172+
if (this.qualifiers != null) {
173+
keys.forEach(k -> this.qualifiers.remove(k));
174+
if (this.qualifiers.isEmpty()) {
175+
this.qualifiers = null;
176+
}
177+
}
178+
return this;
179+
}
180+
181+
182+
/**
183+
* Removes all qualifiers, if any.
184+
* @return a reference to this builder.
185+
*/
186+
public PackageURLBuilder withoutQualifiers() {
187+
qualifiers = null;
188+
return this;
189+
}
190+
144191
/**
145192
* Removes all qualifiers, if any.
146193
* @return a reference to this builder.

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.junit.Test;
2727
import org.junit.rules.ExpectedException;
2828

29+
import java.util.Collections;
30+
import java.util.HashMap;
2931
import java.util.Map;
3032

3133
import static org.junit.Assert.*;
@@ -189,6 +191,29 @@ public void testEditBuilder1() throws MalformedPackageURLException {
189191

190192
}
191193

194+
@Test
195+
public void testQualifiers() throws MalformedPackageURLException {
196+
Map<String, String> qualifiers = new HashMap<>();
197+
qualifiers.put("key2", "value2");
198+
Map<String, String> qualifiers2 = new HashMap<>();
199+
qualifiers.put("key3", "value3");
200+
PackageURL purl = PackageURLBuilder.aPackageURL()
201+
.withType(PackageURL.StandardTypes.GENERIC)
202+
.withNamespace("")
203+
.withName("name")
204+
.withVersion("version")
205+
.withQualifier("key", "value")
206+
.withQualifier("next", "value")
207+
.withQualifiers(qualifiers)
208+
.withQualifier("key4", "value4")
209+
.withQualifiers(qualifiers2)
210+
.withSubpath("")
211+
.withoutQualifiers(Collections.singleton("key4"))
212+
.build();
213+
214+
assertEquals("pkg:generic/name@version?key=value&key2=value2&key3=value3&next=value", purl.toString());
215+
}
216+
192217
private void assertBuilderMatch(PackageURL expected, PackageURLBuilder actual) throws MalformedPackageURLException {
193218

194219
Assert.assertEquals(expected.toString(), actual.build().toString());

0 commit comments

Comments
 (0)