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