From 4292d05f0ca1229aa02ea6aaa93a4a90183f9b9e Mon Sep 17 00:00:00 2001 From: Ashley <73482956+ascopes@users.noreply.github.com> Date: Tue, 19 Mar 2024 07:59:35 +0000 Subject: [PATCH] Tidy null checks in AbstractGenerateMojo.java --- .../AbstractGenerateMojo.java | 28 +++++++------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/github/ascopes/protobufmavenplugin/AbstractGenerateMojo.java b/src/main/java/io/github/ascopes/protobufmavenplugin/AbstractGenerateMojo.java index 06c46547..00b36671 100644 --- a/src/main/java/io/github/ascopes/protobufmavenplugin/AbstractGenerateMojo.java +++ b/src/main/java/io/github/ascopes/protobufmavenplugin/AbstractGenerateMojo.java @@ -15,9 +15,6 @@ */ package io.github.ascopes.protobufmavenplugin; -import static java.util.Objects.requireNonNullElse; -import static java.util.Objects.requireNonNullElseGet; - import io.github.ascopes.protobufmavenplugin.dependency.ResolutionException; import io.github.ascopes.protobufmavenplugin.generate.ImmutableGenerationRequest; import io.github.ascopes.protobufmavenplugin.generate.SourceCodeGenerator; @@ -376,12 +373,11 @@ public void execute() throws MojoExecutionException, MojoFailureException { } var request = ImmutableGenerationRequest.builder() - .additionalImportPaths(nonNullList(additionalImportPaths)) + .additionalImportPaths(nonNullOr(additionalImportPaths, List.of())) .allowedDependencyScopes(allowedScopes()) - .binaryMavenPlugins(nonNullList(binaryMavenPlugins)) - .binaryPathPlugins(nonNullList(binaryPathPlugins)) - .binaryUrlPlugins(nonNullList(binaryUrlPlugins)) - .jvmMavenPlugins(nonNullList(jvmMavenPlugins)) + .binaryMavenPlugins(nonNullOr(binaryMavenPlugins, List.of())) + .binaryPathPlugins(nonNullOr(binaryPathPlugins, List.of())) + .binaryUrlPlugins(nonNullOr(binaryUrlPlugins, List.of())) .isCppEnabled(cppEnabled) .isCsharpEnabled(csharpEnabled) .isFailOnMissingSources(failOnMissingSources) @@ -396,15 +392,12 @@ public void execute() throws MojoExecutionException, MojoFailureException { .isRegisterAsCompilationRoot(registerAsCompilationRoot) .isRubyEnabled(rubyEnabled) .isRustEnabled(rustEnabled) + .jvmMavenPlugins(nonNullOr(jvmMavenPlugins, List.of())) .mavenSession(session) - .outputDirectory(requireNonNullElseGet( - outputDirectory, () -> defaultOutputDirectory(session) - )) + .outputDirectory(nonNullOr(outputDirectory, defaultOutputDirectory(session))) .protocVersion(protocVersion()) .sourceRootRegistrar(sourceRootRegistrar()) - .sourceRoots(requireNonNullElseGet( - sourceDirectories, () -> List.of(defaultSourceDirectory(session)) - )) + .sourceRoots(nonNullOr(sourceDirectories, List.of(defaultSourceDirectory(session)))) .build(); try { @@ -493,11 +486,10 @@ protected void validate() { private String protocVersion() { // Give precedence to overriding the protoc version via the command line // in case the Maven binaries are incompatible with the current system. - var overriddenVersion = System.getProperty("protoc.version"); - return requireNonNullElse(overriddenVersion, protocVersion); + return System.getProperty("protoc.version", protocVersion); } - private List nonNullList(@Nullable List list) { - return requireNonNullElseGet(list, List::of); + private T nonNullOr(@Nullable T item, T otherwise) { + return item == null ? otherwise : item; } }