All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.jhipster.lite.module.domain.javadependency.JHipsterModuleJavaDependencies Maven / Gradle / Ivy

There is a newer version: 1.22.0
Show newest version
package tech.jhipster.lite.module.domain.javadependency;

import static tech.jhipster.lite.module.domain.javadependency.JavaDependencyScope.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;
import tech.jhipster.lite.module.domain.javabuild.ArtifactId;
import tech.jhipster.lite.module.domain.javabuild.GroupId;
import tech.jhipster.lite.module.domain.javabuild.VersionSlug;
import tech.jhipster.lite.module.domain.javabuild.command.JavaBuildCommand;
import tech.jhipster.lite.module.domain.javabuild.command.JavaBuildCommands;
import tech.jhipster.lite.module.domain.javabuild.command.RemoveDirectJavaDependency;
import tech.jhipster.lite.module.domain.javabuild.command.RemoveJavaDependencyManagement;
import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileId;
import tech.jhipster.lite.shared.error.domain.Assert;

public final class JHipsterModuleJavaDependencies {

  private final Collection dependenciesToRemove;
  private final Collection dependenciesManagement;
  private final Collection dependenciesManagementToRemove;
  private final Collection dependencies;

  private JHipsterModuleJavaDependencies(JHipsterModuleJavaDependenciesBuilder builder) {
    dependenciesToRemove = builder.dependenciesToRemove;
    dependenciesManagement = builder.dependenciesManagement;
    dependenciesManagementToRemove = builder.dependenciesManagementToRemove;
    dependencies = builder.dependencies;
  }

  public static  JHipsterModuleJavaDependenciesBuilder builder(M module) {
    return new JHipsterModuleJavaDependenciesBuilder<>(module);
  }

  public JavaBuildCommands buildChanges(JavaDependenciesVersions versions, ProjectJavaDependencies projectDependencies) {
    return buildChanges(versions, projectDependencies, Optional.empty());
  }

  public JavaBuildCommands buildChanges(
    JavaDependenciesVersions versions,
    ProjectJavaDependencies projectJavaDependencies,
    BuildProfileId buildProfile
  ) {
    Assert.notNull("buildProfile", buildProfile);
    return buildChanges(versions, projectJavaDependencies, Optional.of(buildProfile));
  }

  private JavaBuildCommands buildChanges(
    JavaDependenciesVersions versions,
    ProjectJavaDependencies projectDependencies,
    Optional buildProfile
  ) {
    Assert.notNull("versions", versions);
    Assert.notNull("projectDependencies", projectDependencies);

    return Stream.of(
      dependenciesToRemoveCommands(buildProfile),
      dependenciesManagementChanges(versions, projectDependencies, buildProfile),
      dependenciesManagementToRemoveCommands(buildProfile),
      dependenciesChanges(versions, projectDependencies, buildProfile)
    )
      .flatMap(Function.identity())
      .reduce(JavaBuildCommands.EMPTY, JavaBuildCommands::merge);
  }

  private Stream dependenciesToRemoveCommands(Optional buildProfile) {
    return Stream.of(new JavaBuildCommands(dependenciesToRemove.stream().map(toDependencyToRemove(buildProfile)).toList()));
  }

  private Function toDependencyToRemove(Optional buildProfile) {
    return dependency -> new RemoveDirectJavaDependency(dependency, buildProfile);
  }

  private Stream dependenciesManagementChanges(
    JavaDependenciesVersions currentVersions,
    ProjectJavaDependencies projectDependencies,
    Optional buildProfile
  ) {
    return dependenciesManagement.stream().map(dependency -> dependency.changeCommands(currentVersions, projectDependencies, buildProfile));
  }

  private Stream dependenciesManagementToRemoveCommands(Optional buildProfile) {
    return Stream.of(
      new JavaBuildCommands(dependenciesManagementToRemove.stream().map(toDependencyManagementToRemove(buildProfile)).toList())
    );
  }

  private Function toDependencyManagementToRemove(Optional buildProfile) {
    return dependency -> new RemoveJavaDependencyManagement(dependency, buildProfile);
  }

  private Stream dependenciesChanges(
    JavaDependenciesVersions currentVersions,
    ProjectJavaDependencies projectDependencies,
    Optional buildProfile
  ) {
    return dependencies.stream().map(dependency -> dependency.changeCommands(currentVersions, projectDependencies, buildProfile));
  }

  public static final class JHipsterModuleJavaDependenciesBuilder {

    private static final String DEPENDENCY = "dependency";

    private final T parentModuleBuilder;
    private final Collection dependenciesToRemove = new ArrayList<>();
    private final Collection dependencies = new ArrayList<>();
    private final Collection dependenciesManagement = new ArrayList<>();
    private final Collection dependenciesManagementToRemove = new ArrayList<>();

    private JHipsterModuleJavaDependenciesBuilder(T parentModuleBuilder) {
      Assert.notNull("module", parentModuleBuilder);

      this.parentModuleBuilder = parentModuleBuilder;
    }

    public JHipsterModuleJavaDependenciesBuilder removeDependency(DependencyId dependency) {
      Assert.notNull(DEPENDENCY, dependency);

      dependenciesToRemove.add(dependency);

      return this;
    }

    public JHipsterModuleJavaDependenciesBuilder addDependency(GroupId groupId, ArtifactId artifactId) {
      return addDependency(groupId, artifactId, null);
    }

    public JHipsterModuleJavaDependenciesBuilder addDependency(GroupId groupId, ArtifactId artifactId, VersionSlug versionSlug) {
      JavaDependency dependency = JavaDependency.builder().groupId(groupId).artifactId(artifactId).versionSlug(versionSlug).build();

      return addDependency(dependency);
    }

    public JHipsterModuleJavaDependenciesBuilder addTestDependency(GroupId groupId, ArtifactId artifactId, VersionSlug versionSlug) {
      JavaDependency dependency = JavaDependency.builder()
        .groupId(groupId)
        .artifactId(artifactId)
        .versionSlug(versionSlug)
        .scope(TEST)
        .build();

      return addDependency(dependency);
    }

    public JHipsterModuleJavaDependenciesBuilder addDependency(JavaDependency dependency) {
      Assert.notNull(DEPENDENCY, dependency);

      dependencies.add(new DirectJavaDependency(dependency));

      return this;
    }

    public JHipsterModuleJavaDependenciesBuilder addDependencyManagement(JavaDependency dependency) {
      Assert.notNull(DEPENDENCY, dependency);

      dependenciesManagement.add(new JavaDependencyManagement(dependency));

      return this;
    }

    public JHipsterModuleJavaDependenciesBuilder removeDependencyManagement(DependencyId dependency) {
      Assert.notNull(DEPENDENCY, dependency);

      dependenciesManagementToRemove.add(dependency);

      return this;
    }

    public T and() {
      return parentModuleBuilder;
    }

    public JHipsterModuleJavaDependencies build() {
      return new JHipsterModuleJavaDependencies(this);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy