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.Collections;
9   import java.util.HashMap;
10  import java.util.Map;
11  import java.util.Objects;
12  import java.util.Optional;
13  import java.util.Set;
14  import java.util.stream.Collectors;
15  import java.util.stream.Stream;
16  import org.apache.maven.api.annotations.Experimental;
17  import org.apache.maven.api.annotations.Generated;
18  import org.apache.maven.api.annotations.Immutable;
19  import org.apache.maven.api.annotations.Nonnull;
20  import org.apache.maven.api.annotations.NotThreadSafe;
21  import org.apache.maven.api.annotations.ThreadSafe;
22  
23  /**
24   * Specifies the organization that produces this project.
25   */
26  @Experimental
27  @Generated @ThreadSafe @Immutable
28  public class Organization
29      implements Serializable, InputLocationTracker
30  {
31      /**
32       * The full name of the organization.
33       */
34      final String name;
35      /**
36       * The URL to the organization's home page.
37       */
38      final String url;
39      /** Locations */
40      final Map<Object, InputLocation> locations;
41      /** Location tracking */
42      final InputLocation importedFrom;
43  
44      /**
45        * Constructor for this class, to be called from its subclasses and {@link Builder}.
46        * @see Builder#build()
47        */
48      protected Organization(Builder builder) {
49          this.name = builder.name != null ? builder.name : (builder.base != null ? builder.base.name : null);
50          this.url = builder.url != null ? builder.url : (builder.base != null ? builder.base.url : null);
51          this.locations = builder.computeLocations();
52          this.importedFrom = builder.importedFrom;
53      }
54  
55      /**
56       * The full name of the organization.
57       *
58       * @return a {@code String}
59       */
60      public String getName() {
61          return this.name;
62      }
63  
64      /**
65       * The URL to the organization's home page.
66       *
67       * @return a {@code String}
68       */
69      public String getUrl() {
70          return this.url;
71      }
72  
73      /**
74       * Gets the location of the specified field in the input source.
75       *
76       * @param key the key of the field, must not be {@code null}
77       * @return the location of the field in the input source or {@code null} if unknown
78       * @throws NullPointerException if {@code key} is {@code null}
79       */
80      public InputLocation getLocation(Object key) {
81          Objects.requireNonNull(key, "key");
82          return locations.get(key);
83      }
84  
85      /**
86       * Gets the keys of the locations of the input source.
87       */
88      public Set<Object> getLocationKeys() {
89          return locations.keySet();
90      }
91  
92      protected Stream<Object> getLocationKeyStream() {
93          return locations.keySet().stream();
94      }
95  
96      /**
97       * Gets the input location that caused this model to be read.
98       */
99      public InputLocation getImportedFrom() {
100         return importedFrom;
101     }
102 
103     /**
104      * Creates a new builder with this object as the basis.
105      *
106      * @return a {@code Builder}
107      */
108     @Nonnull
109     public Builder with() {
110         return newBuilder(this);
111     }
112     /**
113      * Creates a new {@code Organization} instance using the specified name.
114      *
115      * @param name the new {@code String} to use
116      * @return a {@code Organization} with the specified name
117      */
118     @Nonnull
119     public Organization withName(String name) {
120         return newBuilder(this, true).name(name).build();
121     }
122     /**
123      * Creates a new {@code Organization} instance using the specified url.
124      *
125      * @param url the new {@code String} to use
126      * @return a {@code Organization} with the specified url
127      */
128     @Nonnull
129     public Organization withUrl(String url) {
130         return newBuilder(this, true).url(url).build();
131     }
132 
133     /**
134      * Creates a new {@code Organization} instance.
135      * Equivalent to {@code newInstance(true)}.
136      * @see #newInstance(boolean)
137      *
138      * @return a new {@code Organization}
139      */
140     @Nonnull
141     public static Organization newInstance() {
142         return newInstance(true);
143     }
144 
145     /**
146      * Creates a new {@code Organization} instance using default values or not.
147      * Equivalent to {@code newBuilder(withDefaults).build()}.
148      *
149      * @param withDefaults the boolean indicating whether default values should be used
150      * @return a new {@code Organization}
151      */
152     @Nonnull
153     public static Organization newInstance(boolean withDefaults) {
154         return newBuilder(withDefaults).build();
155     }
156 
157     /**
158      * Creates a new {@code Organization} builder instance.
159      * Equivalent to {@code newBuilder(true)}.
160      * @see #newBuilder(boolean)
161      *
162      * @return a new {@code Builder}
163      */
164     @Nonnull
165     public static Builder newBuilder() {
166         return newBuilder(true);
167     }
168 
169     /**
170      * Creates a new {@code Organization} builder instance using default values or not.
171      *
172      * @param withDefaults the boolean indicating whether default values should be used
173      * @return a new {@code Builder}
174      */
175     @Nonnull
176     public static Builder newBuilder(boolean withDefaults) {
177         return new Builder(withDefaults);
178     }
179 
180     /**
181      * Creates a new {@code Organization} builder instance using the specified object as a basis.
182      * Equivalent to {@code newBuilder(from, false)}.
183      *
184      * @param from the {@code Organization} instance to use as a basis
185      * @return a new {@code Builder}
186      */
187     @Nonnull
188     public static Builder newBuilder(Organization from) {
189         return newBuilder(from, false);
190     }
191 
192     /**
193      * Creates a new {@code Organization} builder instance using the specified object as a basis.
194      *
195      * @param from the {@code Organization} instance to use as a basis
196      * @param forceCopy the boolean indicating if a copy should be forced
197      * @return a new {@code Builder}
198      */
199     @Nonnull
200     public static Builder newBuilder(Organization from, boolean forceCopy) {
201         return new Builder(from, forceCopy);
202     }
203 
204     /**
205      * Builder class used to create Organization instances.
206      * @see #with()
207      * @see #newBuilder()
208      */
209     @NotThreadSafe
210     public static class Builder
211     {
212         Organization base;
213         String name;
214         String url;
215         Map<Object, InputLocation> locations;
216         InputLocation importedFrom;
217 
218         protected Builder(boolean withDefaults) {
219             if (withDefaults) {
220             }
221         }
222 
223         protected Builder(Organization base, boolean forceCopy) {
224             if (forceCopy) {
225                 this.name = base.name;
226                 this.url = base.url;
227                 this.locations = base.locations;
228                 this.importedFrom = base.importedFrom;
229             } else {
230                 this.base = base;
231             }
232         }
233 
234         @Nonnull
235         public Builder name(String name) {
236             this.name = name;
237             return this;
238         }
239 
240         @Nonnull
241         public Builder url(String url) {
242             this.url = url;
243             return this;
244         }
245 
246 
247         @Nonnull
248         public Builder location(Object key, InputLocation location) {
249             if (location != null) {
250                 if (!(this.locations instanceof HashMap)) {
251                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
252                 }
253                 this.locations.put(key, location);
254             }
255             return this;
256         }
257 
258         @Nonnull
259         public Builder importedFrom(InputLocation importedFrom) {
260             this.importedFrom = importedFrom;
261             return this;
262         }
263 
264         @Nonnull
265         public Organization build() {
266             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
267             if (base != null
268                     && (name == null || name == base.name)
269                     && (url == null || url == base.url)
270             ) {
271                 return base;
272             }
273             return new Organization(this);
274         }
275 
276         Map<Object, InputLocation> computeLocations() {
277             Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
278             Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
279             if (newlocs.isEmpty()) {
280                 return Map.copyOf(oldlocs);
281             }
282             if (oldlocs.isEmpty()) {
283                 return Map.copyOf(newlocs);
284             }
285             return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
286                     // Keep value from newlocs in case of duplicates
287                     .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
288         }
289     }
290 
291 
292             
293     /**
294      * @see java.lang.Object#toString()
295      */
296     public String toString()
297     {
298         return "Organization {name=" + getName() + ", url=" + getUrl() + "}";
299     }
300             
301           
302 }