Skip to content

Commit 67c1800

Browse files
andrzejj0slawekjaranowski
authored andcommitted
Resolves #872: Make allowSnapshots an explicit argument in lookupDependencyUpdates and in reports
1 parent 2fe2c3d commit 67c1800

File tree

41 files changed

+474
-296
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+474
-296
lines changed

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

+13-65
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,6 @@ public abstract class AbstractVersionDetails implements VersionDetails {
6464
*/
6565
private ArtifactVersion currentVersion = null;
6666

67-
/**
68-
* Do we want to include snapshots when snapshot inclusion is not specified. Guarded by {@link #currentVersionLock}.
69-
*
70-
* @since 1.0-beta-1
71-
*/
72-
private boolean includeSnapshots = false;
73-
7467
protected boolean verboseDetail = true;
7568

7669
/**
@@ -145,50 +138,18 @@ public final void setCurrentVersion(String currentVersion) {
145138
setCurrentVersion(currentVersion == null ? null : new DefaultArtifactVersion(currentVersion));
146139
}
147140

148-
@Override
149-
public final boolean isIncludeSnapshots() {
150-
synchronized (currentVersionLock) {
151-
return includeSnapshots;
152-
}
153-
}
154-
155-
@Override
156-
public final void setIncludeSnapshots(boolean includeSnapshots) {
157-
synchronized (currentVersionLock) {
158-
this.includeSnapshots = includeSnapshots;
159-
}
160-
}
161-
162-
@Override
163-
public final ArtifactVersion[] getVersions() {
164-
return getVersions(isIncludeSnapshots());
165-
}
166-
167-
@Override
168-
public abstract ArtifactVersion[] getVersions(boolean includeSnapshots);
169-
170141
@Override
171142
public final ArtifactVersion[] getVersions(VersionRange versionRange, boolean includeSnapshots) {
172143
return getVersions(versionRange, null, includeSnapshots);
173144
}
174145

175-
@Override
176-
public final ArtifactVersion[] getVersions(ArtifactVersion lowerBound, ArtifactVersion upperBound) {
177-
return getVersions(lowerBound, upperBound, isIncludeSnapshots());
178-
}
179-
180146
@Override
181147
public final ArtifactVersion[] getVersions(
182148
ArtifactVersion lowerBound, ArtifactVersion upperBound, boolean includeSnapshots) {
183149
Restriction restriction = new Restriction(lowerBound, false, upperBound, false);
184150
return getVersions(restriction, includeSnapshots);
185151
}
186152

187-
@Override
188-
public final ArtifactVersion getNewestVersion(ArtifactVersion lowerBound, ArtifactVersion upperBound) {
189-
return getNewestVersion(lowerBound, upperBound, isIncludeSnapshots());
190-
}
191-
192153
@Override
193154
public final ArtifactVersion getNewestVersion(
194155
ArtifactVersion lowerBound, ArtifactVersion upperBound, boolean includeSnapshots) {
@@ -334,16 +295,6 @@ public final ArtifactVersion[] getAllUpdates(
334295
}
335296
}
336297

337-
@Override
338-
public final ArtifactVersion getNewestUpdate(Optional<Segment> updateScope) {
339-
return getNewestUpdate(updateScope, isIncludeSnapshots());
340-
}
341-
342-
@Override
343-
public final ArtifactVersion[] getAllUpdates(Optional<Segment> updateScope) {
344-
return getAllUpdates(updateScope, isIncludeSnapshots());
345-
}
346-
347298
@Override
348299
public final ArtifactVersion getNewestUpdate(Optional<Segment> updateScope, boolean includeSnapshots) {
349300
if (isCurrentVersionDefined()) {
@@ -361,13 +312,8 @@ public final ArtifactVersion[] getAllUpdates(Optional<Segment> updateScope, bool
361312
}
362313

363314
@Override
364-
public final ArtifactVersion[] getAllUpdates() {
365-
return getAllUpdates((VersionRange) null, isIncludeSnapshots());
366-
}
367-
368-
@Override
369-
public final ArtifactVersion[] getAllUpdates(VersionRange versionRange) {
370-
return getAllUpdates(versionRange, isIncludeSnapshots());
315+
public final ArtifactVersion[] getAllUpdates(boolean includeSnapshots) {
316+
return getAllUpdates((VersionRange) null, includeSnapshots);
371317
}
372318

373319
@Override
@@ -441,24 +387,26 @@ public boolean isVersionInRestriction(Restriction restriction, ArtifactVersion c
441387

442388
/**
443389
* Returns the latest version newer than the specified current version, and within the specified update scope,
444-
* or <code>null</code> if no such version exists.
390+
* or {@code null} if no such version exists.
445391
* @param updateScope the scope of updates to include.
392+
* @param includeSnapshots whether snapshots should be included
446393
* @return the newest version after currentVersion within the specified update scope,
447394
* or <code>null</code> if no version is available.
448395
*/
449-
public final ArtifactVersion getReportNewestUpdate(Optional<Segment> updateScope) {
450-
return getArtifactVersionStream(updateScope)
396+
public final ArtifactVersion getReportNewestUpdate(Optional<Segment> updateScope, boolean includeSnapshots) {
397+
return getArtifactVersionStream(updateScope, includeSnapshots)
451398
.min(Collections.reverseOrder(getVersionComparator()))
452399
.orElse(null);
453400
}
454401

455402
/**
456403
* Returns all versions newer than the specified current version, and within the specified update scope.
457404
* @param updateScope the scope of updates to include.
405+
* @param includeSnapshots whether snapshots should be included
458406
* @return all versions after currentVersion within the specified update scope.
459407
*/
460-
public final ArtifactVersion[] getReportUpdates(Optional<Segment> updateScope) {
461-
TreeSet<ArtifactVersion> versions = getArtifactVersionStream(updateScope)
408+
public final ArtifactVersion[] getReportUpdates(Optional<Segment> updateScope, boolean includeSnapshots) {
409+
TreeSet<ArtifactVersion> versions = getArtifactVersionStream(updateScope, includeSnapshots)
462410
.collect(Collectors.toCollection(() -> new TreeSet<>(getVersionComparator())));
463411
// filter out intermediate minor versions.
464412
if (!verboseDetail) {
@@ -493,16 +441,16 @@ public final ArtifactVersion[] getReportUpdates(Optional<Segment> updateScope) {
493441
/**
494442
* Returns all versions newer than the specified current version, and within the specified update scope.
495443
* @param updateScope the scope of updates to include.
444+
* @param includeSnapshots whether snapshots should be included
496445
* @return all versions after currentVersion within the specified update scope.
497446
*/
498-
private Stream<ArtifactVersion> getArtifactVersionStream(Optional<Segment> updateScope) {
447+
private Stream<ArtifactVersion> getArtifactVersionStream(Optional<Segment> updateScope, boolean includeSnapshots) {
499448
if (isCurrentVersionDefined()) {
500449
try {
501450
Restriction restriction = restrictionFor(updateScope);
502451

503-
return Arrays.stream(getVersions())
504-
.filter(candidate -> (isIncludeSnapshots() || !ArtifactUtils.isSnapshot(candidate.toString()))
505-
&& isVersionInRestriction(restriction, candidate));
452+
return Arrays.stream(getVersions(includeSnapshots))
453+
.filter(candidate -> isVersionInRestriction(restriction, candidate));
506454
} catch (InvalidSegmentException ignored) {
507455
ignored.printStackTrace(System.err);
508456
}

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

+20-3
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ public ArtifactVersions(ArtifactVersions other) {
9191
versionComparator = other.versionComparator;
9292
versions = other.versions;
9393
setCurrentVersion(other.getCurrentVersion());
94-
setIncludeSnapshots(other.isIncludeSnapshots());
9594
}
9695

9796
@SuppressWarnings("checkstyle:InnerAssignment")
@@ -122,7 +121,7 @@ public boolean equals(Object o) {
122121

123122
return new EqualsBuilder()
124123
.append(getArtifact(), that.getArtifact())
125-
.append(getVersions(), that.getVersions())
124+
.append(getVersions(true), that.getVersions(true))
126125
.append(getVersionComparator(), that.getVersionComparator())
127126
.isEquals();
128127
}
@@ -131,7 +130,7 @@ public boolean equals(Object o) {
131130
public int hashCode() {
132131
return new HashCodeBuilder(17, 37)
133132
.append(getArtifact())
134-
.append(getVersions())
133+
.append(getVersions(true))
135134
.append(getVersionComparator())
136135
.toHashCode();
137136
}
@@ -218,6 +217,24 @@ public ArtifactVersion[] getVersions(boolean includeSnapshots) {
218217
.toArray(ArtifactVersion[]::new);
219218
}
220219

220+
/**
221+
* Says whether the versions present in the {@link ArtifactVersions} collection are empty.
222+
* If {@code includeSnapshots} is {@code true}, snapshots will not counted, which means that
223+
* the method will only count release versions.
224+
*
225+
* @param includeSnapshots {@code includeSnapshots} is {@code true}, snapshots will not counted, which means that
226+
* * the method will only count release versions.
227+
* @return if {@code includeSnapshots} is {@code true}, returns {@code true} if there are no versions.
228+
* if {@code includeSnapshots} is {@code false}, returns {@code true} if there are no releases.
229+
*/
230+
public boolean isEmpty(boolean includeSnapshots) {
231+
return includeSnapshots
232+
? versions.isEmpty()
233+
// the below means: the only versions that could be present in the collection
234+
// are snapshots
235+
: versions.stream().map(Object::toString).allMatch(ArtifactUtils::isSnapshot);
236+
}
237+
221238
public VersionComparator getVersionComparator() {
222239
return versionComparator;
223240
}

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

+12-9
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,24 @@
2222
import java.util.HashMap;
2323
import java.util.Map;
2424
import java.util.Optional;
25-
import java.util.function.BiFunction;
2625

27-
import org.apache.commons.lang3.tuple.Pair;
26+
import org.apache.commons.lang3.function.TriFunction;
27+
import org.apache.commons.lang3.tuple.Triple;
2828

2929
/**
30-
* Utility providing a cached {@link ArtifactVersions#getNewestUpdate(Optional)} API
30+
* Utility providing a cached {@link ArtifactVersions#getNewestUpdate(Optional, boolean)} API
3131
*/
3232
public class ArtifactVersionsCache {
33-
private BiFunction<AbstractVersionDetails, Optional<Segment>, ?> cachedFunction;
34-
35-
private Map<Pair<? extends AbstractVersionDetails, Optional<Segment>>, Object> updateCache = new HashMap<>();
33+
private TriFunction<AbstractVersionDetails, Optional<Segment>, Boolean, ?> cachedFunction;
34+
private Map<Triple<? extends AbstractVersionDetails, Optional<Segment>, Boolean>, Object> updateCache =
35+
new HashMap<>();
3636

3737
/**
3838
* Constructs a new instance given the concrete function for obtaining the details
3939
*
4040
* @param cachedFunction reference to the function computing the required information
4141
*/
42-
public ArtifactVersionsCache(BiFunction<AbstractVersionDetails, Optional<Segment>, ?> cachedFunction) {
42+
public ArtifactVersionsCache(TriFunction<AbstractVersionDetails, Optional<Segment>, Boolean, ?> cachedFunction) {
4343
this.cachedFunction = cachedFunction;
4444
}
4545

@@ -52,11 +52,14 @@ public ArtifactVersionsCache(BiFunction<AbstractVersionDetails, Optional<Segment
5252
* @param <R> return type of the cached function
5353
* @param artifactVersions {@linkplain ArtifactVersions} object referring to the given dependency
5454
* @param updateScope update scope
55+
* @param allowSnapshots whether snapshots should be included
5556
* @return last retrieved update information
5657
*/
5758
@SuppressWarnings("unchecked")
58-
public <V extends AbstractVersionDetails, R> R get(V artifactVersions, Optional<Segment> updateScope) {
59+
public <V extends AbstractVersionDetails, R> R get(
60+
V artifactVersions, Optional<Segment> updateScope, boolean allowSnapshots) {
5961
return (R) updateCache.computeIfAbsent(
60-
Pair.of(artifactVersions, updateScope), pair -> cachedFunction.apply(pair.getLeft(), pair.getRight()));
62+
Triple.of(artifactVersions, updateScope, allowSnapshots),
63+
triple -> cachedFunction.apply(triple.getLeft(), triple.getMiddle(), triple.getRight()));
6164
}
6265
}

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

+8-14
Original file line numberDiff line numberDiff line change
@@ -422,23 +422,16 @@ public ArtifactVersion createArtifactVersion(String version) {
422422
return new DefaultArtifactVersion(version);
423423
}
424424

425-
/**
426-
* Returns a map of all possible updates per dependency. The lookup is done in parallel using
427-
* {@code LOOKUP_PARALLEL_THREADS} threads.
428-
*
429-
* @param dependencies The set of {@link Dependency} instances to look up.
430-
* @param usePluginRepositories Search the plugin repositories.
431-
* @return map containing the ArtifactVersions object per dependency
432-
*/
433425
@Override
434426
public Map<Dependency, ArtifactVersions> lookupDependenciesUpdates(
435-
Set<Dependency> dependencies, boolean usePluginRepositories) throws VersionRetrievalException {
427+
Set<Dependency> dependencies, boolean usePluginRepositories, boolean allowSnapshots)
428+
throws VersionRetrievalException {
436429
ExecutorService executor = Executors.newFixedThreadPool(LOOKUP_PARALLEL_THREADS);
437430
try {
438431
Map<Dependency, ArtifactVersions> dependencyUpdates = new TreeMap<>(DependencyComparator.INSTANCE);
439432
List<Future<? extends Pair<Dependency, ArtifactVersions>>> futures = dependencies.stream()
440433
.map(dependency -> executor.submit(() -> new ImmutablePair<>(
441-
dependency, lookupDependencyUpdates(dependency, usePluginRepositories))))
434+
dependency, lookupDependencyUpdates(dependency, usePluginRepositories, allowSnapshots))))
442435
.collect(Collectors.toList());
443436
for (Future<? extends Pair<Dependency, ArtifactVersions>> details : futures) {
444437
Pair<Dependency, ArtifactVersions> pair = details.get();
@@ -455,13 +448,14 @@ dependency, lookupDependencyUpdates(dependency, usePluginRepositories))))
455448
}
456449

457450
@Override
458-
public ArtifactVersions lookupDependencyUpdates(Dependency dependency, boolean usePluginRepositories)
451+
public ArtifactVersions lookupDependencyUpdates(
452+
Dependency dependency, boolean usePluginRepositories, boolean allowSnapshots)
459453
throws VersionRetrievalException {
460454
ArtifactVersions allVersions =
461455
lookupArtifactVersions(createDependencyArtifact(dependency), usePluginRepositories);
462456
return new ArtifactVersions(
463457
allVersions.getArtifact(),
464-
Arrays.stream(allVersions.getAllUpdates()).collect(Collectors.toList()),
458+
Arrays.stream(allVersions.getAllUpdates(allowSnapshots)).collect(Collectors.toList()),
465459
allVersions.getVersionComparator());
466460
}
467461

@@ -498,13 +492,13 @@ public PluginUpdatesDetails lookupPluginUpdates(Plugin plugin, boolean allowSnap
498492
pluginDependencies.addAll(plugin.getDependencies());
499493
}
500494
Map<Dependency, ArtifactVersions> pluginDependencyDetails =
501-
lookupDependenciesUpdates(pluginDependencies, false);
495+
lookupDependenciesUpdates(pluginDependencies, false, allowSnapshots);
502496

503497
ArtifactVersions allVersions = lookupArtifactVersions(
504498
createPluginArtifact(plugin.getGroupId(), plugin.getArtifactId(), version), true);
505499
ArtifactVersions updatedVersions = new ArtifactVersions(
506500
allVersions.getArtifact(),
507-
Arrays.stream(allVersions.getAllUpdates()).collect(Collectors.toList()),
501+
Arrays.stream(allVersions.getAllUpdates(allowSnapshots)).collect(Collectors.toList()),
508502
allVersions.getVersionComparator());
509503
return new PluginUpdatesDetails(updatedVersions, pluginDependencyDetails, allowSnapshots);
510504
}

0 commit comments

Comments
 (0)