1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
77 ex.printStackTrace();
78 return false;
79 }
80 }
81
82 @Override
83 public String toString() {
84 return version.toString();
85 }
86 }
87 }