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 interface VersionStrategy {
23
24 static VersionStrategy partialMatch(String partial) {
25 return artifactVersion -> partial.isEmpty()
26 || artifactVersion.toString().equals(partial)
27 || artifactVersion.toString().startsWith(partial + '.');
28 }
29
30 static VersionStrategy exactMatch(String version) {
31 return artifactVersion -> artifactVersion.toString().equals(version);
32 }
33
34 static VersionStrategy semVerMatchMajor(int major) {
35 return new SemVerVersionStrategy(major, -1);
36 }
37
38 static VersionStrategy semVerMatchMinor(int major, int minor) {
39 return new SemVerVersionStrategy(major, minor);
40 }
41
42 boolean matchVersion(Version version);
43
44 class SemVerVersionStrategy implements VersionStrategy {
45
46 private static final GenericVersionScheme SCHEME = new GenericVersionScheme();
47
48 private final VersionRange range;
49
50 SemVerVersionStrategy(int major, int minor) {
51
52 StringBuilder matchString = new StringBuilder("[")
53 .append(major)
54 .append('.');
55
56 if (minor > 0) {
57 matchString.append(minor)
58 .append('.');
59 }
60
61 matchString.append("*]");
62
63 try {
64 this.range = SCHEME.parseVersionRange(matchString.toString());
65 } catch (InvalidVersionSpecificationException e) {
66 throw new IllegalArgumentException(e);
67 }
68 }
69
70 @Override
71 public boolean matchVersion(Version version) {
72 return range.containsVersion(version);
73 }
74 }
75 }