Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/main/java/com/github/packageurl/PackageURLBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
package com.github.packageurl;

import java.util.Map;
import java.util.Set;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
Expand All @@ -37,7 +39,7 @@ public final class PackageURLBuilder {
private TreeMap<String, String> qualifiers = null;

private PackageURLBuilder() {
//empty constructor for utility class
// empty constructor for utility class
}

/**
Expand Down Expand Up @@ -126,6 +128,26 @@ public PackageURLBuilder withQualifier(final String key, final String value) {
return this;
}

/**
* Adds the package qualifiers.
*
* @param qualifiers the package qualifiers
* @return a reference to the builder
* @see PackageURL#getQualifiers()
*/
public PackageURLBuilder withQualifiers(final Map<String, String> qualifiers) {
if (qualifiers == null) {
this.qualifiers = null;
} else {
if (this.qualifiers == null) {
this.qualifiers = new TreeMap<>(qualifiers);
} else {
this.qualifiers.putAll(qualifiers);
}
}
return this;
}

/**
* Removes a package qualifier. This is a no-op if the qualifier is not present.
* @param key the package qualifier key to remove
Expand All @@ -141,6 +163,31 @@ public PackageURLBuilder withoutQualifier(final String key) {
return this;
}

/**
* Removes a package qualifier. This is a no-op if the qualifier is not present.
* @param keys the package qualifier keys to remove
* @return a reference to the builder
*/
public PackageURLBuilder withoutQualifiers(final Set<String> keys) {
if (this.qualifiers != null) {
keys.forEach(k -> this.qualifiers.remove(k));
if (this.qualifiers.isEmpty()) {
this.qualifiers = null;
}
}
return this;
}


/**
* Removes all qualifiers, if any.
* @return a reference to this builder.
*/
public PackageURLBuilder withoutQualifiers() {
qualifiers = null;
return this;
}

/**
* Removes all qualifiers, if any.
* @return a reference to this builder.
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/github/packageurl/PackageURLBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

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

}

@Test
public void testQualifiers() throws MalformedPackageURLException {
Map<String, String> qualifiers = new HashMap<>();
qualifiers.put("key2", "value2");
Map<String, String> qualifiers2 = new HashMap<>();
qualifiers.put("key3", "value3");
PackageURL purl = PackageURLBuilder.aPackageURL()
.withType(PackageURL.StandardTypes.GENERIC)
.withNamespace("")
.withName("name")
.withVersion("version")
.withQualifier("key", "value")
.withQualifier("next", "value")
.withQualifiers(qualifiers)
.withQualifier("key4", "value4")
.withQualifiers(qualifiers2)
.withSubpath("")
.withoutQualifiers(Collections.singleton("key4"))
.build();

assertEquals("pkg:generic/name@version?key=value&key2=value2&key3=value3&next=value", purl.toString());
}

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

Assert.assertEquals(expected.toString(), actual.build().toString());
Expand Down