Skip to content

Commit 7c10d53

Browse files
committed
Allow use of map for qualifiers in constructor
The `PackageURL` constructor should not require the use of `TreeMap` and can just convert the map to a `TreeMap` if it isn't already.
1 parent ee6dda9 commit 7c10d53

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public PackageURL(final String type, final String name) throws MalformedPackageU
9494
public PackageURL(final String type, final String namespace, final String name, final String version,
9595
final TreeMap<String, String> qualifiers, final String subpath)
9696
throws MalformedPackageURLException {
97-
9897
this.scheme = validateScheme("pkg");
9998
this.type = validateType(type);
10099
this.namespace = validateNamespace(namespace);
@@ -105,6 +104,24 @@ public PackageURL(final String type, final String namespace, final String name,
105104
verifyTypeConstraints(this.type, this.namespace, this.name);
106105
}
107106

107+
/**
108+
* Constructs a new PackageURL object.
109+
*
110+
* @param type the type of package (i.e. maven, npm, gem, etc)
111+
* @param namespace the name prefix (i.e. group, owner, organization)
112+
* @param name the name of the package
113+
* @param version the version of the package
114+
* @param qualifiers an array of key/value pair qualifiers
115+
* @param subpath the subpath string
116+
* @throws MalformedPackageURLException if parsing fails
117+
* @since 1.0.0
118+
*/
119+
public PackageURL(final String type, final String namespace, final String name, final String version,
120+
final Map<String, String> qualifiers, final String subpath)
121+
throws MalformedPackageURLException {
122+
this(type, namespace, name, version, (qualifiers == null) ? null : ((qualifiers instanceof TreeMap) ? (TreeMap<String, String>) qualifiers : new TreeMap<>(qualifiers)), subpath);
123+
}
124+
108125
/**
109126
* The PackageURL scheme constant
110127
*/

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import java.io.IOException;
2525
import java.io.InputStream;
26+
import java.util.HashMap;
27+
import java.util.Map;
2628
import java.util.TreeMap;
2729

2830
import org.apache.commons.io.IOUtils;
@@ -128,14 +130,18 @@ public void testConstructorParameters() throws MalformedPackageURLException {
128130
final String subpath = testDefinition.optString("subpath", null);
129131

130132
TreeMap<String, String> map = null;
133+
Map<String, String> hashMap = null;
131134
if (qualifiers != null) {
132135
map = qualifiers.toMap().entrySet().stream().collect(
133136
TreeMap<String, String>::new,
134137
(qmap, entry) -> qmap.put(entry.getKey(), (String) entry.getValue()),
135138
TreeMap<String, String>::putAll
136139
);
140+
hashMap = new HashMap<>(map);
137141
}
138142

143+
144+
139145
if (invalid) {
140146
try {
141147
PackageURL purl = new PackageURL(type, namespace, name, version, map, subpath);
@@ -163,6 +169,8 @@ public void testConstructorParameters() throws MalformedPackageURLException {
163169
Assert.assertTrue(purl.getQualifiers().containsKey(key));
164170
Assert.assertEquals(value, purl.getQualifiers().get(key));
165171
});
172+
PackageURL purl2 = new PackageURL(type, namespace, name, version, hashMap, subpath);
173+
Assert.assertEquals(purl.getQualifiers(), purl2.getQualifiers());
166174
}
167175
}
168176
}

0 commit comments

Comments
 (0)