1 package org.apache.maven.toolchain;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
98 ex.printStackTrace();
99 return false;
100 }
101 }
102
103 @Override
104 public String toString()
105 {
106 return version.toString();
107 }
108 }
109 }