View Javadoc
1   // =================== DO NOT EDIT THIS FILE ====================
2   //  Generated by Modello Velocity from model.vm
3   //  template, any modifications will be overwritten.
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   * Definition of include or exclude patterns.
24   */
25  @Experimental
26  @Generated @ThreadSafe @Immutable
27  public class PatternSet
28      implements Serializable, InputLocationTracker
29  {
30      /**
31       * A list of patterns to include, e.g. {@code **/*.xml}.
32       */
33      final List<String> includes;
34      /**
35       * A list of patterns to exclude, e.g. {@code **&#47;*.xml}
36       */
37      final List<String> excludes;
38      /** Locations (this potentially hides the same name field from the super class) */
39      final Map<Object, InputLocation> locations;
40      /** Location tracking */
41      final InputLocation importedFrom;
42  
43      /**
44        * Constructor for this class, to be called from its subclasses and {@link Builder}.
45        * @see Builder#build()
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       * A list of patterns to include, e.g. {@code **&#47;*.xml}.
62       *
63       * @return a {@code List<String>}
64       */
65      @Nonnull
66      public List<String> getIncludes() {
67          return this.includes;
68      }
69  
70      /**
71       * A list of patterns to exclude, e.g. {@code **&#47;*.xml}
72       *
73       * @return a {@code List<String>}
74       */
75      @Nonnull
76      public List<String> getExcludes() {
77          return this.excludes;
78      }
79  
80      /**
81       * Gets the location of the specified field in the input source.
82       */
83      public InputLocation getLocation(Object key) {
84          return locations != null ? locations.get(key) : null;
85      }
86  
87      /**
88      * Gets the keys of the locations of the input source.
89      */
90      public Set<Object> getLocationKeys() {
91          return locations != null ? locations.keySet() : null;
92      }
93  
94      /**
95       * Gets the input location that caused this model to be read.
96       */
97      public InputLocation getImportedFrom()
98      {
99          return importedFrom;
100     }
101 
102     /**
103      * Creates a new builder with this object as the basis.
104      *
105      * @return a {@code Builder}
106      */
107     @Nonnull
108     public Builder with() {
109         return newBuilder(this);
110     }
111     /**
112      * Creates a new {@code PatternSet} instance using the specified includes.
113      *
114      * @param includes the new {@code Collection<String>} to use
115      * @return a {@code PatternSet} with the specified includes
116      */
117     @Nonnull
118     public PatternSet withIncludes(Collection<String> includes) {
119         return newBuilder(this, true).includes(includes).build();
120     }
121     /**
122      * Creates a new {@code PatternSet} instance using the specified excludes.
123      *
124      * @param excludes the new {@code Collection<String>} to use
125      * @return a {@code PatternSet} with the specified excludes
126      */
127     @Nonnull
128     public PatternSet withExcludes(Collection<String> excludes) {
129         return newBuilder(this, true).excludes(excludes).build();
130     }
131 
132     /**
133      * Creates a new {@code PatternSet} instance.
134      * Equivalent to {@code newInstance(true)}.
135      * @see #newInstance(boolean)
136      *
137      * @return a new {@code PatternSet}
138      */
139     @Nonnull
140     public static PatternSet newInstance() {
141         return newInstance(true);
142     }
143 
144     /**
145      * Creates a new {@code PatternSet} instance using default values or not.
146      * Equivalent to {@code newBuilder(withDefaults).build()}.
147      *
148      * @param withDefaults the boolean indicating whether default values should be used
149      * @return a new {@code PatternSet}
150      */
151     @Nonnull
152     public static PatternSet newInstance(boolean withDefaults) {
153         return newBuilder(withDefaults).build();
154     }
155 
156     /**
157      * Creates a new {@code PatternSet} builder instance.
158      * Equivalent to {@code newBuilder(true)}.
159      * @see #newBuilder(boolean)
160      *
161      * @return a new {@code Builder}
162      */
163     @Nonnull
164     public static Builder newBuilder() {
165         return newBuilder(true);
166     }
167 
168     /**
169      * Creates a new {@code PatternSet} builder instance using default values or not.
170      *
171      * @param withDefaults the boolean indicating whether default values should be used
172      * @return a new {@code Builder}
173      */
174     @Nonnull
175     public static Builder newBuilder(boolean withDefaults) {
176         return new Builder(withDefaults);
177     }
178 
179     /**
180      * Creates a new {@code PatternSet} builder instance using the specified object as a basis.
181      * Equivalent to {@code newBuilder(from, false)}.
182      *
183      * @param from the {@code PatternSet} instance to use as a basis
184      * @return a new {@code Builder}
185      */
186     @Nonnull
187     public static Builder newBuilder(PatternSet from) {
188         return newBuilder(from, false);
189     }
190 
191     /**
192      * Creates a new {@code PatternSet} builder instance using the specified object as a basis.
193      *
194      * @param from the {@code PatternSet} instance to use as a basis
195      * @param forceCopy the boolean indicating if a copy should be forced
196      * @return a new {@code Builder}
197      */
198     @Nonnull
199     public static Builder newBuilder(PatternSet from, boolean forceCopy) {
200         return new Builder(from, forceCopy);
201     }
202 
203     /**
204      * Builder class used to create PatternSet instances.
205      * @see #with()
206      * @see #newBuilder()
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             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
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      * @see java.lang.Object#toString()
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 }