Skip to content

Commit e83f643

Browse files
committed
Add withoutQualifiers(Set) and withoutQualifiers()
1 parent f7d8a54 commit e83f643

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
package com.github.packageurl;
2323

2424
import java.util.Map;
25+
import java.util.Set;
2526
import java.util.TreeMap;
2627

2728
/**
@@ -36,7 +37,7 @@ public final class PackageURLBuilder {
3637
private TreeMap<String, String> qualifiers = null;
3738

3839
private PackageURLBuilder() {
39-
//empty constructor for utility class
40+
// empty constructor for utility class
4041
}
4142

4243
/**
@@ -126,7 +127,7 @@ public PackageURLBuilder withQualifier(final String key, final String value) {
126127
}
127128

128129
/**
129-
* Adds the package qualifiers .
130+
* Adds the package qualifiers.
130131
*
131132
* @param qualifiers the package qualifiers
132133
* @return a reference to the builder
@@ -158,6 +159,29 @@ public PackageURLBuilder withoutQualifier(final String key) {
158159
return this;
159160
}
160161

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

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.junit.Test;
2727
import org.junit.rules.ExpectedException;
2828

29+
import java.util.Collections;
2930
import java.util.HashMap;
3031
import java.util.Map;
3132

@@ -211,9 +212,10 @@ public void testQualifiers() throws MalformedPackageURLException {
211212
.withQualifier("key4", "value4")
212213
.withQualifiers(qualifiers2)
213214
.withSubpath("")
215+
.withoutQualifiers(Collections.singleton("key4"))
214216
.build();
215217

216-
assertEquals("pkg:generic/name@version?key=value&key2=value2&key3=value3&key4=value4&next=value", purl.toString());
218+
assertEquals("pkg:generic/name@version?key=value&key2=value2&key3=value3&next=value", purl.toString());
217219
}
218220

219221
private void assertBuilderMatch(PackageURL expected, PackageURLBuilder actual) throws MalformedPackageURLException {

0 commit comments

Comments
 (0)