Skip to content

Commit 41dc212

Browse files
committed
Add javadoc and fix one null issue
1 parent 57d2ab4 commit 41dc212

File tree

4 files changed

+173
-15
lines changed

4 files changed

+173
-15
lines changed

src/main/java/com/github/packageurl/internal/PackageTypeFactory.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,29 @@
4040
import org.jspecify.annotations.NonNull;
4141
import org.jspecify.annotations.Nullable;
4242

43+
/**
44+
* The PackageTypeFactory is a singleton that provides a way to normalize and validate package URLs.
45+
* It uses the ServiceLoader mechanism to load available PackageTypeProvider implementations.
46+
* This class is not intended to be instantiated directly.
47+
*/
4348
@ServiceConsumer(
4449
value = PackageTypeProvider.class,
4550
resolution = Resolution.MANDATORY,
4651
cardinality = Cardinality.MULTIPLE)
4752
public final class PackageTypeFactory implements PackageTypeProvider {
4853
private static final @NonNull PackageTypeFactory INSTANCE = new PackageTypeFactory();
4954

50-
public static final @NonNull String TYPE = "__packagetypefactory__";
55+
private static final @NonNull String TYPE = "__packagetypefactory__";
5156

5257
private @Nullable Map<@NonNull String, @NonNull PackageTypeProvider> packageTypeProviders;
5358

5459
private PackageTypeFactory() {}
5560

61+
/**
62+
* Returns the singleton instance of PackageTypeFactory.
63+
*
64+
* @return the singleton instance of PackageTypeFactory
65+
*/
5666
public static @NonNull PackageTypeFactory getInstance() {
5767
return INSTANCE;
5868
}
@@ -116,6 +126,13 @@ private static void validateQualifiers(@Nullable Map<String, String> qualifiers)
116126
}
117127
}
118128

129+
/**
130+
* Validates the type of the package URL. The type must start with an alphabetic character and
131+
* can only contain alphanumeric characters, underscores, and hyphens.
132+
*
133+
* @param type the type of the package URL
134+
* @throws MalformedPackageURLException if the type is invalid
135+
*/
119136
public static void validateType(@NonNull String type) throws MalformedPackageURLException {
120137
if (type.isEmpty()) {
121138
throw new MalformedPackageURLException("a type is always required");
@@ -207,6 +224,12 @@ public void validateComponents(
207224
});
208225
}
209226

