@Override public int compare(String version1, String version2) { SuffixedVersion v1 = versionParser.parse(version1); SuffixedVersion v2 = versionParser.parse(version2); int r = v1.compareTo(v2); if (r == 0 || base == null) { return r; } else { return compareByDistance(v1, v2); } }
/** * Returns comparator that compares versions by distance to the base version. * @param base The base version. * @param versionParser Parser that will be used to parse the version. */ public VersionComparator(String base, VersionParser versionParser) { this.versionParser = versionParser; this.base = versionParser.parse(base); }
/** * Sets the suffix that distinguish product artifacts in the repository. * @param suffix Suffix of the product artifacts. */ public void setVersionSuffix(String suffix) { versionParser = new VersionParser(suffix); }
/** * Return information about first part of version that is different. */ public VersionDifference difference(String version1, String version2) { SuffixedVersion v1 = versionParser.parse(version1); SuffixedVersion v2 = versionParser.parse(version2); if (v1.getMajor() != v2.getMajor()) { return VersionDifference.MAJOR; } if (v1.getMinor() != v2.getMinor()) { return VersionDifference.MINOR; } if (v1.getMicro() != v2.getMicro()) { return VersionDifference.MICRO; } if (!v1.getQualifier().equals(v2.getQualifier())) { return VersionDifference.QUALIFIER; } if (!v1.getSuffix().equals(v2.getSuffix())) { return VersionDifference.SUFFIX; } if (!v1.getSuffixVersion().equals(v2.getSuffixVersion())) { return VersionDifference.RH_SUFFIX; } return VersionDifference.EQUAL; }
private Optional<String> findBiggestMatchingVersion(SuffixedVersion queryVersion, Collection<SuffixedVersion> versions) { String bestMatchVersion = null; int biggestBuildNumber = 0; String unsuffixedVesion = queryVersion.unsuffixedVesion(); List<SuffixedVersion> candidateVersions = versions.stream() .filter(SuffixedVersion::isSuffixed) .filter(v-> unsuffixedVesion.equals(v.unsuffixedVesion())) .collect(Collectors.toList()); boolean onlyDefaultSuffixPresent = candidateVersions.stream() .map(v -> v.getSuffix().get()) .allMatch(VersionAnalyzer::isDefaultSuffix); List<SuffixedVersion> versionsToSearch = candidateVersions.stream() .filter(v -> onlyDefaultSuffixPresent || !isDefaultSuffix(v.getSuffix().get())) .collect(Collectors.toList()); for (SuffixedVersion ver : versionsToSearch) { int foundBuildNumber = ver.getSuffixVersion().get(); if (bestMatchVersion == null || foundBuildNumber > biggestBuildNumber) { bestMatchVersion = ver.getOriginalVersion(); biggestBuildNumber = foundBuildNumber; } else if (foundBuildNumber == biggestBuildNumber) { bestMatchVersion = getMoreSpecificVersion(bestMatchVersion, ver.getOriginalVersion()); } } return Optional.ofNullable(bestMatchVersion); }
private void populateAdvancedArtifactReportFields(AdvancedArtifactReport advancedReport, ArtifactReport report, Set<GAV> modulesAnalyzed, File repoFolder) { VersionParser parser = new VersionParser(VersionParser.DEFAULT_SUFFIX); } else { if (parser.parse(dep.getVersion()).isSuffixed()) continue; } else { if (!dep.getAvailableVersions().isEmpty()) { Set<String> versions = new TreeSet<>(new VersionComparator( gav.getVersion(), parser)); versions.addAll(dep.getAvailableVersions());
@Override public Optional<BlackArtifact> getArtifact(String groupId, String artifactId, String version) { SuffixedVersion parsedVersion = versionParser.parse(version); Optional<BlackArtifact> artifact = blackArtifactDAO.findArtifact(groupId, artifactId, parsedVersion.unsuffixedVesion()); if (parsedVersion.isSuffixed() && !artifact.isPresent()) { artifact = blackArtifactDAO.findArtifact(groupId, artifactId, version); } return artifact; }
public VersionAnalysisResult analyseVersions(String querry, Collection<String> versions){ VersionComparator comparator = new VersionComparator(querry, versionParser); List<String> sortedVersions = new ArrayList<>(); List<SuffixedVersion> parsedVersions = versions.stream() .sorted(comparator) .distinct() .peek(v -> sortedVersions.add(v)) .map(versionParser::parse) .collect(Collectors.toList()); SuffixedVersion version = versionParser.parse(querry); Optional<String> bmv = findBiggestMatchingVersion(version, parsedVersions); return new VersionAnalysisResult(bmv, sortedVersions); }
private WhiteArtifact createArtifact(String groupId, String artifactId, String version) { SuffixedVersion parsedVersion = versionParser.parse(version); final boolean is3rdParty = !parsedVersion.isSuffixed(); final String osgiVersion = VersionParser.getOSGiVersion(version); GA ga = gaDAO.findOrCreate(groupId, artifactId); WhiteArtifact a = new WhiteArtifact(ga, version, currentUser(), osgiVersion, is3rdParty); whiteArtifactDAO.create(a); return a; }
private Stream<String> getVersionsStream(GA ga) { if (!ga.isValid()) { log.warn("Received nonvalid GA: " + ga); return Stream.empty(); } try { return aproxConnector.getVersionsOfGA(ga).stream() .filter(v -> versionParser.parse(v).isSuffixed()) .distinct(); } catch (CommunicationException ex) { throw new ProductException(ex); } }
private int compareByDistance(SuffixedVersion v1, SuffixedVersion v2) { if (base.equals(v1)) { return -1; if (base.equals(v2)) { return 1; if (v1.getMajor() != v2.getMajor()) { if (v1.getMajor() == base.getMajor()) { return -1; if (v2.getMajor() == base.getMajor()) { return 1; if (v1.getMajor() == v2.getMajor() && v1.getMinor() == v2.getMinor() && v1.getMicro() == v2.getMicro()) { candidate *= -1;
public SuffixedVersion parse(String version) { SuffixedVersion suffixedVersion = parseVersion(versionPattern.matcher(version), version, suffix); if (!suffixedVersion.isSuffixed()) { suffixedVersion = parseVersion(defaultPattern.matcher(version), version, DEFAULT_SUFFIX); } return suffixedVersion; }
private SuffixedVersion parseVersion(Matcher versionMatcher, String version, String parseSuffix) throws NumberFormatException, IllegalArgumentException { if (!versionMatcher.matches()) { throw new IllegalArgumentException("Version " + version + "is unparsable"); } String majorString = versionMatcher.group("major"); String minorString = versionMatcher.group("minor"); String microString = versionMatcher.group("micro"); String qualifierString = versionMatcher.group("qualifier"); String suffixVersionString = versionMatcher.group("suffixversion"); int major = parseNumberString(majorString); int minor = parseNumberString(minorString); int micro = parseNumberString(microString); String qualifier = qualifierString == null ? "" : qualifierString; if (suffixVersionString == null) { return new SuffixedVersion(major, minor, micro, qualifier, version); } else { int suffixVersion = Integer.parseInt(suffixVersionString); return new SuffixedVersion(major, minor, micro, qualifier, parseSuffix, suffixVersion, version); } }
private BuiltReportModule toBuiltReportModule(GAV gav, VersionAnalysisResult vlr) { BuiltReportModule report = new BuiltReportModule(gav); report.setAvailableVersions(vlr.getAvailableVersions()); vlr.getBestMatchVersion().ifPresent(bmv -> report.setBuiltVersion(bmv)); return report; }
private static ProductArtifact toProductArtifact(Product p, Artifact a, String origVersion, VersionComparator comparator) { ProductArtifact ret = new ProductArtifact(); ret.setArtifact(a.getGav()); ret.setProductName(p.getName()); ret.setProductVersion(p.getVersion()); ret.setSupportStatus(p.getStatus()); ret.setDifferenceType(comparator.difference(origVersion, a.getGav().getVersion()) .toString()); return ret; }
private static Set<ProductArtifacts> getBuiltDifferent(Set<ProductArtifacts> a, VersionAnalysisResult v) { Optional<String> bmv = v.getBestMatchVersion(); if (bmv.isPresent()) { return Collections.emptySet(); } else { return a; } }
@Override public String toString() { return normalizedVesion(); }
private Stream<String> getVersionsStream(GA ga, String repository) { if (!ga.isValid()) { log.warn("Received nonvalid GA: " + ga); return Stream.empty(); } try { return aproxConnector.getVersionsOfGA(ga, repository).stream() .filter(v -> versionParser.parse(v).isSuffixed()) .distinct(); } catch (CommunicationException ex) { throw new ProductException(ex); } }
@Override public SortedSet<BlackArtifact> getArtifacts(String groupId, String artifactId) { Comparator<BlackArtifact> baComparator = Comparator .comparing((BlackArtifact a) -> versionParser.parse(a.getVersion())); SortedSet<BlackArtifact> ret = new TreeSet<>(baComparator); ret.addAll(blackArtifactDAO.findArtifacts(groupId, artifactId)); return ret; } }
private static Set<ProductArtifacts> getBuilt(Set<ProductArtifacts> a, VersionAnalysisResult v) { return v.getBestMatchVersion().map(b -> AggregatedProductProvider .filterArtifacts(a, x -> b.equals(x.getGav().getVersion()))) .orElse(Collections.emptySet()); }