Skip to content

Commit

Permalink
removed Maven and Numeric
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejj0 committed Jan 22, 2025
1 parent a7e7a63 commit 38bab3d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ public PropertyVersionsBuilder[] getPropertyVersionsBuilders(
}

// Finally, remove any properties without associations
propertiesMap.values().removeIf(versions -> versions.getAssociations().length == 0);
propertiesMap.values().removeIf(versions -> !versions.isAssociated());

return propertiesMap.values().toArray(new PropertyVersionsBuilder[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
Expand Down Expand Up @@ -61,40 +60,27 @@ public class PropertyVersions extends AbstractVersionDetails {
*
* @since 1.0-beta-1
*/
private final SortedSet<ArtifactVersion> allVersions;
private final SortedSet<ArtifactVersion> resolvedVersions;

private final Log log;

PropertyVersions(
VersionsHelper helper, String profileId, String name, Log log, Set<ArtifactAssociation> associations)
throws VersionRetrievalException {
String profileId,
String name,
Log log,
Set<ArtifactAssociation> associations,
SortedSet<ArtifactVersion> resolvedVersions) {
this.profileId = profileId;
this.name = name;
this.log = log;
this.associations = new TreeSet<>(associations);
this.allVersions = resolveAssociatedVersions(helper, associations);
this.resolvedVersions = resolvedVersions;
}

public ArtifactAssociation[] getAssociations() {
return associations.toArray(new ArtifactAssociation[0]);
}

private SortedSet<ArtifactVersion> resolveAssociatedVersions(
VersionsHelper helper, Set<ArtifactAssociation> associations) throws VersionRetrievalException {
SortedSet<ArtifactVersion> result = new TreeSet<>();
for (ArtifactAssociation association : associations) {
ArtifactVersions artifactVersions =
helper.lookupArtifactVersions(association.getArtifact(), association.isUsePluginRepositories());
List<ArtifactVersion> associatedVersions = Arrays.asList(artifactVersions.getVersions(true));
if (result.isEmpty()) {
result.addAll(associatedVersions);
} else {
result.retainAll(associatedVersions);
}
}
return result;
}

/**
* Uses the supplied {@link Collection} of {@link Artifact} instances to see if an ArtifactVersion can be provided.
*
Expand Down Expand Up @@ -150,7 +136,7 @@ public ArtifactVersion[] getVersions(Collection<Artifact> artifacts) {
* @return The (possibly empty) array of versions.
*/
public ArtifactVersion[] getVersions(boolean includeSnapshots) {
return allVersions.stream()
return resolvedVersions.stream()
.filter(v -> includeSnapshots || !ArtifactUtils.isSnapshot(v.toString()))
.toArray(ArtifactVersion[]::new);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
* under the License.
*/

import java.util.Comparator;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import org.apache.maven.artifact.Artifact;
Expand Down Expand Up @@ -84,12 +86,9 @@ public boolean isAssociated() {
return !associations.isEmpty();
}

public ArtifactAssociation[] getAssociations() {
return associations.toArray(new ArtifactAssociation[0]);
}

public PropertyVersions build() throws VersionRetrievalException {
PropertyVersions instance = new PropertyVersions(helper, profileId, name, log, associations);
SortedSet<ArtifactVersion> resolvedVersions = resolveAssociatedVersions(helper, associations);
PropertyVersions instance = new PropertyVersions(profileId, name, log, associations, resolvedVersions);
instance.setCurrentVersion(currentVersion);
instance.setCurrentVersionRange(currentVersionRange);
return instance;
Expand All @@ -100,7 +99,6 @@ public String getName() {
}

public String getVersionRange() {
Comparator<ArtifactVersion> comparator = new PropertyVersionComparator();
if (lowerBounds.isEmpty() && upperBounds.isEmpty()) {
return null;
}
Expand All @@ -112,11 +110,12 @@ public String getVersionRange() {
lowerBound = candidate;
includeLower = entry.getValue();
} else {
final int result = comparator.compare(lowerBound, candidate);
if (result > 0) {
assert isAssociated();
final int comparisonResult = lowerBound.compareTo(candidate);
if (comparisonResult > 0) {
lowerBound = candidate;
includeLower = entry.getValue();
} else if (result == 0) {
} else if (comparisonResult == 0) {
includeLower = includeLower && entry.getValue();
}
}
Expand All @@ -129,11 +128,12 @@ public String getVersionRange() {
upperBound = candidate;
includeUpper = entry.getValue();
} else {
final int result = comparator.compare(upperBound, candidate);
if (result < 0) {
assert isAssociated();
final int comparisonResult = upperBound.compareTo(candidate);
if (comparisonResult < 0) {
upperBound = candidate;
includeUpper = entry.getValue();
} else if (result == 0) {
} else if (comparisonResult == 0) {
includeUpper = includeUpper && entry.getValue();
}
}
Expand Down Expand Up @@ -181,19 +181,6 @@ public PropertyVersionsBuilder withUpperBound(String upperBound, boolean include
return this;
}

private final class PropertyVersionComparator implements Comparator<ArtifactVersion> {
public int compare(ArtifactVersion v1, ArtifactVersion v2) {
return innerCompare(v1, v2);
}

private int innerCompare(ArtifactVersion v1, ArtifactVersion v2) {
if (!isAssociated()) {
throw new IllegalStateException("Cannot compare versions for a property with no associations");
}
return v1.compareTo(v2);
}
}

public PropertyVersionsBuilder withCurrentVersion(ArtifactVersion currentVersion) {
this.currentVersion = currentVersion;
return this;
Expand All @@ -203,4 +190,20 @@ public PropertyVersionsBuilder withCurrentVersionRange(VersionRange currentVersi
this.currentVersionRange = currentVersionRange;
return this;
}

private SortedSet<ArtifactVersion> resolveAssociatedVersions(
VersionsHelper helper, Set<ArtifactAssociation> associations) throws VersionRetrievalException {
SortedSet<ArtifactVersion> result = new TreeSet<>();
for (ArtifactAssociation association : associations) {
ArtifactVersions artifactVersions =
helper.lookupArtifactVersions(association.getArtifact(), association.isUsePluginRepositories());
List<ArtifactVersion> associatedVersions = Arrays.asList(artifactVersions.getVersions(true));
if (result.isEmpty()) {
result.addAll(associatedVersions);
} else {
result.retainAll(associatedVersions);
}
}
return result;
}
}

0 comments on commit 38bab3d

Please sign in to comment.