227+
/**
228+
* Returns a map of available package type providers. The map is unmodifiable and contains the
229+
* package type as the key and the corresponding PackageTypeProvider as the value.
230+
*
231+
* @return a map of available package type providers
232+
*/
210233
public @NonNull Map<String, PackageTypeProvider> getPackageTypeProviders() {
211234
if (packageTypeProviders == null) {
212235
packageTypeProviders = findAvailablePackageTypeProviders();

src/main/java/com/github/packageurl/internal/PackageTypeProviders.java

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,35 @@
3232
import org.jspecify.annotations.NonNull;
3333
import org.jspecify.annotations.Nullable;
3434

35+
/**
36+
* This class provides a set of package type providers for different package types.
37+
* Each provider implements the PackageTypeProvider interface and provides its own
38+
* validation and normalization logic for the components of a package URL.
39+
*/
3540
public final class PackageTypeProviders {
3641
private PackageTypeProviders() {}
3742

43+
/**
44+
* This class provides a package type provider for the "apk" package type.
45+
*/
3846
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
3947
public static class Apk extends LowercaseNamespaceAndNameTypeProvider {}
4048

49+
/**
50+
* This class provides a package type provider for the "bitbucket" package type.
51+
*/
4152
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
4253
public static class Bitbucket extends LowercaseNamespaceAndNameTypeProvider {}
4354

55+
/**
56+
* This class provides a package type provider for the "bitnami" package type.
57+
*/
4458
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
4559
public static class Bitnami extends LowercaseNamespacePackageTypeProvider {}
4660

61+
/**
62+
* This class provides a package type provider for the "cocoapods" package type.
63+
*/
4764
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
4865
public static class Cocoapods implements PackageTypeProvider {
4966
@Override
@@ -65,9 +82,15 @@ public void validateComponents(
6582
}
6683
}
6784

85+
/**
86+
* This class provides a package type provider for the "composer" package type.
87+
*/
6888
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
6989
public static class Composer extends LowercaseNamespaceAndNameTypeProvider {}
7090

91+
/**
92+
* This class provides a package type provider for the "conan" package type.
93+
*/
7194
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
7295
public static class Conan implements PackageTypeProvider {
7396
@Override
@@ -89,6 +112,9 @@ public void validateComponents(
89112
}
90113
}
91114

115+
/**
116+
* This class provides a package type provider for the "cpan" package type.
117+
*/
92118
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
93119
public static class Cpan implements PackageTypeProvider {
94120
@Override
@@ -108,6 +134,9 @@ public void validateComponents(
108134
}
109135
}
110136

137+
/**
138+
* This class provides a package type provider for the "cran" package type.
139+
*/
111140
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
112141
public static class Cran implements PackageTypeProvider {
113142
@Override
@@ -125,18 +154,33 @@ public void validateComponents(
125154
}
126155
}
127156

157+
/**
158+
* This class provides a package type provider for the "deb" package type.
159+
*/
128160
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
129161
public static class Deb extends LowercaseNamespaceAndNameTypeProvider {}
130162

163+
/**
164+
* This class provides a package type provider for the "generic" package type.
165+
*/
131166
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
132167
public static class Generic implements PackageTypeProvider {}
133168

169+
/**
170+
* This class provides a package type provider for the "github" package type.
171+
*/
134172
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
135173
public static class Github extends LowercaseNamespaceAndNameTypeProvider {}
136174

175+
/**
176+
* This class provides a package type provider for the "golang" package type.
177+
*/
137178
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
138179
public static class Golang extends LowercaseNamespacePackageTypeProvider {}
139180

181+
/**
182+
* This class provides a package type provider for the "hackage" package type.
183+
*/
140184
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
141185
public static class Hackage implements PackageTypeProvider {
142186
@Override
@@ -154,15 +198,27 @@ public void validateComponents(
154198
}
155199
}
156200

201+
/**
202+
* This class provides a package type provider for the "hex" package type.
203+
*/
157204
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
158205
public static class Hex extends LowercaseNamespaceAndNameTypeProvider {}
159206

207+
/**
208+
* This class provides a package type provider for the "huggingface" package type.
209+
*/
160210
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
161211
public static class Huggingface extends LowercaseVersionPackageTypeProvider {}
162212

213+
/**
214+
* This class provides a package type provider for the "luarocks" package type.
215+
*/
163216
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
164217
public static class Luarocks extends LowercaseVersionPackageTypeProvider {}
165218

219+
/**
220+
* This class provides a package type provider for the "maven" package type.
221+
*/
166222
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
167223
public static class Maven implements PackageTypeProvider {
168224
@Override
@@ -180,9 +236,12 @@ public void validateComponents(
180236
}
181237
}
182238

239+
/**
240+
* This class provides a package type provider for the "mlflow" package type.
241+
*/
183242
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
184243
public static class Mlflow implements PackageTypeProvider {
185-
private static @Nullable String normalizeName(@Nullable String name, @Nullable Map<String, String> qualifiers)
244+
private static @NonNull String normalizeName(@NonNull String name, @Nullable Map<String, String> qualifiers)
186245
throws MalformedPackageURLException {
187246
if (qualifiers != null) {
188247
String repositoryUrl = qualifiers.get("repository_url");
@@ -194,7 +253,7 @@ public static class Mlflow implements PackageTypeProvider {
194253
URI url = new URI(repositoryUrl);
195254
host = url.getHost();
196255

197-
if (name != null && host.matches(".*[.]?azuredatabricks.net$")) {
256+
if (host.matches(".*[.]?azuredatabricks.net$")) {
198257
return StringUtil.toLowerCase(name);
199258
}
200259
} catch (URISyntaxException e) {
@@ -234,6 +293,9 @@ public void validateComponents(
234293
}
235294
}
236295

296+
/**
297+
* This class provides a package type provider for the "oci" package type.
298+
*/
237299
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
238300
public static class Oci extends LowercaseNameAndVersionPackageTypeProvider {
239301
@Override
@@ -251,9 +313,15 @@ public void validateComponents(
251313
}
252314
}
253315

316+
/**
317+
* This class provides a package type provider for the "pub" package type.
318+
*/
254319
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
255320
public static class Pub extends LowercaseNamePackageTypeProvider {}
256321

322+
/**
323+
* This class provides a package type provider for the "pypi" package type.
324+
*/
257325
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
258326
public static class Pypi implements PackageTypeProvider {
259327
@Override
@@ -270,12 +338,21 @@ public static class Pypi implements PackageTypeProvider {
270338
}
271339
}
272340

341+
/**
342+
* This class provides a package type provider for the "qpkg" package type.
343+
*/
273344
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
274345
public static class Qpkg extends LowercaseNamespacePackageTypeProvider {}
275346

347+
/**
348+
* This class provides a package type provider for the "rpm" package type.
349+
*/
276350
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
277351
public static class Rpm extends LowercaseNamespacePackageTypeProvider {}
278352

353+
/**
354+
* This class provides a package type provider for the "swift" package type.
355+
*/
279356
@ServiceProvider(value = PackageTypeProvider.class, resolution = Resolution.MANDATORY)
280357
public static class Swift implements PackageTypeProvider {
281358
@Override
@@ -297,6 +374,9 @@ public void validateComponents(
297374
}
298375
}
299376

377+
/**
378+
* This class provides a common interface for the lowercase name and version package types.
379+
*/
300380
public static class LowercaseNameAndVersionPackageTypeProvider implements PackageTypeProvider {
301381
@Override
302382
public @NonNull PackageURL normalizeComponents(
@@ -317,6 +397,9 @@ public static class LowercaseNameAndVersionPackageTypeProvider implements Packag
317397
}
318398
}
319399

400+
/**
401+
* This class provides a common interface for the lowercase name package types.
402+
*/
320403
public static class LowercaseNamePackageTypeProvider implements PackageTypeProvider {
321404
@Override
322405
public @NonNull PackageURL normalizeComponents(
@@ -331,6 +414,9 @@ public static class LowercaseNamePackageTypeProvider implements PackageTypeProvi
331414
}
332415
}
333416

417+
/**
418+
* This class provides a common interface for the lowercase namespace and name package types.
419+
*/
334420
public static class LowercaseNamespaceAndNameTypeProvider implements PackageTypeProvider {
335421
@Override
336422
public @NonNull PackageURL normalizeComponents(
@@ -351,6 +437,9 @@ public static class LowercaseNamespaceAndNameTypeProvider implements PackageType
351437
}
352438
}
353439

440+
/**
441+
* This class provides a common interface for the lowercase namespace package types.
442+
*/
354443
public static class LowercaseNamespacePackageTypeProvider implements PackageTypeProvider {
355444
@Override
356445
public @NonNull PackageURL normalizeComponents(
@@ -371,6 +460,9 @@ public static class LowercaseNamespacePackageTypeProvider implements PackageType
371460
}
372461
}
373462

463+
/**
464+
* This class provides a common interface for the lowercase version package types.
465+
*/
374466
public static class LowercaseVersionPackageTypeProvider implements PackageTypeProvider {
375467
@Override
376468
public @NonNull PackageURL normalizeComponents(

src/main/java/com/github/packageurl/internal/StringUtil.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ private StringUtil() {
7777
*
7878
* @param s the string to convert to lower case
7979
* @return the lower case version of the string
80-
*
81-
* @since 2.0.0
8280
*/
8381
public static @NonNull String toLowerCase(@NonNull String s) {
8482
int pos = indexOfFirstUpperCaseChar(s);
@@ -101,8 +99,6 @@ private StringUtil() {
10199
*
102100
* @param source the string to decode
103101
* @return the percent decoded string
104-
*
105-
* @since 2.0.0
106102
*/
107103
public static @NonNull String percentDecode(@NonNull final String source) {
108104
if (source.indexOf(PERCENT_CHAR) == -1) {
@@ -132,8 +128,6 @@ private StringUtil() {
132128
*
133129
* @param source the string to encode
134130
* @return the percent encoded string
135-
*
136-
* @since 2.0.0
137131
*/
138132
public static @NonNull String percentEncode(@NonNull final String source) {
139133
if (!shouldEncode(source)) {
@@ -162,8 +156,6 @@ private StringUtil() {
162156
*
163157
* @param c the character to check
164158
* @return true if the character is a digit; otherwise, false
165-
*
166-
* @since 2.0.0
167159
*/
168160
public static boolean isDigit(int c) {
169161
return (c >= '0' && c <= '9');
@@ -174,8 +166,6 @@ public static boolean isDigit(int c) {
174166
*
175167
* @param c the character to check
176168
* @return true if the character is valid for the package-url type; otherwise, false
177-
*
178-
* @since 2.0.0
179169
*/
180170
public static boolean isValidCharForType(int c) {
181171
return (isAlphaNumeric(c) || c == '.' || c == '+' || c == '-');
@@ -186,8 +176,6 @@ public static boolean isValidCharForType(int c) {
186176
*
187177
* @param c the character to check
188178
* @return true if the character is valid for the package-url qualifier key; otherwise, false
189-
*
190-
* @since 2.0.0
191179
*/
192180
public static boolean isValidCharForKey(int c) {
193181
return (isAlphaNumeric(c) || c == '.' || c == '_' || c == '-');
@@ -225,6 +213,12 @@ private static boolean shouldEncode(String s) {
225213
return false;
226214
}
227215

216+
/**
217+
* Determines if the character is an ASCII alphabetic character, i.e., a lowercase or uppercase ASCII character.
218+
*
219+
* @param c the character to check
220+
* @return whether the character is an ASCII alphabetic character
221+
*/
228222
public static boolean isAlpha(int c) {
229223
return (isLowerCase(c) || isUpperCase(c));
230224
}
@@ -241,6 +235,13 @@ private static boolean isLowerCase(int c) {
241235
return (c >= 'a' && c <= 'z');
242236
}
243237

238+
/**
239+
* Determines if the character is an ASCII whitespace character, i.e.,
240+
* one of {@code ['\f', '\t', '\n', '\r', '\v', ' ']}.
241+
*
242+
* @param c the character to check
243+
* @return whether the character is an ASCII whitespace character
244+
*/
244245
public static boolean isWhitespace(int c) {
245246
if (c < 0 || c >= NBITS) {
246247
return false;

0 commit comments

Comments
 (0)