View Javadoc
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 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  }