Skip to content

Commit f16a2eb

Browse files
authored
fix: make private methods static where possible (#214)
1 parent 09a8645 commit f16a2eb

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ public PackageURL(
155155
final @Nullable String subpath)
156156
throws MalformedPackageURLException {
157157
this.type = toLowerCase(validateType(requireNonNull(type, "type")));
158-
this.namespace = validateNamespace(namespace);
159-
this.name = validateName(requireNonNull(name, "name"));
158+
this.namespace = validateNamespace(this.type, namespace);
159+
this.name = validateName(this.type, requireNonNull(name, "name"));
160160
this.version = validateVersion(type, version);
161161
this.qualifiers = parseQualifiers(qualifiers);
162162
this.subpath = validateSubpath(subpath);
@@ -274,7 +274,7 @@ public Map<String, String> getQualifiers() {
274274
return subpath;
275275
}
276276

277-
private void validateScheme(final String value) throws MalformedPackageURLException {
277+
private static void validateScheme(final String value) throws MalformedPackageURLException {
278278
if (!SCHEME.equals(value)) {
279279
throw new MalformedPackageURLException(
280280
"The PackageURL scheme '" + value + "' is invalid. It should be '" + SCHEME + "'");
@@ -319,14 +319,16 @@ private static void validateChars(String value, IntPredicate predicate, String c
319319
}
320320
}
321321

322-
private @Nullable String validateNamespace(final @Nullable String value) throws MalformedPackageURLException {
322+
private static @Nullable String validateNamespace(final String type, final @Nullable String value)
323+
throws MalformedPackageURLException {
323324
if (isEmpty(value)) {
324325
return null;
325326
}
326-
return validateNamespace(value.split("/"));
327+
return validateNamespace(type, value.split("/"));
327328
}
328329

329-
private @Nullable String validateNamespace(final String[] values) throws MalformedPackageURLException {
330+
private static @Nullable String validateNamespace(final String type, final String[] values)
331+
throws MalformedPackageURLException {
330332
if (values.length == 0) {
331333
return null;
332334
}
@@ -360,7 +362,7 @@ private static void validateChars(String value, IntPredicate predicate, String c
360362
return retVal;
361363
}
362364

363-
private String validateName(final String value) throws MalformedPackageURLException {
365+
private static String validateName(final String type, final String value) throws MalformedPackageURLException {
364366
if (value.isEmpty()) {
365367
throw new MalformedPackageURLException("The PackageURL name specified is invalid");
366368
}
@@ -391,7 +393,7 @@ private String validateName(final String value) throws MalformedPackageURLExcept
391393
return temp;
392394
}
393395

394-
private @Nullable String validateVersion(final String type, final @Nullable String value) {
396+
private static @Nullable String validateVersion(final String type, final @Nullable String value) {
395397
if (value == null) {
396398
return null;
397399
}
@@ -406,7 +408,7 @@ private String validateName(final String value) throws MalformedPackageURLExcept
406408
}
407409
}
408410

409-
private @Nullable Map<String, String> validateQualifiers(final @Nullable Map<String, String> values)
411+
private static @Nullable Map<String, String> validateQualifiers(final @Nullable Map<String, String> values)
410412
throws MalformedPackageURLException {
411413
if (values == null || values.isEmpty()) {
412414
return null;
@@ -436,7 +438,7 @@ private static void validateValue(final String key, final @Nullable String value
436438
}
437439
}
438440

439-
private @Nullable String validateSubpath(final @Nullable String value) throws MalformedPackageURLException {
441+
private static @Nullable String validateSubpath(final @Nullable String value) throws MalformedPackageURLException {
440442
if (isEmpty(value)) {
441443
return null;
442444
}
@@ -764,11 +766,11 @@ private void parse(final String purl) throws MalformedPackageURLException {
764766
// The 'remainder' should now consist of an optional namespace and the name
765767
index = remainder.lastIndexOf('/');
766768
if (index <= start) {
767-
this.name = validateName(percentDecode(remainder.substring(start)));
769+
this.name = validateName(this.type, percentDecode(remainder.substring(start)));
768770
} else {
769-
this.name = validateName(percentDecode(remainder.substring(index + 1)));
771+
this.name = validateName(this.type, percentDecode(remainder.substring(index + 1)));
770772
remainder = remainder.substring(0, index);
771-
this.namespace = validateNamespace(parsePath(remainder.substring(start), false));
773+
this.namespace = validateNamespace(this.type, parsePath(remainder.substring(start), false));
772774
}
773775
verifyTypeConstraints(this.type, this.namespace, this.name);
774776
} catch (URISyntaxException e) {
@@ -782,7 +784,7 @@ private void parse(final String purl) throws MalformedPackageURLException {
782784
* @param namespace the purl namespace
783785
* @throws MalformedPackageURLException if constraints are not met
784786
*/
785-
private void verifyTypeConstraints(String type, @Nullable String namespace, @Nullable String name)
787+
private static void verifyTypeConstraints(String type, @Nullable String namespace, @Nullable String name)
786788
throws MalformedPackageURLException {
787789
if (StandardTypes.MAVEN.equals(type)) {
788790
if (isEmpty(namespace) || isEmpty(name)) {
@@ -792,7 +794,7 @@ private void verifyTypeConstraints(String type, @Nullable String namespace, @Nul
792794
}
793795
}
794796

795-
private @Nullable Map<String, String> parseQualifiers(final @Nullable Map<String, String> qualifiers)
797+
private static @Nullable Map<String, String> parseQualifiers(final @Nullable Map<String, String> qualifiers)
796798
throws MalformedPackageURLException {
797799
if (qualifiers == null || qualifiers.isEmpty()) {
798800
return null;
@@ -812,7 +814,7 @@ private void verifyTypeConstraints(String type, @Nullable String namespace, @Nul
812814
}
813815

814816
@SuppressWarnings("StringSplitter") // reason: surprising behavior is okay in this case
815-
private @Nullable Map<String, String> parseQualifiers(final String encodedString)
817+
private static @Nullable Map<String, String> parseQualifiers(final String encodedString)
816818
throws MalformedPackageURLException {
817819
try {
818820
final TreeMap<String, String> results = Arrays.stream(encodedString.split("&"))
@@ -836,14 +838,14 @@ private void verifyTypeConstraints(String type, @Nullable String namespace, @Nul
836838
}
837839
}
838840

839-
private String[] parsePath(final String path, final boolean isSubpath) {
841+
private static String[] parsePath(final String path, final boolean isSubpath) {
840842
return Arrays.stream(path.split("/"))
841843
.filter(segment -> !segment.isEmpty() && !(isSubpath && (".".equals(segment) || "..".equals(segment))))
842844
.map(PackageURL::percentDecode)
843845
.toArray(String[]::new);
844846
}
845847

846-
private String encodePath(final String path) {
848+
private static String encodePath(final String path) {
847849
return Arrays.stream(path.split("/")).map(PackageURL::percentEncode).collect(Collectors.joining("/"));
848850
}
849851

0 commit comments

Comments
 (0)