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