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

tech.jhipster.lite.module.domain.npm.NpmPackagesVersions Maven / Gradle / Ivy

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

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import tech.jhipster.lite.shared.error.domain.Assert;

public final class NpmPackagesVersions {

  public static final NpmPackagesVersions EMPTY = builder().build();

  private final Map versions;

  private NpmPackagesVersions(NpmPackagesVersionsBuilder builder) {
    versions = Collections.unmodifiableMap(builder.versions);
  }

  private NpmPackagesVersions(Map versions) {
    this.versions = Collections.unmodifiableMap(versions);
  }

  public static NpmPackagesVersionsBuilder builder() {
    return new NpmPackagesVersionsBuilder();
  }

  public NpmPackageVersion get(NpmPackageName packageName, NpmVersionSource source) {
    Assert.notNull("packageName", packageName);
    Assert.notNull("source", source);

    return Optional.ofNullable(versions.get(source))
      .orElseThrow(() -> new UnknownNpmPackageException(packageName, source))
      .get(packageName);
  }

  public NpmPackagesVersions merge(NpmPackagesVersions other) {
    Assert.notNull("other", other);

    Map mergedVersions = new HashMap<>(other.versions);
    versions.forEach((source, packages) -> mergedVersions.computeIfAbsent(source, NpmSourceVersions::new).add(packages));

    return new NpmPackagesVersions(mergedVersions);
  }

  public static class NpmPackagesVersionsBuilder {

    private final Map versions = new ConcurrentHashMap<>();

    public NpmPackagesVersionsBuilder put(NpmVersionSource source, Collection packages) {
      versions.computeIfAbsent(source, key -> new NpmSourceVersions(source)).add(packages);

      return this;
    }

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

  private static final class NpmSourceVersions {

    private final NpmVersionSource source;
    private final Map versions = new ConcurrentHashMap<>();

    private NpmSourceVersions(NpmVersionSource source) {
      Assert.notNull("source", source);

      this.source = source;
    }

    private void add(NpmSourceVersions packages) {
      versions.putAll(packages.versions);
    }

    private void add(Collection packages) {
      Assert.field("packages", packages).notNull().noNullElement();

      versions.putAll(packages.stream().collect(Collectors.toMap(NpmPackage::name, NpmPackage::version)));
    }

    private NpmPackageVersion get(NpmPackageName packageName) {
      return Optional.ofNullable(versions.get(packageName)).orElseThrow(() -> new UnknownNpmPackageException(packageName, source));
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy