Skip to content

Commit 95e3695

Browse files
authored
A thin adapter over Resolver (#1301)
1 parent 4edadbc commit 95e3695

26 files changed

+894
-217
lines changed

versions-common/src/main/java/org/codehaus/mojo/versions/api/ArtifactVersions.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.Objects;
2626
import java.util.SortedSet;
2727
import java.util.TreeSet;
28+
import java.util.function.Predicate;
29+
import java.util.stream.Collectors;
2830

2931
import org.apache.commons.lang3.builder.EqualsBuilder;
3032
import org.apache.maven.artifact.Artifact;
@@ -57,7 +59,8 @@ public class ArtifactVersions extends AbstractVersionDetails implements Comparab
5759
private final SortedSet<ArtifactVersion> versions;
5860

5961
/**
60-
* Creates a new {@link ArtifactVersions} instance.
62+
* Creates a new {@link ArtifactVersions} instance given the artifact and a list of {@link ArtifactVersion}
63+
* objects.
6164
*
6265
* @param artifact The artifact.
6366
* @param versions The versions.
@@ -71,6 +74,26 @@ public ArtifactVersions(Artifact artifact, List<ArtifactVersion> versions) {
7174
setCurrentVersionRange(artifact.getVersionRange());
7275
}
7376

77+
/**
78+
* <p>Creates a new {@link ArtifactVersions} instance from the current instance using a filter predicate,
79+
* matching version strings allowing to select versions which can remain in the resulting list of versions.</p>
80+
* <p>The filtering operation uses a shallow copy of the version elements. Also, the underlying artifact
81+
* refers to the same {@link Artifact} object as the original.</p>
82+
* @param versionFilter a {@link Predicate<String>}, saying which artifact versions of the original
83+
* should be present in the newly created object
84+
* @return a new instance with versions filtered by the predicate
85+
* @since 2.20.0
86+
*/
87+
public ArtifactVersions filter(Predicate<String> versionFilter) {
88+
ArtifactVersions result = new ArtifactVersions(
89+
artifact,
90+
versions.stream().filter(v -> versionFilter.test(v.toString())).collect(Collectors.toList()));
91+
if (!versionFilter.test(result.artifact.getVersion())) {
92+
result.setCurrentVersion((ArtifactVersion) null);
93+
}
94+
return result;
95+
}
96+
7497
/**
7598
* Creates a new {@link ArtifactVersions} instance as shallow copy of the other
7699
*

versions-common/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Map;
3030
import java.util.Optional;
3131
import java.util.Set;
32+
import java.util.SortedMap;
3233
import java.util.TreeMap;
3334
import java.util.TreeSet;
3435
import java.util.concurrent.ExecutionException;
@@ -249,15 +250,42 @@ public void resolveArtifact(Artifact artifact, boolean usePluginRepositories) th
249250
}
250251
}
251252

252-
public Map<Dependency, ArtifactVersions> lookupDependenciesUpdates(
253+
@Override
254+
public ArtifactVersions resolveArtifactVersions(
255+
Artifact artifact, boolean usePluginRepositories, boolean useProjectRepositories)
256+
throws VersionRetrievalException {
257+
return lookupArtifactVersions(artifact, null, usePluginRepositories, !usePluginRepositories);
258+
}
259+
260+
@Override
261+
public SortedMap<Dependency, ArtifactVersions> resolveDependencyVersions(
262+
Collection<Dependency> dependencies, boolean usePluginRepositories, boolean useProjectRepositories)
263+
throws VersionRetrievalException {
264+
return lookupDependenciesUpdates(dependencies.stream(), usePluginRepositories, useProjectRepositories, true);
265+
}
266+
267+
@Override
268+
public PluginUpdatesDetails resolvePluginVersions(Plugin plugin) throws VersionRetrievalException {
269+
return lookupPluginUpdates(plugin, true);
270+
}
271+
272+
@Override
273+
public SortedMap<Plugin, PluginUpdatesDetails> resolvePluginVersions(Collection<Plugin> plugins)
274+
throws VersionRetrievalException {
275+
return lookupPluginsUpdates(plugins.stream(), true);
276+
}
277+
278+
@Override
279+
public SortedMap<Dependency, ArtifactVersions> lookupDependenciesUpdates(
253280
Stream<Dependency> dependencies,
254281
boolean usePluginRepositories,
255282
boolean useProjectRepositories,
256283
boolean allowSnapshots)
257284
throws VersionRetrievalException {
285+
@SuppressWarnings("resource")
258286
ExecutorService executor = Executors.newFixedThreadPool(LOOKUP_PARALLEL_THREADS);
259287
try {
260-
Map<Dependency, ArtifactVersions> dependencyUpdates = new TreeMap<>(DependencyComparator.INSTANCE);
288+
SortedMap<Dependency, ArtifactVersions> dependencyUpdates = new TreeMap<>(DependencyComparator.INSTANCE);
261289
List<Future<? extends Pair<Dependency, ArtifactVersions>>> futures = dependencies
262290
.map(dependency -> executor.submit(() -> new ImmutablePair<>(
263291
dependency,
@@ -303,11 +331,12 @@ public ArtifactVersions lookupDependencyUpdates(
303331
}
304332

305333
@Override
306-
public Map<Plugin, PluginUpdatesDetails> lookupPluginsUpdates(Stream<Plugin> plugins, boolean allowSnapshots)
334+
public SortedMap<Plugin, PluginUpdatesDetails> lookupPluginsUpdates(Stream<Plugin> plugins, boolean allowSnapshots)
307335
throws VersionRetrievalException {
336+
@SuppressWarnings("resource")
308337
ExecutorService executor = Executors.newFixedThreadPool(LOOKUP_PARALLEL_THREADS);
309338
try {
310-
Map<Plugin, PluginUpdatesDetails> pluginUpdates = new TreeMap<>(PluginComparator.INSTANCE);
339+
SortedMap<Plugin, PluginUpdatesDetails> pluginUpdates = new TreeMap<>(PluginComparator.INSTANCE);
311340
List<Future<? extends Pair<Plugin, PluginUpdatesDetails>>> futures = plugins.map(
312341
p -> executor.submit(() -> new ImmutablePair<>(p, lookupPluginUpdates(p, allowSnapshots))))
313342
.collect(Collectors.toList());
@@ -354,7 +383,7 @@ public Map<Property, PropertyVersions> getVersionPropertiesMap(VersionProperties
354383
Map<String, Property> properties = new HashMap<>();
355384
// Populate properties map from request
356385
if (request.getPropertyDefinitions() != null) {
357-
Arrays.stream(request.getPropertyDefinitions()).forEach(p -> properties.put(p.getName(), p));
386+
request.getPropertyDefinitions().forEach(p -> properties.put(p.getName(), p));
358387
}
359388

360389
Map<String, PropertyVersionsBuilder> builders = new HashMap<>();

versions-common/src/main/java/org/codehaus/mojo/versions/api/IgnoreVersionHelper.java

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616
*/
1717

1818
import java.util.Arrays;
19-
import java.util.Collections;
20-
import java.util.HashMap;
2119
import java.util.List;
22-
import java.util.Map;
20+
import java.util.Optional;
2321
import java.util.function.BiFunction;
2422
import java.util.regex.Pattern;
23+
import java.util.stream.Collectors;
2524

2625
import org.apache.maven.artifact.versioning.ArtifactVersion;
2726
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
@@ -44,16 +43,40 @@ static class IgnoreVersionException extends RuntimeException {
4443
/**
4544
* Provides an instance of a list of valid types of {@link IgnoreVersion}
4645
*/
47-
public static final List<String> VALID_TYPES = Collections.unmodifiableList(
48-
Arrays.asList(IgnoreVersion.TYPE_EXACT, IgnoreVersion.TYPE_REGEX, IgnoreVersion.TYPE_RANGE));
46+
public static final List<String> VALID_TYPES = Arrays.stream(IgnoreVersionType.values())
47+
.map(IgnoreVersionType::getType)
48+
.collect(Collectors.toList());
4949

50-
private static final Map<String, BiFunction<Version, IgnoreVersion, Boolean>> VERSION_MATCHERS;
50+
public enum IgnoreVersionType {
51+
TYPE_EXACT(IgnoreVersion.TYPE_EXACT, IgnoreVersionHelper::isVersionIgnoredExact),
52+
TYPE_REGEX(IgnoreVersion.TYPE_REGEX, IgnoreVersionHelper::isVersionIgnoredRegex),
53+
TYPE_RANGE(IgnoreVersion.TYPE_RANGE, IgnoreVersionHelper::isVersionIgnoredRange);
5154

52-
static {
53-
VERSION_MATCHERS = new HashMap<>();
54-
VERSION_MATCHERS.put(IgnoreVersion.TYPE_EXACT, IgnoreVersionHelper::isVersionIgnoredExact);
55-
VERSION_MATCHERS.put(IgnoreVersion.TYPE_REGEX, IgnoreVersionHelper::isVersionIgnoredRegex);
56-
VERSION_MATCHERS.put(IgnoreVersion.TYPE_RANGE, IgnoreVersionHelper::isVersionIgnoredRange);
55+
private final String type;
56+
57+
private final BiFunction<String, IgnoreVersion, Boolean> predicate;
58+
59+
IgnoreVersionType(String type, BiFunction<String, IgnoreVersion, Boolean> predicate) {
60+
this.type = type;
61+
this.predicate = predicate;
62+
}
63+
64+
public String getType() {
65+
return type;
66+
}
67+
68+
public BiFunction<String, IgnoreVersion, Boolean> getPredicate() {
69+
return predicate;
70+
}
71+
72+
public static Optional<IgnoreVersionType> forType(String type) {
73+
for (IgnoreVersionType v : IgnoreVersionType.values()) {
74+
if (v.getType().equals(type)) {
75+
return Optional.of(v);
76+
}
77+
}
78+
return Optional.empty();
79+
}
5780
}
5881

5982
private IgnoreVersionHelper() {}
@@ -65,33 +88,44 @@ private IgnoreVersionHelper() {}
6588
* @return {@code true} if type is valid
6689
*/
6790
public static boolean isValidType(IgnoreVersion ignoreVersion) {
68-
return VALID_TYPES.contains(ignoreVersion.getType());
91+
return IgnoreVersionType.forType(ignoreVersion.getType()).isPresent();
92+
}
93+
94+
/**
95+
* Check if the provided version is ignored by the provided {@link IgnoreVersion} instance
96+
*
97+
* @param v version string to be checked
98+
* @param ignoreVersion {@link IgnoreVersion} instance providing the filter
99+
* @return {@code true} if the provided version is ignored
100+
*/
101+
public static boolean isVersionIgnored(String v, IgnoreVersion ignoreVersion) {
102+
return IgnoreVersionType.forType(ignoreVersion.getType())
103+
.map(t -> t.getPredicate().apply(v, ignoreVersion))
104+
.orElseThrow(() -> new IllegalArgumentException("Invalid version type: " + ignoreVersion.getType()));
69105
}
70106

71107
/**
72108
* Check if the provided version is ignored by the provided {@link IgnoreVersion} instance
73109
*
74-
* @param version version to be checked
110+
* @param v version string to be checked
75111
* @param ignoreVersion {@link IgnoreVersion} instance providing the filter
76112
* @return {@code true} if the provided version is ignored
77113
*/
78-
public static boolean isVersionIgnored(Version version, IgnoreVersion ignoreVersion) {
79-
return VERSION_MATCHERS.get(ignoreVersion.getType()).apply(version, ignoreVersion);
114+
public static boolean isVersionIgnored(Version v, IgnoreVersion ignoreVersion) {
115+
return isVersionIgnored(v.toString(), ignoreVersion);
80116
}
81117

82-
private static boolean isVersionIgnoredExact(Version version, IgnoreVersion ignoreVersion) {
83-
return ignoreVersion.getVersion().equals(version.toString());
118+
private static boolean isVersionIgnoredExact(String v, IgnoreVersion ignoreVersion) {
119+
return ignoreVersion.getVersion().equals(v);
84120
}
85121

86-
private static boolean isVersionIgnoredRegex(Version version, IgnoreVersion ignoreVersion) {
87-
return Pattern.compile(ignoreVersion.getVersion())
88-
.matcher(version.toString())
89-
.matches();
122+
private static boolean isVersionIgnoredRegex(String v, IgnoreVersion ignoreVersion) {
123+
return Pattern.compile(ignoreVersion.getVersion()).matcher(v).matches();
90124
}
91125

92-
private static boolean isVersionIgnoredRange(Version version, IgnoreVersion ignoreVersion) {
126+
private static boolean isVersionIgnoredRange(String v, IgnoreVersion ignoreVersion) {
93127
try {
94-
ArtifactVersion aVersion = ArtifactVersionService.getArtifactVersion(version.toString());
128+
ArtifactVersion aVersion = ArtifactVersionService.getArtifactVersion(v);
95129
VersionRange versionRange = VersionRange.createFromVersionSpec(ignoreVersion.getVersion());
96130
if (versionRange.hasRestrictions()) {
97131
return versionRange.containsVersion(aVersion);

versions-common/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ static Map<MavenProject, Model> getRawModelWithParents(MavenProject project) thr
803803
/**
804804
* Examines the project to find any properties which are associated with versions of artifacts in the project.
805805
*
806-
* @param helper {@link VersionsHelper} instance
806+
* @param resolverAdapter {@link ResolverAdapter} instance
807807
* @param log {@link Log} instance
808808
* @param project The project to examine.
809809
* @param includeParent whether parent POMs should be included
@@ -813,7 +813,7 @@ static Map<MavenProject, Model> getRawModelWithParents(MavenProject project) thr
813813
* @since 1.0-alpha-3
814814
*/
815815
public PropertyVersionsBuilder[] getPropertyVersionsBuilders(
816-
VersionsHelper helper, Log log, MavenProject project, boolean includeParent)
816+
ResolverAdapter resolverAdapter, Log log, MavenProject project, boolean includeParent)
817817
throws ExpressionEvaluationException, IOException {
818818

819819
Map<MavenProject, Model> reactorModels = includeParent
@@ -825,8 +825,8 @@ public PropertyVersionsBuilder[] getPropertyVersionsBuilders(
825825

826826
Map<String, PropertyVersionsBuilder> propertiesMap = new TreeMap<>();
827827
reactorModels.values().forEach(model -> {
828-
processProfiles(helper, log, propertiesMap, model, activeProfileIds);
829-
putPropertiesIfAbsent(helper, log, propertiesMap, null, model.getProperties());
828+
processProfiles(resolverAdapter, log, propertiesMap, model, activeProfileIds);
829+
putPropertiesIfAbsent(resolverAdapter, log, propertiesMap, null, model.getProperties());
830830
});
831831

832832
// Process the project and its parent hierarchy
@@ -847,7 +847,7 @@ public PropertyVersionsBuilder[] getPropertyVersionsBuilders(
847847
}
848848

849849
private void processProfiles(
850-
VersionsHelper helper,
850+
ResolverAdapter resolverAdapter,
851851
Log log,
852852
Map<String, PropertyVersionsBuilder> propertiesMap,
853853
Model model,
@@ -857,7 +857,8 @@ private void processProfiles(
857857
.filter(profile -> activeProfileIds.contains(profile.getId()))
858858
.forEach(profile -> {
859859
try {
860-
putPropertiesIfAbsent(helper, log, propertiesMap, profile.getId(), profile.getProperties());
860+
putPropertiesIfAbsent(
861+
resolverAdapter, log, propertiesMap, profile.getId(), profile.getProperties());
861862
processDependencies(
862863
propertiesMap, profile.getDependencyManagement(), profile.getDependencies());
863864
processBuild(propertiesMap, profile.getBuild());
@@ -1076,15 +1077,15 @@ private static void addBounds(PropertyVersionsBuilder builder, String rawVersion
10761077
}
10771078

10781079
private void putPropertiesIfAbsent(
1079-
VersionsHelper helper,
1080+
ResolverAdapter resolverAdapter,
10801081
Log log,
10811082
Map<String, PropertyVersionsBuilder> result,
10821083
String profileId,
10831084
Properties properties) {
10841085
ofNullable(properties)
10851086
.map(Properties::stringPropertyNames)
10861087
.ifPresent(propertyNames -> propertyNames.forEach(propertyName -> result.putIfAbsent(
1087-
propertyName, new PropertyVersionsBuilder(helper, profileId, propertyName, log))));
1088+
propertyName, new PropertyVersionsBuilder(resolverAdapter, profileId, propertyName, log))));
10881089
}
10891090

10901091
/**

0 commit comments

Comments
 (0)