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