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      /** Location of the xml element for this object. */
38      final InputLocation location;
39      /** Location of the xml element for the field includes. */
40      final InputLocation includesLocation;
41      /** Location of the xml element for the field excludes. */
42      final InputLocation excludesLocation;
43      /** Other locations */
44      final Map<Object, InputLocation> locations;
45  
46      /**
47        * Constructor for this class, package protected.
48        * @see Builder#build()
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       * A list of patterns to include, e.g. {@code **&#47;*.xml}.
69       *
70       * @return a {@code List<String>}
71       */
72      @Nonnull
73      public List<String> getIncludes()
74      {
75          return this.includes;
76      }
77  
78      /**
79       * A list of patterns to exclude, e.g. {@code **&#47;*.xml}
80       *
81       * @return a {@code List<String>}
82       */
83      @Nonnull
84      public List<String> getExcludes()
85      {
86          return this.excludes;
87      }
88  
89      /**
90       * Gets the location of the specified field in the input source.
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      * Creates a new builder with this object as the basis.
111      *
112      * @return a {@code Builder}
113      */
114     @Nonnull
115     public Builder with()
116     {
117         return newBuilder( this );
118     }
119     /**
120      * Creates a new {@code PatternSet} instance using the specified includes.
121      *
122      * @param includes the new {@code Collection<String>} to use
123      * @return a {@code PatternSet} with the specified includes
124      */
125     @Nonnull
126     public PatternSet withIncludes( Collection<String> includes )
127     {
128         return with().includes( includes ).build();
129     }
130     /**
131      * Creates a new {@code PatternSet} instance using the specified excludes.
132      *
133      * @param excludes the new {@code Collection<String>} to use
134      * @return a {@code PatternSet} with the specified excludes
135      */
136     @Nonnull
137     public PatternSet withExcludes( Collection<String> excludes )
138     {
139         return with().excludes( excludes ).build();
140     }
141 
142     /**
143      * Creates a new {@code PatternSet} instance.
144      * Equivalent to {@code newInstance( true )}.
145      * @see #newInstance(boolean)
146      *
147      * @return a new {@code PatternSet}
148      */
149     @Nonnull
150     public static PatternSet newInstance()
151     {
152         return newInstance( true );
153     }
154 
155     /**
156      * Creates a new {@code PatternSet} instance using default values or not.
157      * Equivalent to {@code newBuilder( withDefaults ).build()}.
158      *
159      * @param withDefaults the boolean indicating whether default values should be used
160      * @return a new {@code PatternSet}
161      */
162     @Nonnull
163     public static PatternSet newInstance( boolean withDefaults )
164     {
165         return newBuilder( withDefaults ).build();
166     }
167 
168     /**
169      * Creates a new {@code PatternSet} builder instance.
170      * Equivalent to {@code newBuilder( true )}.
171      * @see #newBuilder(boolean)
172      *
173      * @return a new {@code Builder}
174      */
175     @Nonnull
176     public static Builder newBuilder()
177     {
178         return newBuilder( true );
179     }
180 
181     /**
182      * Creates a new {@code PatternSet} builder instance using default values or not.
183      *
184      * @param withDefaults the boolean indicating whether default values should be used
185      * @return a new {@code Builder}
186      */
187     @Nonnull
188     public static Builder newBuilder( boolean withDefaults )
189     {
190         return new Builder( withDefaults );
191     }
192 
193     /**
194      * Creates a new {@code PatternSet} builder instance using the specified object as a basis.
195      * Equivalent to {@code newBuilder( from, false )}.
196      *
197      * @param from the {@code PatternSet} instance to use as a basis
198      * @return a new {@code Builder}
199      */
200     @Nonnull
201     public static Builder newBuilder( PatternSet from )
202     {
203         return newBuilder( from, false );
204     }
205 
206     /**
207      * Creates a new {@code PatternSet} builder instance using the specified object as a basis.
208      *
209      * @param from the {@code PatternSet} instance to use as a basis
210      * @param forceCopy the boolean indicating if a copy should be forced
211      * @return a new {@code Builder}
212      */
213     @Nonnull
214     public static Builder newBuilder( PatternSet from, boolean forceCopy )
215     {
216         return new Builder( from, forceCopy );
217     }
218 
219     /**
220      * Builder class used to create PatternSet instances.
221      * @see #with()
222      * @see #newBuilder()
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      * @see java.lang.Object#toString()
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 }