Skip to content

Commit f926adf

Browse files
authored
feat: allow creating a builder from a pre-existing purl (#168)
* 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. * Fix some missing javadoc
1 parent 09c7cb2 commit f926adf

3 files changed

Lines changed: 51 additions & 7 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,13 @@ public PackageURL(final String type, final String namespace, final String name,
166166
*/
167167
private String subpath;
168168

169+
/**
170+
* Converts this {@link PackageURL} to a {@link PackageURLBuilder}.
171+
*
172+
* @return the builder
173+
* @deprecated Use {@link PackageURLBuilder#aPackageURL(PackageURL)} or {@link PackageURLBuilder#aPackageURL(String)}
174+
*/
169175
public PackageURLBuilder toBuilder() {
170-
171176
PackageURLBuilder builder = PackageURLBuilder.aPackageURL()
172177
.withType(getType())
173178
.withNamespace(getNamespace())
@@ -180,7 +185,6 @@ public PackageURLBuilder toBuilder() {
180185
}
181186

182187
return builder;
183-
184188
}
185189

186190
/**

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

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,35 @@ private PackageURLBuilder() {
4343
}
4444

4545
/**
46-
* Obtain a reference to a new builder object.
46+
* Obtains a reference to a new builder object.
4747
*
48-
* @return a new builder object.
48+
* @return a new builder object
4949
*/
5050
public static PackageURLBuilder aPackageURL() {
5151
return new PackageURLBuilder();
5252
}
5353

54+
/**
55+
* Obtains a reference to a new builder object initialized with the existing {@link PackageURL} object.
56+
*
57+
* @param packageURL the existing Package URL object
58+
* @return a new builder object
59+
*/
60+
public static PackageURLBuilder aPackageURL(final PackageURL packageURL) {
61+
return packageURL.toBuilder();
62+
}
63+
64+
/**
65+
* Obtain a reference to a new builder object initialized with the existing Package URL string.
66+
*
67+
* @param purl the existing Package URL string
68+
* @return a new builder object
69+
* @throws MalformedPackageURLException if an error occurs while parsing the input
70+
*/
71+
public static PackageURLBuilder aPackageURL(final String purl) throws MalformedPackageURLException {
72+
return new PackageURL(purl).toBuilder();
73+
}
74+
5475
/**
5576
* Adds the package URL type.
5677
*
@@ -150,6 +171,7 @@ public PackageURLBuilder withQualifiers(final Map<String, String> qualifiers) {
150171

151172
/**
152173
* Removes a package qualifier. This is a no-op if the qualifier is not present.
174+
*
153175
* @param key the package qualifier key to remove
154176
* @return a reference to the builder
155177
*/
@@ -190,6 +212,7 @@ public PackageURLBuilder withoutQualifiers() {
190212

191213
/**
192214
* Removes all qualifiers, if any.
215+
*
193216
* @return a reference to this builder.
194217
*/
195218
public PackageURLBuilder withNoQualifiers() {
@@ -199,6 +222,7 @@ public PackageURLBuilder withNoQualifiers() {
199222

200223
/**
201224
* Returns current type value set in the builder.
225+
*
202226
* @return type set in this builder
203227
*/
204228
public String getType() {
@@ -207,6 +231,7 @@ public String getType() {
207231

208232
/**
209233
* Returns current namespace value set in the builder.
234+
*
210235
* @return namespace set in this builder
211236
*/
212237
public String getNamespace() {
@@ -215,6 +240,7 @@ public String getNamespace() {
215240

216241
/**
217242
* Returns current name value set in the builder.
243+
*
218244
* @return name set in this builder
219245
*/
220246
public String getName() {
@@ -223,6 +249,7 @@ public String getName() {
223249

224250
/**
225251
* Returns current version value set in the builder.
252+
*
226253
* @return version set in this builder
227254
*/
228255
public String getVersion() {
@@ -231,6 +258,7 @@ public String getVersion() {
231258

232259
/**
233260
* Returns current subpath value set in the builder.
261+
*
234262
* @return subpath set in this builder
235263
*/
236264
public String getSubpath() {
@@ -239,8 +267,9 @@ public String getSubpath() {
239267

240268
/**
241269
* Returns sorted map containing all qualifiers set in this builder.
242-
* An empty map is returned if no qualifiers is set.
243-
* @return all qualifiers set in this builder, or an empty map if none are set.
270+
* An empty map is returned if no qualifiers are set
271+
*
272+
* @return all qualifiers set in this builder, or an empty map if none are set
244273
*/
245274
public Map<String, String> getQualifiers() {
246275
if (qualifiers == null) {
@@ -251,8 +280,9 @@ public Map<String, String> getQualifiers() {
251280

252281
/**
253282
* Returns a currently set qualifier value set in the builder for the specified key.
283+
*s
254284
* @param key qualifier key
255-
* @return qualifier value or {@code null} if one is not set.
285+
* @return qualifier value or {@code null} if one is not set
256286
*/
257287
public String getQualifier(String key) {
258288
if (qualifiers == null) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,16 @@ public void testQualifiers() throws MalformedPackageURLException {
214214
assertEquals("pkg:generic/name@version?key=value&key2=value2&key3=value3&next=value", purl.toString());
215215
}
216216

217+
@Test
218+
public void testFromExistingPurl() throws MalformedPackageURLException {
219+
String purl = "pkg:generic/namespace/name@1.0.0?k=v#s";
220+
PackageURL p = new PackageURL(purl);
221+
PackageURL purl2 = PackageURLBuilder.aPackageURL(p).build();
222+
PackageURL purl3 = PackageURLBuilder.aPackageURL(purl).build();
223+
assertEquals(p, purl2);
224+
assertEquals(purl2, purl3);
225+
}
226+
217227
private void assertBuilderMatch(PackageURL expected, PackageURLBuilder actual) throws MalformedPackageURLException {
218228

219229
Assert.assertEquals(expected.toString(), actual.build().toString());

0 commit comments

Comments
 (0)