1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.maven.toolchain;
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
33 private RequirementMatcherFactory( )
34 {
35 }
36
37 public static RequirementMatcher createExactMatcher( String provideValue )
38 {
39 return new ExactMatcher( provideValue );
40 }
41
42 public static RequirementMatcher createVersionMatcher( String provideValue )
43 {
44 return new VersionMatcher( provideValue );
45 }
46
47 private static final class ExactMatcher
48 implements RequirementMatcher
49 {
50
51 private String provides;
52
53 private ExactMatcher( String provides )
54 {
55 this.provides = provides;
56 }
57
58 public boolean matches( String requirement )
59 {
60 return provides.equalsIgnoreCase( requirement );
61 }
62 }
63
64 private static final class VersionMatcher
65 implements RequirementMatcher
66 {
67
68 DefaultArtifactVersion version;
69
70 private VersionMatcher( String version )
71 {
72 this.version = new DefaultArtifactVersion(version);
73 }
74
75 public boolean matches( String requirement )
76 {
77 try
78 {
79 VersionRange range = VersionRange.createFromVersionSpec(requirement);
80 if (range.hasRestrictions()) {
81 return range.containsVersion(version);
82 } else {
83 return range.getRecommendedVersion().compareTo(version) == 0;
84 }
85 }
86 catch (InvalidVersionSpecificationException ex)
87 {
88
89 ex.printStackTrace();
90 return false;
91 }
92 }
93 }
94 }