1 package org.apache.maven.shared.artifact.filter.collection;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Arrays;
23 import java.util.LinkedHashSet;
24 import java.util.List;
25 import java.util.Objects;
26 import java.util.Set;
27
28 import org.apache.maven.artifact.Artifact;
29
30 import static org.apache.maven.shared.artifact.filter.internal.Utils.isNotEmpty;
31
32
33
34
35
36
37 public abstract class AbstractArtifactFeatureFilter
38 extends AbstractArtifactsFilter
39 {
40
41 private List<String> includes;
42
43
44
45
46 private List<String> excludes;
47
48
49
50
51
52
53
54 public AbstractArtifactFeatureFilter( String include, String exclude )
55 {
56 setExcludes( exclude );
57 setIncludes( include );
58 }
59
60
61
62
63
64
65 public Set<Artifact> filter( Set<Artifact> artifacts )
66 {
67 Set<Artifact> results = artifacts;
68
69 if ( this.includes != null && !this.includes.isEmpty() )
70 {
71 results = filterIncludes( results, this.includes );
72 }
73
74 if ( this.excludes != null && !this.excludes.isEmpty() )
75 {
76 results = filterExcludes( results, this.excludes );
77 }
78
79 return results;
80 }
81
82
83
84
85
86
87
88
89 private Set<Artifact> filterIncludes( Set<Artifact> artifacts, List<String> theIncludes )
90 {
91 Set<Artifact> result = new LinkedHashSet<>();
92
93 for ( String include : theIncludes )
94 {
95 for ( Artifact artifact : artifacts )
96 {
97
98
99
100
101 if ( compareFeatures( getArtifactFeature( artifact ), include ) )
102 {
103 result.add( artifact );
104 }
105 }
106 }
107 return result;
108 }
109
110
111
112
113
114
115
116
117 private Set<Artifact> filterExcludes( Set<Artifact> artifacts, List<String> theExcludes )
118 {
119 Set<Artifact> result = new LinkedHashSet<>();
120
121 for ( Artifact artifact : artifacts )
122 {
123 boolean exclude = false;
124 String artifactFeature = getArtifactFeature( artifact );
125
126
127
128
129 for ( String excludeFeature : theExcludes )
130 {
131 if ( compareFeatures( artifactFeature, excludeFeature ) )
132 {
133 exclude = true;
134 break;
135 }
136 }
137
138 if ( !exclude )
139 {
140 result.add( artifact );
141 }
142 }
143
144 return result;
145 }
146
147
148
149
150
151
152
153 protected abstract String getArtifactFeature( Artifact artifact );
154
155
156
157
158
159
160 public void setExcludes( String excludeString )
161 {
162 if ( isNotEmpty( excludeString ) )
163 {
164 this.excludes = Arrays.asList( excludeString.split( "," ) );
165 }
166 }
167
168
169
170
171
172
173 public void setIncludes( String includeString )
174 {
175 if ( isNotEmpty( includeString ) )
176 {
177 this.includes = Arrays.asList( includeString.split( "," ) );
178 }
179 }
180
181
182
183
184
185
186 public List<String> getExcludes()
187 {
188 return this.excludes;
189 }
190
191
192
193
194
195
196 public List<String> getIncludes()
197 {
198 return this.includes;
199 }
200
201
202
203
204
205
206
207
208 protected boolean compareFeatures( String lhs, String rhs )
209 {
210 return ( Objects.equals( lhs, rhs ) );
211 }
212 }