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