Skip to content

Commit 92b5a1b

Browse files
committed
Allow creating a builder from a pre-existing purl
This is useful if you have an existing purl and need to modify one or more of its components. This differs from `PackageURL::toBuilder` in that it doesn't require the direct use of the `PackageURL` constructor which conflicts with the builder pattern.
1 parent ee6dda9 commit 92b5a1b

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ public PackageURL(final String type, final String namespace, final String name,
147147
private String subpath;
148148

149149
public PackageURLBuilder toBuilder() {
150-
151150
PackageURLBuilder builder = PackageURLBuilder.aPackageURL()
152151
.withType(getType())
153152
.withNamespace(getNamespace())
@@ -160,7 +159,6 @@ public PackageURLBuilder toBuilder() {
160159
}
161160

162161
return builder;
163-
164162
}
165163

166164
/**

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,26 @@ public static PackageURLBuilder aPackageURL() {
4747
return new PackageURLBuilder();
4848
}
4949

50+
/**
51+
* Obtain a reference to a new builder object initialized with the existing {@link PackageURL} object.
52+
*
53+
* @param packageURL the existing Package URL object
54+
* @return a new builder object.
55+
*/
56+
public static PackageURLBuilder aPackageURL(final PackageURL packageURL) {
57+
return packageURL.toBuilder();
58+
}
59+
60+
/**
61+
* Obtain a reference to a new builder object initialized with the existing Package URL string.
62+
*
63+
* @param purl the existing Package URL string
64+
* @return a new builder object.
65+
*/
66+
public static PackageURLBuilder aPackageURL(final String purl) throws MalformedPackageURLException {
67+
return new PackageURL(purl).toBuilder();
68+
}
69+
5070
/**
5171
* Adds the package URL type.
5272
*

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,16 @@ public void testEditBuilder1() throws MalformedPackageURLException {
193193

194194
}
195195

196+
@Test
197+
public void testFromExistingPurl() throws MalformedPackageURLException {
198+
String purl = "pkg:generic/namespace/[email protected]?k=v#s";
199+
PackageURL p = new PackageURL(purl);
200+
PackageURL purl2 = PackageURLBuilder.aPackageURL(p).build();
201+
PackageURL purl3 = PackageURLBuilder.aPackageURL(purl).build();
202+
assertEquals(p, purl2);
203+
assertEquals(purl2, purl3);
204+
}
205+
196206
private void assertBuilderMatch(PackageURL expected, PackageURLBuilder actual) throws MalformedPackageURLException {
197207

198208
Assert.assertEquals(expected.toString(), actual.build().toString());
@@ -216,4 +226,4 @@ private void assertBuilderMatch(PackageURL expected, PackageURLBuilder actual) t
216226

217227
}
218228

219-
}
229+
}

0 commit comments

Comments
 (0)