1
2
3
4
5 package org.apache.maven.api.model;
6
7 import java.io.Serializable;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 import org.apache.maven.api.annotations.Experimental;
15 import org.apache.maven.api.annotations.Generated;
16 import org.apache.maven.api.annotations.Immutable;
17 import org.apache.maven.api.annotations.Nonnull;
18 import org.apache.maven.api.annotations.NotThreadSafe;
19 import org.apache.maven.api.annotations.ThreadSafe;
20
21
22
23
24 @Experimental
25 @Generated @ThreadSafe @Immutable
26 public class PatternSet
27 implements Serializable, InputLocationTracker
28 {
29
30
31
32 final List<String> includes;
33
34
35
36 final List<String> excludes;
37
38 final InputLocation location;
39
40 final InputLocation includesLocation;
41
42 final InputLocation excludesLocation;
43
44 final Map<Object, InputLocation> locations;
45
46
47
48
49
50 PatternSet(
51 Collection<String> includes,
52 Collection<String> excludes,
53 Map<Object, InputLocation> locations,
54 InputLocation location,
55 InputLocation includesLocation,
56 InputLocation excludesLocation
57 )
58 {
59 this.includes = ImmutableCollections.copy( includes );
60 this.excludes = ImmutableCollections.copy( excludes );
61 this.locations = ImmutableCollections.copy( locations );
62 this.location = location;
63 this.includesLocation = includesLocation;
64 this.excludesLocation = excludesLocation;
65 }
66
67
68
69
70
71
72 @Nonnull
73 public List<String> getIncludes()
74 {
75 return this.includes;
76 }
77
78
79
80
81
82
83 @Nonnull
84 public List<String> getExcludes()
85 {
86 return this.excludes;
87 }
88
89
90
91
92 public InputLocation getLocation( Object key )
93 {
94 if ( key instanceof String )
95 {
96 switch ( ( String ) key )
97 {
98 case "":
99 return location;
100 case "includes":
101 return includesLocation;
102 case "excludes":
103 return excludesLocation;
104 }
105 }
106 return locations != null ? locations.get( key ) : null;
107 }
108
109
110
111
112
113
114 @Nonnull
115 public Builder with()
116 {
117 return newBuilder( this );
118 }
119
120
121
122
123
124
125 @Nonnull
126 public PatternSet withIncludes( Collection<String> includes )
127 {
128 return with().includes( includes ).build();
129 }
130
131
132
133
134
135
136 @Nonnull
137 public PatternSet withExcludes( Collection<String> excludes )
138 {
139 return with().excludes( excludes ).build();
140 }
141
142
143
144
145
146
147
148
149 @Nonnull
150 public static PatternSet newInstance()
151 {
152 return newInstance( true );
153 }
154
155
156
157
158
159
160
161
162 @Nonnull
163 public static PatternSet newInstance( boolean withDefaults )
164 {
165 return newBuilder( withDefaults ).build();
166 }
167
168
169
170
171
172
173
174
175 @Nonnull
176 public static Builder newBuilder()
177 {
178 return newBuilder( true );
179 }
180
181
182
183
184
185
186
187 @Nonnull
188 public static Builder newBuilder( boolean withDefaults )
189 {
190 return new Builder( withDefaults );
191 }
192
193
194
195
196
197
198
199
200 @Nonnull
201 public static Builder newBuilder( PatternSet from )
202 {
203 return newBuilder( from, false );
204 }
205
206
207
208
209
210
211
212
213 @Nonnull
214 public static Builder newBuilder( PatternSet from, boolean forceCopy )
215 {
216 return new Builder( from, forceCopy );
217 }
218
219
220
221
222
223
224 @NotThreadSafe
225 public static class Builder
226 {
227 PatternSet base;
228 Collection<String> includes;
229 Collection<String> excludes;
230 Map<Object, InputLocation> locations;
231
232 Builder( boolean withDefaults )
233 {
234 if ( withDefaults )
235 {
236 }
237 }
238
239 Builder( PatternSet base, boolean forceCopy )
240 {
241 if ( forceCopy )
242 {
243 this.includes = base.includes;
244 this.excludes = base.excludes;
245 }
246 else
247 {
248 this.base = base;
249 }
250 }
251
252 @Nonnull
253 public Builder includes( Collection<String> includes )
254 {
255 this.includes = includes;
256 return this;
257 }
258
259 @Nonnull
260 public Builder excludes( Collection<String> excludes )
261 {
262 this.excludes = excludes;
263 return this;
264 }
265
266
267 @Nonnull
268 public Builder location( Object key, InputLocation location )
269 {
270 if ( location != null )
271 {
272 if ( this.locations == null )
273 {
274 this.locations = new HashMap<>();
275 }
276 this.locations.put( key, location );
277 }
278 return this;
279 }
280
281 @Nonnull
282 public PatternSet build()
283 {
284 if ( base != null
285 && ( includes == null || includes == base.includes )
286 && ( excludes == null || excludes == base.excludes )
287 )
288 {
289 return base;
290 }
291 Map<Object, InputLocation> locations = null;
292 InputLocation location = null;
293 InputLocation includesLocation = null;
294 InputLocation excludesLocation = null;
295 if ( this.locations != null )
296 {
297 locations = this.locations;
298 location = locations.remove( "" );
299 includesLocation = locations.remove( "includes" );
300 excludesLocation = locations.remove( "excludes" );
301 }
302 return new PatternSet(
303 includes != null ? includes : ( base != null ? base.includes : null ),
304 excludes != null ? excludes : ( base != null ? base.excludes : null ),
305 locations != null ? locations : ( base != null ? base.locations : null ),
306 location != null ? location : ( base != null ? base.location : null ),
307 includesLocation != null ? includesLocation : ( base != null ? base.includesLocation : null ),
308 excludesLocation != null ? excludesLocation : ( base != null ? base.excludesLocation : null )
309 );
310 }
311 }
312
313
314
315
316
317
318 public String toString()
319 {
320 StringBuilder sb = new StringBuilder( 128 );
321
322 sb.append("PatternSet [includes: {");
323 for (java.util.Iterator i = getIncludes().iterator(); i.hasNext(); )
324 {
325 String str = (String) i.next();
326 sb.append(str).append(", ");
327 }
328 if (sb.substring(sb.length() - 2).equals(", ")) sb.delete(sb.length() - 2, sb.length());
329
330 sb.append("}, excludes: {");
331 for (java.util.Iterator i = getExcludes().iterator(); i.hasNext(); )
332 {
333 String str = (String) i.next();
334 sb.append(str).append(", ");
335 }
336 if (sb.substring(sb.length() - 2).equals(", ")) sb.delete(sb.length() - 2, sb.length());
337
338 sb.append("}]");
339 return sb.toString();
340 }
341
342
343 }