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 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   * Definition of include or exclude patterns.
23   */
24  @Experimental
25  @Generated @ThreadSafe @Immutable
26  public class PatternSet
27      implements Serializable, InputLocationTracker
28  {
29      /**
30       * A list of patterns to include, e.g. {@code **/*.xml}.
31       */
32      final List<String> includes;
33      /**
34       * A list of patterns to exclude, e.g. {@code **&#47;*.xml}
35       */
36      final List<String> excludes;
37      /** Locations */
38      final Map<Object, InputLocation> locations;
39  
40      /**
41        * Constructor for this class, package protected.
42        * @see Builder#build()
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       * A list of patterns to include, e.g. {@code **&#47;*.xml}.
56       *
57       * @return a {@code List<String>}
58       */
59      @Nonnull
60      public List<String> getIncludes() {
61          return this.includes;
62      }
63  
64      /**
65       * A list of patterns to exclude, e.g. {@code **&#47;*.xml}
66       *
67       * @return a {@code List<String>}
68       */
69      @Nonnull
70      public List<String> getExcludes() {
71          return this.excludes;
72      }
73  
74      /**
75       * Gets the location of the specified field in the input source.
76       */
77      public InputLocation getLocation(Object key) {
78          return locations != null ? locations.get(key) : null;
79      }
80  
81      /**
82       * Creates a new builder with this object as the basis.
83       *
84       * @return a {@code Builder}
85       */
86      @Nonnull
87      public Builder with() {
88          return newBuilder(this);
89      }
90      /**
91       * Creates a new {@code PatternSet} instance using the specified includes.
92       *
93       * @param includes the new {@code Collection<String>} to use
94       * @return a {@code PatternSet} with the specified includes
95       */
96      @Nonnull
97      public PatternSet withIncludes(Collection<String> includes) {
98          return newBuilder(this, true).includes(includes).build();
99      }
100     /**
101      * Creates a new {@code PatternSet} instance using the specified excludes.
102      *
103      * @param excludes the new {@code Collection<String>} to use
104      * @return a {@code PatternSet} with the specified excludes
105      */
106     @Nonnull
107     public PatternSet withExcludes(Collection<String> excludes) {
108         return newBuilder(this, true).excludes(excludes).build();
109     }
110 
111     /**
112      * Creates a new {@code PatternSet} instance.
113      * Equivalent to {@code newInstance(true)}.
114      * @see #newInstance(boolean)
115      *
116      * @return a new {@code PatternSet}
117      */
118     @Nonnull
119     public static PatternSet newInstance() {
120         return newInstance(true);
121     }
122 
123     /**
124      * Creates a new {@code PatternSet} instance using default values or not.
125      * Equivalent to {@code newBuilder(withDefaults).build()}.
126      *
127      * @param withDefaults the boolean indicating whether default values should be used
128      * @return a new {@code PatternSet}
129      */
130     @Nonnull
131     public static PatternSet newInstance(boolean withDefaults) {
132         return newBuilder(withDefaults).build();
133     }
134 
135     /**
136      * Creates a new {@code PatternSet} builder instance.
137      * Equivalent to {@code newBuilder(true)}.
138      * @see #newBuilder(boolean)
139      *
140      * @return a new {@code Builder}
141      */
142     @Nonnull
143     public static Builder newBuilder() {
144         return newBuilder(true);
145     }
146 
147     /**
148      * Creates a new {@code PatternSet} builder instance using default values or not.
149      *
150      * @param withDefaults the boolean indicating whether default values should be used
151      * @return a new {@code Builder}
152      */
153     @Nonnull
154     public static Builder newBuilder(boolean withDefaults) {
155         return new Builder(withDefaults);
156     }
157 
158     /**
159      * Creates a new {@code PatternSet} builder instance using the specified object as a basis.
160      * Equivalent to {@code newBuilder(from, false)}.
161      *
162      * @param from the {@code PatternSet} instance to use as a basis
163      * @return a new {@code Builder}
164      */
165     @Nonnull
166     public static Builder newBuilder(PatternSet from) {
167         return newBuilder(from, false);
168     }
169 
170     /**
171      * Creates a new {@code PatternSet} builder instance using the specified object as a basis.
172      *
173      * @param from the {@code PatternSet} instance to use as a basis
174      * @param forceCopy the boolean indicating if a copy should be forced
175      * @return a new {@code Builder}
176      */
177     @Nonnull
178     public static Builder newBuilder(PatternSet from, boolean forceCopy) {
179         return new Builder(from, forceCopy);
180     }
181 
182     /**
183      * Builder class used to create PatternSet instances.
184      * @see #with()
185      * @see #newBuilder()
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      * @see java.lang.Object#toString()
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 }