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 java.util.Set;
15 import org.apache.maven.api.annotations.Experimental;
16 import org.apache.maven.api.annotations.Generated;
17 import org.apache.maven.api.annotations.Immutable;
18 import org.apache.maven.api.annotations.Nonnull;
19 import org.apache.maven.api.annotations.NotThreadSafe;
20 import org.apache.maven.api.annotations.ThreadSafe;
21
22
23
24
25 @Experimental
26 @Generated @ThreadSafe @Immutable
27 public class PatternSet
28 implements Serializable, InputLocationTracker
29 {
30
31
32
33 final List<String> includes;
34
35
36
37 final List<String> excludes;
38
39 final Map<Object, InputLocation> locations;
40
41 final InputLocation importedFrom;
42
43
44
45
46
47 protected PatternSet(Builder builder) {
48 this.includes = ImmutableCollections.copy(builder.includes != null ? builder.includes : (builder.base != null ? builder.base.includes : null));
49 this.excludes = ImmutableCollections.copy(builder.excludes != null ? builder.excludes : (builder.base != null ? builder.base.excludes : null));
50 Map<Object, InputLocation> newlocs = builder.locations != null ? builder.locations : Collections.emptyMap();
51 Map<Object, InputLocation> oldlocs = builder.base != null && builder.base.locations != null ? builder.base.locations : Collections.emptyMap();
52 Map<Object, InputLocation> mutableLocations = new HashMap<>();
53 this.importedFrom = builder.importedFrom;
54 mutableLocations.put("", newlocs.containsKey("") ? newlocs.get("") : oldlocs.get(""));
55 mutableLocations.put("includes", newlocs.containsKey("includes") ? newlocs.get("includes") : oldlocs.get("includes"));
56 mutableLocations.put("excludes", newlocs.containsKey("excludes") ? newlocs.get("excludes") : oldlocs.get("excludes"));
57 this.locations = Collections.unmodifiableMap(mutableLocations);
58 }
59
60
61
62
63
64
65 @Nonnull
66 public List<String> getIncludes() {
67 return this.includes;
68 }
69
70
71
72
73
74
75 @Nonnull
76 public List<String> getExcludes() {
77 return this.excludes;
78 }
79
80
81
82
83 public InputLocation getLocation(Object key) {
84 return locations != null ? locations.get(key) : null;
85 }
86
87
88
89
90 public Set<Object> getLocationKeys() {
91 return locations != null ? locations.keySet() : null;
92 }
93
94
95
96
97 public InputLocation getImportedFrom()
98 {
99 return importedFrom;
100 }
101
102
103
104
105
106
107 @Nonnull
108 public Builder with() {
109 return newBuilder(this);
110 }
111
112
113
114
115
116
117 @Nonnull
118 public PatternSet withIncludes(Collection<String> includes) {
119 return newBuilder(this, true).includes(includes).build();
120 }
121
122
123
124
125
126
127 @Nonnull
128 public PatternSet withExcludes(Collection<String> excludes) {
129 return newBuilder(this, true).excludes(excludes).build();
130 }
131
132
133
134
135
136
137
138
139 @Nonnull
140 public static PatternSet newInstance() {
141 return newInstance(true);
142 }
143
144
145
146
147
148
149
150
151 @Nonnull
152 public static PatternSet newInstance(boolean withDefaults) {
153 return newBuilder(withDefaults).build();
154 }
155
156
157
158
159
160
161
162
163 @Nonnull
164 public static Builder newBuilder() {
165 return newBuilder(true);
166 }
167
168
169
170
171
172
173
174 @Nonnull
175 public static Builder newBuilder(boolean withDefaults) {
176 return new Builder(withDefaults);
177 }
178
179
180
181
182
183
184
185
186 @Nonnull
187 public static Builder newBuilder(PatternSet from) {
188 return newBuilder(from, false);
189 }
190
191
192
193
194
195
196
197
198 @Nonnull
199 public static Builder newBuilder(PatternSet from, boolean forceCopy) {
200 return new Builder(from, forceCopy);
201 }
202
203
204
205
206
207
208 @NotThreadSafe
209 public static class Builder
210 {
211 PatternSet base;
212 Collection<String> includes;
213 Collection<String> excludes;
214 Map<Object, InputLocation> locations;
215 InputLocation importedFrom;
216
217 protected Builder(boolean withDefaults) {
218 if (withDefaults) {
219 }
220 }
221
222 protected Builder(PatternSet base, boolean forceCopy) {
223 if (forceCopy) {
224 this.includes = base.includes;
225 this.excludes = base.excludes;
226 this.locations = base.locations;
227 this.importedFrom = base.importedFrom;
228 } else {
229 this.base = base;
230 }
231 }
232
233 @Nonnull
234 public Builder includes(Collection<String> includes) {
235 this.includes = includes;
236 return this;
237 }
238
239 @Nonnull
240 public Builder excludes(Collection<String> excludes) {
241 this.excludes = excludes;
242 return this;
243 }
244
245
246 @Nonnull
247 public Builder location(Object key, InputLocation location) {
248 if (location != null) {
249 if (!(this.locations instanceof HashMap)) {
250 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
251 }
252 this.locations.put(key, location);
253 }
254 return this;
255 }
256
257 @Nonnull
258 public Builder importedFrom(InputLocation importedFrom) {
259 this.importedFrom = importedFrom;
260 return this;
261 }
262
263 @Nonnull
264 public PatternSet build() {
265
266 if (base != null
267 && (includes == null || includes == base.includes)
268 && (excludes == null || excludes == base.excludes)
269 ) {
270 return base;
271 }
272 return new PatternSet(this);
273 }
274 }
275
276
277
278
279
280
281 public String toString() {
282 return "PatternSet [" +
283 "includes: {" + getIncludes().stream().collect(java.util.stream.Collectors.joining(", ")) + "}, " +
284 "excludes: {" + getExcludes().stream().collect(java.util.stream.Collectors.joining(", ")) + "}]";
285 }
286
287
288 }