Skip to content

Commit

Permalink
Use Objects.requireNotNull
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo committed Jan 3, 2025
1 parent 49b9d6d commit 2c3f20f
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 90 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 java.util.Objects;

/**
* 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( Objects.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( Objects.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( Objects.requireNonNull( session, "session" ) )
.coordinateString( Objects.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( Objects.requireNonNull( session, "session" ) )
.groupId( Objects.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 @@ -19,6 +19,7 @@
package org.apache.maven.api.services;

import java.util.Collection;
import java.util.Objects;

import org.apache.maven.api.ProducedArtifact;
import org.apache.maven.api.RemoteRepository;
Expand All @@ -27,8 +28,6 @@
import org.apache.maven.api.annotations.Immutable;
import org.apache.maven.api.annotations.Nonnull;

import static org.apache.maven.api.services.BaseRequest.nonNull;

/**
* A request for deploying one or more artifacts to a remote repository.
*
Expand Down Expand Up @@ -60,9 +59,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( Objects.requireNonNull( session, "session cannot be null" ) )
.repository( Objects.requireNonNull( repository, "repository cannot be null" ) )
.artifacts( Objects.requireNonNull( artifacts, "artifacts cannot be null" ) )
.build();
}

Expand Down Expand Up @@ -114,8 +113,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 = Objects.requireNonNull( repository, "repository cannot be null" );
this.artifacts = unmodifiable( Objects.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 java.util.Objects;

/**
*
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( Objects.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( Objects.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 @@ -20,6 +20,7 @@

import java.util.Collection;
import java.util.Collections;
import java.util.Objects;

import org.apache.maven.api.ProducedArtifact;
import org.apache.maven.api.Session;
Expand All @@ -29,8 +30,6 @@
import org.apache.maven.api.annotations.NotThreadSafe;
import org.apache.maven.api.annotations.Nullable;

import static org.apache.maven.api.services.BaseRequest.nonNull;

/**
* A request for installing one or more artifacts in the local repository.
*
Expand All @@ -54,8 +53,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( Objects.requireNonNull( session, "session cannot be null" ) )
.artifacts( Objects.requireNonNull( artifacts, "artifacts cannot be null" ) )
.build();
}

Expand Down Expand Up @@ -89,7 +88,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( Objects.requireNonNull( artifacts, "artifacts cannot be null" ) );
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Collection;
import java.util.List;
import java.util.Objects;

import org.apache.maven.api.ArtifactCoordinates;
import org.apache.maven.api.RemoteRepository;
Expand All @@ -30,8 +31,6 @@
import org.apache.maven.api.annotations.NotThreadSafe;
import org.apache.maven.api.annotations.Nullable;

import static org.apache.maven.api.services.BaseRequest.nonNull;

/**
* A request for resolving an artifact.
*
Expand All @@ -58,8 +57,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( Objects.requireNonNull( session, "session cannot be null" ) )
.coordinates( Objects.requireNonNull( coordinates, "coordinates cannot be null" ) )
.build();
}

Expand All @@ -69,8 +68,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( Objects.requireNonNull( session, "session cannot be null" ) )
.coordinates( Objects.requireNonNull( coordinates, "coordinates cannot be null" ) )
.repositories(repositories)
.build();
}
Expand Down Expand Up @@ -119,7 +118,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( Objects.requireNonNull( coordinates, "coordinates cannot be null" ) );
this.repositories = repositories;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;

import org.apache.maven.api.ProtoSession;
import org.apache.maven.api.annotations.Experimental;
Expand All @@ -37,25 +38,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 = Objects.requireNonNull( session, "session cannot be null" );
}

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

/**
* Use Objects.nonNull instead
*/
@Deprecated
public static <T> T nonNull(T obj, String message) {
if (obj == null) {
throw new NullPointerException(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 @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;

import org.apache.maven.api.ArtifactCoordinates;
import org.apache.maven.api.Dependency;
Expand All @@ -31,8 +32,6 @@
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.NotThreadSafe;

import static org.apache.maven.api.services.BaseRequest.nonNull;

/**
*
* @since 4.0.0
Expand All @@ -58,7 +57,7 @@ static DependencyCoordinatesFactoryRequest build(
String extension,
String type) {
return DependencyCoordinatesFactoryRequest.builder()
.session(nonNull(session, "session cannot be null"))
.session( Objects.requireNonNull( session, "session cannot be null" ) )
.groupId(groupId)
.artifactId(artifactId)
.version(version)
Expand All @@ -72,8 +71,8 @@ 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( Objects.requireNonNull( session, "session cannot be null" ) )
.groupId( Objects.requireNonNull( coordinates, "coordinates cannot be null" ).getGroupId())
.artifactId(coordinates.getArtifactId())
.version(coordinates.getVersionConstraint().asString())
.classifier(coordinates.getClassifier())
Expand All @@ -84,8 +83,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( Objects.requireNonNull( session, "session cannot be null" ) )
.groupId( Objects.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 @@ -22,6 +22,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;

Expand Down Expand Up @@ -394,15 +395,17 @@ 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 = Objects.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(
Objects.requireNonNull( dependencies, "dependencies cannot be null" ) );
this.managedDependencies =
unmodifiable(nonNull(managedDependencies, "managedDependencies cannot be null"));
unmodifiable(
Objects.requireNonNull( managedDependencies, "managedDependencies cannot be null" ) );
this.verbose = verbose;
this.pathScope = nonNull(pathScope, "pathScope cannot be null");
this.pathScope = Objects.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 @@ -22,6 +22,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.maven.api.RemoteRepository;
import org.apache.maven.api.Session;
Expand All @@ -32,8 +33,6 @@
import org.apache.maven.api.annotations.Nullable;
import org.apache.maven.api.model.Profile;

import static org.apache.maven.api.services.BaseRequest.nonNull;

/**
* Request used to build a {@link org.apache.maven.api.Project} using
* the {@link ProjectBuilder} service.
Expand Down Expand Up @@ -143,23 +142,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( Objects.requireNonNull( request, "request cannot be null" ) )
.source( Objects.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( Objects.requireNonNull( session, "session cannot be null" ) )
.source( Objects.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( Objects.requireNonNull( session, "session cannot be null" ) )
.source(ModelSource.fromPath(path))
.build();
}
Expand Down Expand Up @@ -320,7 +319,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 = Objects.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 @@ -19,12 +19,11 @@
package org.apache.maven.api.services;

import java.nio.file.Path;
import java.util.Objects;

import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;

import static org.apache.maven.api.services.BaseRequest.nonNull;

/**
* A Source specific to load POMs. The {@link #resolve(ModelLocator, String)} method
* will be used to find POMs for subprojects.
Expand Down Expand Up @@ -54,6 +53,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( Objects.requireNonNull( path, "path cannot be null" ), location);
}
}
Loading

0 comments on commit 2c3f20f

Please sign in to comment.