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