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