View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.toolchain;
20  
21  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
22  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
23  import org.apache.maven.artifact.versioning.VersionRange;
24  
25  /**
26   *
27   * @author mkleint
28   */
29  public final class RequirementMatcherFactory {
30      private RequirementMatcherFactory() {}
31  
32      public static RequirementMatcher createExactMatcher(String provideValue) {
33          return new ExactMatcher(provideValue);
34      }
35  
36      public static RequirementMatcher createVersionMatcher(String provideValue) {
37          return new VersionMatcher(provideValue);
38      }
39  
40      private static final class ExactMatcher implements RequirementMatcher {
41  
42          private String provides;
43  
44          private ExactMatcher(String provides) {
45              this.provides = provides;
46          }
47  
48          @Override
49          public boolean matches(String requirement) {
50              return provides.equalsIgnoreCase(requirement);
51          }
52  
53          @Override
54          public String toString() {
55              return provides;
56          }
57      }
58  
59      private static final class VersionMatcher implements RequirementMatcher {
60          DefaultArtifactVersion version;
61  
62          private VersionMatcher(String version) {
63              this.version = new DefaultArtifactVersion(version);
64          }
65  
66          @Override
67          public boolean matches(String requirement) {
68              try {
69                  VersionRange range = VersionRange.createFromVersionSpec(requirement);
70                  if (range.hasRestrictions()) {
71                      return range.containsVersion(version);
72                  } else {
73                      return range.getRecommendedVersion().compareTo(version) == 0;
74                  }
75              } catch (InvalidVersionSpecificationException ex) {
76                  // TODO error reporting
77                  ex.printStackTrace();
78                  return false;
79              }
80          }
81  
82          @Override
83          public String toString() {
84              return version.toString();
85          }
86      }
87  }