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