Skip to content

Commit 9bf2d2b

Browse files
dwalluckjeremylong
andauthored
feat: allow use of map for qualifiers in the PackageURL constructor (#155)
* 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. * Fix javadoc since version * Change constructor to make a copy of the map --------- Co-authored-by: Jeremy Long <jeremy.long@gmail.com>
1 parent b2f26f8 commit 9bf2d2b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,25 @@ public PackageURL(final String type, final String namespace, final String name,
105105
}
106106

107107
/**
108-
* The PackageURL scheme constant.
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.6.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) ? new TreeMap<>(qualifiers) : null, subpath);
123+
}
124+
125+
/**
126+
* The PackageURL scheme constant
109127
*/
110128
public static final String SCHEME = "pkg";
111129

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.Locale;
2729
import java.util.TreeMap;
2830

@@ -138,14 +140,18 @@ public void testConstructorParameters() throws MalformedPackageURLException {
138140
final String subpath = testDefinition.optString("subpath", null);
139141

140142
TreeMap<String, String> map = null;
143+
Map<String, String> hashMap = null;
141144
if (qualifiers != null) {
142145
map = qualifiers.toMap().entrySet().stream().collect(
143146
TreeMap<String, String>::new,
144147
(qmap, entry) -> qmap.put(entry.getKey(), (String) entry.getValue()),
145148
TreeMap<String, String>::putAll
146149
);
150+
hashMap = new HashMap<>(map);
147151
}
148152

153+
154+
149155
if (invalid) {
150156
try {
151157
PackageURL purl = new PackageURL(type, namespace, name, version, map, subpath);
@@ -173,6 +179,8 @@ public void testConstructorParameters() throws MalformedPackageURLException {
173179
Assert.assertTrue(purl.getQualifiers().containsKey(key));
174180
Assert.assertEquals(value, purl.getQualifiers().get(key));
175181
});
182+
PackageURL purl2 = new PackageURL(type, namespace, name, version, hashMap, subpath);
183+
Assert.assertEquals(purl.getQualifiers(), purl2.getQualifiers());
176184
}
177185
}
178186
}

0 commit comments

Comments
 (0)