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 java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.Map;
24 import java.util.Objects;
25 import java.util.Properties;
26
27 import org.apache.maven.toolchain.model.ToolchainModel;
28 import org.slf4j.Logger;
29
30
31
32
33
34
35
36
37 public abstract class DefaultToolchain
38 implements Toolchain, ToolchainPrivate {
39 private final Logger logger;
40
41 private String type;
42
43 private Map<String, RequirementMatcher> provides = new HashMap<>();
44
45 public static final String KEY_TYPE = "type";
46
47 private ToolchainModel model;
48
49
50
51
52
53
54 protected DefaultToolchain(ToolchainModel model, Logger logger) {
55 this.model = model;
56 this.logger = logger;
57 }
58
59
60
61
62
63
64
65 protected DefaultToolchain(ToolchainModel model, String type, Logger logger) {
66 this(model, logger);
67 this.type = type;
68 }
69
70 @Override
71 public final String getType() {
72 return type != null ? type : model.getType();
73 }
74
75 @Override
76 public final ToolchainModel getModel() {
77 return model;
78 }
79
80 public final void addProvideToken(String type, RequirementMatcher matcher) {
81 provides.put(type, matcher);
82 }
83
84 @Override
85 public boolean matchesRequirements(Map<String, String> requirements) {
86 for (Map.Entry<String, String> requirement : requirements.entrySet()) {
87 String key = requirement.getKey();
88
89 RequirementMatcher matcher = provides.get(key);
90
91 if (matcher == null) {
92 getLog().debug("Toolchain " + this + " is missing required property: " + key);
93 return false;
94 }
95 if (!matcher.matches(requirement.getValue())) {
96 getLog().debug("Toolchain " + this + " doesn't match required property: " + key);
97 return false;
98 }
99 }
100 return true;
101 }
102
103 protected Logger getLog() {
104 return logger;
105 }
106
107 @Override
108 public boolean equals(Object obj) {
109 if (obj == null) {
110 return false;
111 }
112
113 if (this == obj) {
114 return true;
115 }
116
117 if (!(obj instanceof DefaultToolchain)) {
118 return false;
119 }
120
121 DefaultToolchain other = (DefaultToolchain) obj;
122
123 if (!Objects.equals(type, other.type)) {
124 return false;
125 }
126
127 Properties thisProvides = this.getModel().getProvides();
128 Properties otherProvides = other.getModel().getProvides();
129
130 return Objects.equals(thisProvides, otherProvides);
131 }
132
133 @Override
134 public int hashCode() {
135 int hashCode = (type == null) ? 0 : type.hashCode();
136
137 if (this.getModel().getProvides() != null) {
138 hashCode = 31 * hashCode + this.getModel().getProvides().hashCode();
139 }
140 return hashCode;
141 }
142
143 @Override
144 public String toString() {
145 StringBuilder builder = new StringBuilder();
146 builder.append("type:").append(getType());
147 builder.append('{');
148
149 Iterator<Map.Entry<String, RequirementMatcher>> providesIter =
150 provides.entrySet().iterator();
151 while (providesIter.hasNext()) {
152 Map.Entry<String, RequirementMatcher> provideEntry = providesIter.next();
153 builder.append(provideEntry.getKey()).append(" = ").append(provideEntry.getValue());
154 if (providesIter.hasNext()) {
155 builder.append(';');
156 }
157 }
158
159 builder.append('}');
160
161 return builder.toString();
162 }
163 }