1 /*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
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 * A builder class to control better what versions should be returned.
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 * Support a partial match to the given version string. If the value is empty, any version will match. Otherwise, the version must be either exact or a
48 * prefix to match a version.
49 *
50 * @param partial The partial version to match.
51 * @return the builder
52 */
53 public MavenVersionMatchBuilder partialMatch(String partial) {
54 this.versionStrategy = VersionStrategy.partialMatch(partial);
55 return this;
56 }
57
58 /**
59 * Support an exact match to a version.
60 *
61 * @param partial The version to match.
62 * @return the builder
63 */
64 public MavenVersionMatchBuilder exactMatch(String partial) {
65 this.versionStrategy = VersionStrategy.exactMatch(partial);
66 return this;
67 }
68
69 /**
70 * Supports semantic versioning, match the major version.
71 *
72 * @param major the major version to match.
73 * @return the builder
74 */
75 public MavenVersionMatchBuilder semVerMajor(int major) {
76 this.versionStrategy = VersionStrategy.semVerMatchMajor(major);
77 return this;
78 }
79
80 /**
81 * Supports semantic versioning, match the major and minor version.
82 *
83 * @param major the major version to match.
84 * @param minor the minor version to match.
85 * @return the builder
86 */
87 public MavenVersionMatchBuilder semVerMinor(int major, int minor) {
88 this.versionStrategy = VersionStrategy.semVerMatchMinor(major, minor);
89 return this;
90 }
91
92 /**
93 * Set the extension to consider. Default is "jar".
94 *
95 * @param extension Sets the extension.
96 * @return the builder
97 */
98 public MavenVersionMatchBuilder extension(String extension) {
99 this.extension = requireNonNull(extension, "extension is null");
100 return this;
101 }
102
103 /**
104 * If true, snapshots are included in the results.
105 *
106 * @param includeSnapshots If true, include snapshots in the results. Default is {@code true}.
107 * @return the builder
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 * Returns a list of all versions that match the search constraints.
136 * @return A list of versions. This list may be empty but is never null.
137 * @throws IOException If the underlying code encounters an IO problem (e.g. no network connection).
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 * Returns the best match for the given version contraints.
148 * @return The best match for the given version constraints. Can be {@link Optional#empty()} if no version matches.
149 * @throws IOException If the underlying code encounters an IO problem (e.g. no network connection).
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 }