001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014
015package de.softwareforge.testing.maven;
016
017import static java.util.Objects.requireNonNull;
018
019import java.io.IOException;
020import java.util.LinkedList;
021import java.util.Optional;
022import java.util.stream.Collectors;
023
024import org.eclipse.aether.version.Version;
025
026/**
027 * A builder class to control better what versions should be returned.
028 */
029public final class MavenVersionMatchBuilder {
030
031    private final MavenArtifactLoader loader;
032    private final String groupId;
033    private final String artifactId;
034
035    private VersionStrategy versionStrategy = VersionStrategy.partialMatch("");
036
037    private String extension = "jar";
038    private boolean includeSnapshots = true;
039
040    MavenVersionMatchBuilder(MavenArtifactLoader loader, String groupId, String artifactId) {
041        this.loader = loader;
042        this.groupId = groupId;
043        this.artifactId = artifactId;
044    }
045
046    /**
047     * 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
048     * prefix to match a version.
049     *
050     * @param partial The partial version to match.
051     * @return the builder
052     */
053    public MavenVersionMatchBuilder partialMatch(String partial) {
054        this.versionStrategy = VersionStrategy.partialMatch(partial);
055        return this;
056    }
057
058    /**
059     * Support an exact match to a version.
060     *
061     * @param partial The version to match.
062     * @return the builder
063     */
064    public MavenVersionMatchBuilder exactMatch(String partial) {
065        this.versionStrategy = VersionStrategy.exactMatch(partial);
066        return this;
067    }
068
069    /**
070     * Supports semantic versioning, match the major version.
071     *
072     * @param major the major version to match.
073     * @return the builder
074     */
075    public MavenVersionMatchBuilder semVerMajor(int major) {
076        this.versionStrategy = VersionStrategy.semVerMatchMajor(major);
077        return this;
078    }
079
080    /**
081     * Supports semantic versioning, match the major and minor version.
082     *
083     * @param major the major version to match.
084     * @param minor the minor version to match.
085     * @return the builder
086     */
087    public MavenVersionMatchBuilder semVerMinor(int major, int minor) {
088        this.versionStrategy = VersionStrategy.semVerMatchMinor(major, minor);
089        return this;
090    }
091
092    /**
093     * Set the extension to consider. Default is "jar".
094     *
095     * @param extension Sets the extension.
096     * @return the builder
097     */
098    public MavenVersionMatchBuilder extension(String extension) {
099        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}