Skip to content

Commit

Permalink
[MNG-8483] Prefer Objects.requireNonNull (#2019)
Browse files Browse the repository at this point in the history
* Prefer Objects.requireNonNull
  • Loading branch information
elharo authored Jan 10, 2025
1 parent 529b78e commit 03cca08
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.NotThreadSafe;

import static org.apache.maven.api.services.BaseRequest.nonNull;
import static java.util.Objects.requireNonNull;

/**
* A request for creating a {@link ArtifactCoordinates} object.
Expand Down Expand Up @@ -57,7 +57,7 @@ public interface ArtifactCoordinatesFactoryRequest {
static ArtifactCoordinatesFactoryRequest build(
@Nonnull Session session, String groupId, String artifactId, String version, String extension) {
return ArtifactCoordinatesFactoryRequest.builder()
.session(nonNull(session, "session"))
.session(requireNonNull(session, "session"))
.groupId(groupId)
.artifactId(artifactId)
.version(version)
Expand All @@ -75,7 +75,7 @@ static ArtifactCoordinatesFactoryRequest build(
String extension,
String type) {
return ArtifactCoordinatesFactoryRequest.builder()
.session(nonNull(session, "session"))
.session(requireNonNull(session, "session"))
.groupId(groupId)
.artifactId(artifactId)
.version(version)
Expand All @@ -88,16 +88,16 @@ static ArtifactCoordinatesFactoryRequest build(
@Nonnull
static ArtifactCoordinatesFactoryRequest build(@Nonnull Session session, @Nonnull String coordinateString) {
return ArtifactCoordinatesFactoryRequest.builder()
.session(nonNull(session, "session"))
.coordinateString(nonNull(coordinateString, "coordinateString"))
.session(requireNonNull(session, "session"))
.coordinateString(requireNonNull(coordinateString, "coordinateString"))
.build();
}

@Nonnull
static ArtifactCoordinatesFactoryRequest build(@Nonnull Session session, @Nonnull ArtifactCoordinates coordinates) {
return ArtifactCoordinatesFactoryRequest.builder()
.session(nonNull(session, "session"))
.groupId(nonNull(coordinates, "coordinates").getGroupId())
.session(requireNonNull(session, "session"))
.groupId(requireNonNull(coordinates, "coordinates").getGroupId())
.artifactId(coordinates.getArtifactId())
.classifier(coordinates.getClassifier())
.version(coordinates.getVersionConstraint().asString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.apache.maven.api.annotations.Immutable;
import org.apache.maven.api.annotations.Nonnull;

import static org.apache.maven.api.services.BaseRequest.nonNull;
import static java.util.Objects.requireNonNull;

/**
* A request for deploying one or more artifacts to a remote repository.
Expand Down Expand Up @@ -60,9 +60,9 @@ static ArtifactDeployerRequest build(
@Nonnull RemoteRepository repository,
@Nonnull Collection<ProducedArtifact> artifacts) {
return builder()
.session(nonNull(session, "session cannot be null"))
.repository(nonNull(repository, "repository cannot be null"))
.artifacts(nonNull(artifacts, "artifacts cannot be null"))
.session(requireNonNull(session, "session cannot be null"))
.repository(requireNonNull(repository, "repository cannot be null"))
.artifacts(requireNonNull(artifacts, "artifacts cannot be null"))
.build();
}

Expand Down Expand Up @@ -114,8 +114,8 @@ private static class DefaultArtifactDeployerRequest extends BaseRequest<Session>
@Nonnull Collection<ProducedArtifact> artifacts,
int retryFailedDeploymentCount) {
super(session);
this.repository = nonNull(repository, "repository cannot be null");
this.artifacts = unmodifiable(nonNull(artifacts, "artifacts cannot be null"));
this.repository = requireNonNull(repository, "repository cannot be null");
this.artifacts = unmodifiable(requireNonNull(artifacts, "artifacts cannot be null"));
this.retryFailedDeploymentCount = retryFailedDeploymentCount;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.NotThreadSafe;

import static org.apache.maven.api.services.BaseRequest.nonNull;
import static java.util.Objects.requireNonNull;

/**
*
Expand Down Expand Up @@ -53,7 +53,7 @@ public interface ArtifactFactoryRequest {
static ArtifactFactoryRequest build(
Session session, String groupId, String artifactId, String version, String extension) {
return ArtifactFactoryRequest.builder()
.session(nonNull(session, "session cannot be null"))
.session(requireNonNull(session, "session cannot be null"))
.groupId(groupId)
.artifactId(artifactId)
.version(version)
Expand All @@ -70,7 +70,7 @@ static ArtifactFactoryRequest build(
String extension,
String type) {
return ArtifactFactoryRequest.builder()
.session(nonNull(session, "session cannot be null"))
.session(requireNonNull(session, "session cannot be null"))
.groupId(groupId)
.artifactId(artifactId)
.version(version)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.apache.maven.api.annotations.NotThreadSafe;
import org.apache.maven.api.annotations.Nullable;

import static org.apache.maven.api.services.BaseRequest.nonNull;
import static java.util.Objects.requireNonNull;

/**
* A request for installing one or more artifacts in the local repository.
Expand All @@ -54,8 +54,8 @@ static ArtifactInstallerRequestBuilder builder() {
@Nonnull
static ArtifactInstallerRequest build(Session session, Collection<ProducedArtifact> artifacts) {
return builder()
.session(nonNull(session, "session cannot be null"))
.artifacts(nonNull(artifacts, "artifacts cannot be null"))
.session(requireNonNull(session, "session cannot be null"))
.artifacts(requireNonNull(artifacts, "artifacts cannot be null"))
.build();
}

Expand Down Expand Up @@ -89,7 +89,7 @@ static class DefaultArtifactInstallerRequest extends BaseRequest<Session> implem

DefaultArtifactInstallerRequest(@Nonnull Session session, @Nonnull Collection<ProducedArtifact> artifacts) {
super(session);
this.artifacts = unmodifiable(nonNull(artifacts, "artifacts cannot be null"));
this.artifacts = unmodifiable(requireNonNull(artifacts, "artifacts cannot be null"));
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.apache.maven.api.annotations.NotThreadSafe;
import org.apache.maven.api.annotations.Nullable;

import static org.apache.maven.api.services.BaseRequest.nonNull;
import static java.util.Objects.requireNonNull;

/**
* A request for resolving an artifact.
Expand Down Expand Up @@ -58,8 +58,8 @@ static ArtifactResolverRequestBuilder builder() {
static ArtifactResolverRequest build(
@Nonnull Session session, @Nonnull Collection<? extends ArtifactCoordinates> coordinates) {
return builder()
.session(nonNull(session, "session cannot be null"))
.coordinates(nonNull(coordinates, "coordinates cannot be null"))
.session(requireNonNull(session, "session cannot be null"))
.coordinates(requireNonNull(coordinates, "coordinates cannot be null"))
.build();
}

Expand All @@ -69,8 +69,8 @@ static ArtifactResolverRequest build(
@Nonnull Collection<? extends ArtifactCoordinates> coordinates,
List<RemoteRepository> repositories) {
return builder()
.session(nonNull(session, "session cannot be null"))
.coordinates(nonNull(coordinates, "coordinates cannot be null"))
.session(requireNonNull(session, "session cannot be null"))
.coordinates(requireNonNull(coordinates, "coordinates cannot be null"))
.repositories(repositories)
.build();
}
Expand Down Expand Up @@ -119,7 +119,7 @@ private static class DefaultArtifactResolverRequest extends BaseRequest<Session>
@Nonnull Collection<? extends ArtifactCoordinates> coordinates,
@Nonnull List<RemoteRepository> repositories) {
super(session);
this.coordinates = unmodifiable(nonNull(coordinates, "coordinates cannot be null"));
this.coordinates = unmodifiable(requireNonNull(coordinates, "coordinates cannot be null"));
this.repositories = repositories;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;

import static java.util.Objects.requireNonNull;

/**
* Base class for requests.
*
Expand All @@ -37,21 +39,14 @@ abstract class BaseRequest<S extends ProtoSession> {
private final S session;

protected BaseRequest(@Nonnull S session) {
this.session = nonNull(session, "session cannot be null");
this.session = requireNonNull(session, "session cannot be null");
}

@Nonnull
public S getSession() {
return session;
}

public static <T> T nonNull(T obj, String message) {
if (obj == null) {
throw new IllegalArgumentException(message);
}
return obj;
}

protected static <T> Collection<T> unmodifiable(Collection<T> obj) {
return obj != null && !obj.isEmpty()
? Collections.unmodifiableCollection(new ArrayList<>(obj))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.NotThreadSafe;

import static org.apache.maven.api.services.BaseRequest.nonNull;
import static java.util.Objects.requireNonNull;

/**
*
Expand All @@ -58,7 +58,7 @@ static DependencyCoordinatesFactoryRequest build(
String extension,
String type) {
return DependencyCoordinatesFactoryRequest.builder()
.session(nonNull(session, "session cannot be null"))
.session(requireNonNull(session, "session cannot be null"))
.groupId(groupId)
.artifactId(artifactId)
.version(version)
Expand All @@ -72,8 +72,9 @@ static DependencyCoordinatesFactoryRequest build(
static DependencyCoordinatesFactoryRequest build(
@Nonnull Session session, @Nonnull ArtifactCoordinates coordinates) {
return builder()
.session(nonNull(session, "session cannot be null"))
.groupId(nonNull(coordinates, "coordinates cannot be null").getGroupId())
.session(requireNonNull(session, "session cannot be null"))
.groupId(requireNonNull(coordinates, "coordinates cannot be null")
.getGroupId())
.artifactId(coordinates.getArtifactId())
.version(coordinates.getVersionConstraint().asString())
.classifier(coordinates.getClassifier())
Expand All @@ -84,8 +85,8 @@ static DependencyCoordinatesFactoryRequest build(
@Nonnull
static DependencyCoordinatesFactoryRequest build(@Nonnull Session session, @Nonnull Dependency dependency) {
return builder()
.session(nonNull(session, "session cannot be null"))
.groupId(nonNull(dependency, "dependency").getGroupId())
.session(requireNonNull(session, "session cannot be null"))
.groupId(requireNonNull(dependency, "dependency").getGroupId())
.artifactId(dependency.getArtifactId())
.version(dependency.getVersion().asString())
.classifier(dependency.getClassifier())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import org.apache.maven.api.annotations.NotThreadSafe;
import org.apache.maven.api.annotations.Nullable;

import static java.util.Objects.requireNonNull;

/**
* A request to collect the transitive dependencies and to build a dependency graph from them. There are three ways to
* create a dependency graph. First, only the root dependency can be given. Second, a root dependency and direct
Expand Down Expand Up @@ -394,15 +396,15 @@ static class DefaultDependencyResolverRequest extends BaseRequest<Session>
@Nullable Predicate<PathType> pathTypeFilter,
@Nullable List<RemoteRepository> repositories) {
super(session);
this.requestType = nonNull(requestType, "requestType cannot be null");
this.requestType = requireNonNull(requestType, "requestType cannot be null");
this.project = project;
this.rootArtifact = rootArtifact;
this.root = root;
this.dependencies = unmodifiable(nonNull(dependencies, "dependencies cannot be null"));
this.dependencies = unmodifiable(requireNonNull(dependencies, "dependencies cannot be null"));
this.managedDependencies =
unmodifiable(nonNull(managedDependencies, "managedDependencies cannot be null"));
unmodifiable(requireNonNull(managedDependencies, "managedDependencies cannot be null"));
this.verbose = verbose;
this.pathScope = nonNull(pathScope, "pathScope cannot be null");
this.pathScope = requireNonNull(pathScope, "pathScope cannot be null");
this.pathTypeFilter = (pathTypeFilter != null) ? pathTypeFilter : (t) -> true;
this.repositories = repositories;
if (verbose && requestType != RequestType.COLLECT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.apache.maven.api.annotations.Nullable;
import org.apache.maven.api.model.Profile;

import static org.apache.maven.api.services.BaseRequest.nonNull;
import static java.util.Objects.requireNonNull;

/**
* Request used to build a {@link org.apache.maven.api.Project} using
Expand Down Expand Up @@ -143,23 +143,23 @@ enum RepositoryMerging {

@Nonnull
static ModelBuilderRequest build(@Nonnull ModelBuilderRequest request, @Nonnull ModelSource source) {
return builder(nonNull(request, "request cannot be null"))
.source(nonNull(source, "source cannot be null"))
return builder(requireNonNull(request, "request cannot be null"))
.source(requireNonNull(source, "source cannot be null"))
.build();
}

@Nonnull
static ModelBuilderRequest build(@Nonnull Session session, @Nonnull ModelSource source) {
return builder()
.session(nonNull(session, "session cannot be null"))
.source(nonNull(source, "source cannot be null"))
.session(requireNonNull(session, "session cannot be null"))
.source(requireNonNull(source, "source cannot be null"))
.build();
}

@Nonnull
static ModelBuilderRequest build(@Nonnull Session session, @Nonnull Path path) {
return builder()
.session(nonNull(session, "session cannot be null"))
.session(requireNonNull(session, "session cannot be null"))
.source(ModelSource.fromPath(path))
.build();
}
Expand Down Expand Up @@ -320,7 +320,7 @@ private static class DefaultModelBuilderRequest extends BaseRequest<Session> imp
List<RemoteRepository> repositories,
ModelTransformer lifecycleBindingsInjector) {
super(session);
this.requestType = nonNull(requestType, "requestType cannot be null");
this.requestType = requireNonNull(requestType, "requestType cannot be null");
this.locationTracking = locationTracking;
this.recursive = recursive;
this.source = source;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;

import static org.apache.maven.api.services.BaseRequest.nonNull;
import static java.util.Objects.requireNonNull;

/**
* A Source specific to load POMs. The {@link #resolve(ModelLocator, String)} method
Expand Down Expand Up @@ -54,6 +54,6 @@ static ModelSource fromPath(@Nonnull Path path) {

@Nonnull
static ModelSource fromPath(@Nonnull Path path, @Nullable String location) {
return new PathSource(nonNull(path, "path cannot be null"), location);
return new PathSource(requireNonNull(path, "path cannot be null"), location);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.apache.maven.api.annotations.NotThreadSafe;
import org.apache.maven.api.annotations.Nullable;

import static org.apache.maven.api.services.BaseRequest.nonNull;
import static java.util.Objects.requireNonNull;

/**
* Request used to build a {@link org.apache.maven.api.Project} using
Expand Down Expand Up @@ -65,16 +65,16 @@ public interface ProjectBuilderRequest {
@Nonnull
static ProjectBuilderRequest build(@Nonnull Session session, @Nonnull Source source) {
return builder()
.session(nonNull(session, "session cannot be null"))
.source(nonNull(source, "source cannot be null"))
.session(requireNonNull(session, "session cannot be null"))
.source(requireNonNull(source, "source cannot be null"))
.build();
}

@Nonnull
static ProjectBuilderRequest build(@Nonnull Session session, @Nonnull Path path) {
return builder()
.session(nonNull(session, "session cannot be null"))
.path(nonNull(path, "path cannot be null"))
.session(requireNonNull(session, "session cannot be null"))
.path(requireNonNull(path, "path cannot be null"))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.apache.maven.api.annotations.NotThreadSafe;
import org.apache.maven.api.annotations.Nullable;

import static org.apache.maven.api.services.BaseRequest.nonNull;
import static java.util.Objects.requireNonNull;

/**
* Collects settings that control the building of effective settings.
Expand Down Expand Up @@ -95,7 +95,7 @@ static SettingsBuilderRequest build(
@Nullable Source projectSettingsSource,
@Nullable Source userSettingsSource) {
return builder()
.session(nonNull(session, "session cannot be null"))
.session(requireNonNull(session, "session cannot be null"))
.installationSettingsSource(installationSettingsSource)
.projectSettingsSource(projectSettingsSource)
.userSettingsSource(userSettingsSource)
Expand All @@ -109,7 +109,7 @@ static SettingsBuilderRequest build(
@Nullable Path projectSettingsPath,
@Nullable Path userSettingsPath) {
return builder()
.session(nonNull(session, "session cannot be null"))
.session(requireNonNull(session, "session cannot be null"))
.installationSettingsSource(
installationSettingsPath != null && Files.exists(installationSettingsPath)
? Source.fromPath(installationSettingsPath)
Expand Down
Loading

0 comments on commit 03cca08

Please sign in to comment.