1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package de.softwareforge.testing.maven;
16
17 import org.eclipse.aether.util.version.GenericVersionScheme;
18 import org.eclipse.aether.version.InvalidVersionSpecificationException;
19 import org.eclipse.aether.version.Version;
20 import org.eclipse.aether.version.VersionRange;
21
22 @SuppressWarnings("PMD.ImplicitFunctionalInterface")
23 interface VersionStrategy {
24
25 static VersionStrategy partialMatch(String partial) {
26 return artifactVersion -> partial.isEmpty()
27 || artifactVersion.toString().equals(partial)
28 || artifactVersion.toString().startsWith(partial + '.');
29 }
30
31 static VersionStrategy exactMatch(String version) {
32 return artifactVersion -> artifactVersion.toString().equals(version);
33 }
34
35 static VersionStrategy semVerMatchMajor(int major) {
36 return new SemVerVersionStrategy(major, -1);
37 }
38
39 static VersionStrategy semVerMatchMinor(int major, int minor) {
40 return new SemVerVersionStrategy(major, minor);
41 }
42
43 boolean matchVersion(Version version);
44
45 class SemVerVersionStrategy implements VersionStrategy {
46
47 private static final GenericVersionScheme SCHEME = new GenericVersionScheme();
48
49 private final VersionRange range;
50
51 SemVerVersionStrategy(int major, int minor) {
52
53 StringBuilder matchString = new StringBuilder("[")
54 .append(major)
55 .append('.');
56
57 if (minor > 0) {
58 matchString.append(minor)
59 .append('.');
60 }
61
62 matchString.append("*]");
63
64 try {
65 this.range = SCHEME.parseVersionRange(matchString.toString());
66 } catch (InvalidVersionSpecificationException e) {
67 throw new IllegalArgumentException(e);
68 }
69 }
70
71 @Override
72 public boolean matchVersion(Version version) {
73 return range.containsVersion(version);
74 }
75 }
76 }