1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package de.softwareforge.testing.maven;
16
17 import static java.util.Objects.requireNonNull;
18
19 import java.io.IOException;
20 import java.util.LinkedList;
21 import java.util.Optional;
22 import java.util.stream.Collectors;
23
24 import org.eclipse.aether.version.Version;
25
26
27
28
29 public final class MavenVersionMatchBuilder {
30
31 private final MavenArtifactLoader loader;
32 private final String groupId;
33 private final String artifactId;
34
35 private VersionStrategy versionStrategy = VersionStrategy.partialMatch("");
36
37 private String extension = "jar";
38 private boolean includeSnapshots = true;
39
40 MavenVersionMatchBuilder(MavenArtifactLoader loader, String groupId, String artifactId) {
41 this.loader = loader;
42 this.groupId = groupId;
43 this.artifactId = artifactId;
44 }
45
46
47
48
49
50
51
52
53 public MavenVersionMatchBuilder partialMatch(String partial) {
54 this.versionStrategy = VersionStrategy.partialMatch(partial);
55 return this;
56 }
57
58
59
60
61
62
63
64 public MavenVersionMatchBuilder exactMatch(String partial) {
65 this.versionStrategy = VersionStrategy.exactMatch(partial);
66 return this;
67 }
68
69
70
71
72
73
74
75 public MavenVersionMatchBuilder semVerMajor(int major) {
76 this.versionStrategy = VersionStrategy.semVerMatchMajor(major);
77 return this;
78 }
79
80
81
82
83
84
85
86
87 public MavenVersionMatchBuilder semVerMinor(int major, int minor) {
88 this.versionStrategy = VersionStrategy.semVerMatchMinor(major, minor);
89 return this;
90 }
91
92
93
94
95
96
97
98 public MavenVersionMatchBuilder extension(String extension) {
99 this.extension = requireNonNull(extension, "extension is null");
100 return this;
101 }
102
103
104
105
106
107
108
109 public MavenVersionMatchBuilder includeSnapshots(boolean includeSnapshots) {
110 this.includeSnapshots = includeSnapshots;
111 return this;
112 }
113
114 String groupId() {
115 return groupId;
116 }
117
118 String artifactId() {
119 return artifactId;
120 }
121
122 VersionStrategy versionStrategy() {
123 return versionStrategy;
124 }
125
126 String extension() {
127 return extension;
128 }
129
130 boolean includeSnapshots() {
131 return includeSnapshots;
132 }
133
134
135
136
137
138
139 public LinkedList<String> findAll() throws IOException {
140 return loader.findAllVersions(this)
141 .stream()
142 .map(Version::toString)
143 .collect(Collectors.toCollection(LinkedList::new));
144 }
145
146
147
148
149
150
151 public Optional<String> findBestMatch() throws IOException {
152 LinkedList<String> versions = findAll();
153 return versions.isEmpty() ? Optional.empty() : Optional.of(versions.getLast());
154 }
155 }