View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.resource.repository;
17  
18  import java.io.Serializable;
19  
20  import javax.annotation.concurrent.ThreadSafe;
21  
22  import org.apache.commons.lang.ObjectUtils;
23  
24  import de.smartics.util.lang.Arguments;
25  
26  /**
27   * Identifies an artifact of resources.
28   */
29  @ThreadSafe
30  public final class ArtifactId implements Serializable, Comparable<ArtifactId>
31  { // NOPMD
32    // ********************************* Fields *********************************
33  
34    // --- constants ------------------------------------------------------------
35  
36    /**
37     * The class version identifier.
38     */
39    private static final long serialVersionUID = 1L;
40  
41    // --- members --------------------------------------------------------------
42  
43    /**
44     * The group the artifact belongs to.
45     *
46     * @serial
47     */
48    private final String groupId;
49  
50    /**
51     * The name of the artifact.
52     *
53     * @serial
54     */
55    private final String name;
56  
57    /**
58     * The version of the artifact.
59     *
60     * @serial
61     */
62    private final String version;
63  
64    /**
65     * The artifact type that is expressed by the artifact's file name extension.
66     *
67     * @serial
68     */
69    private final String archiveType;
70  
71    /**
72     * The classifier to the artifact. May be <code>null</code>.
73     *
74     * @serial
75     */
76    private final String classifier;
77  
78    // ****************************** Initializer *******************************
79  
80    // ****************************** Constructors ******************************
81  
82    private ArtifactId(final Builder builder)
83    {
84      this.groupId = builder.groupId;
85      this.name = builder.name;
86      this.version = builder.version;
87      this.archiveType = builder.archiveType;
88      this.classifier = builder.classifier;
89    }
90  
91    // ****************************** Inner Classes *****************************
92  
93    /**
94     * Builder to create instances of {@link ArtifactId}.
95     */
96    public static final class Builder
97    {
98      // ******************************** Fields ********************************
99  
100     // --- constants ----------------------------------------------------------
101 
102     // --- members ------------------------------------------------------------
103 
104     /**
105      * The group the artifact belongs to.
106      */
107     private String groupId;
108 
109     /**
110      * The name of the artifact.
111      */
112     private String name;
113 
114     /**
115      * The version of the artifact.
116      */
117     private String version;
118 
119     /**
120      * The artifact type that is expressed by the artifact's file name
121      * extension.
122      */
123     private String archiveType;
124 
125     /**
126      * The classifier to the artifact. May be <code>null</code>.
127      */
128     private String classifier;
129 
130     // ***************************** Initializer ******************************
131 
132     // ***************************** Constructors *****************************
133 
134     // ***************************** Inner Classes ****************************
135 
136     // ******************************** Methods *******************************
137 
138     // --- init ---------------------------------------------------------------
139 
140     // --- get&set ------------------------------------------------------------
141 
142     /**
143      * Sets the group the artifact belongs to.
144      *
145      * @param groupId the group the artifact belongs to.
146      * @return a reference to this builder instance.
147      * @throws IllegalArgumentException if {@code groupId} is blank.
148      */
149     public Builder withGroupId(final String groupId)
150       throws IllegalArgumentException
151     {
152       Arguments.checkNotBlank("groupId", groupId);
153       this.groupId = groupId;
154       return this;
155     }
156 
157     /**
158      * Sets the name of the artifact.
159      *
160      * @param name the name of the artifact.
161      * @return a reference to this builder instance.
162      * @throws IllegalArgumentException if {@code name} is blank.
163      */
164     public Builder withName(final String name) throws IllegalArgumentException
165     {
166       Arguments.checkNotBlank("name", name);
167       this.name = name;
168       return this;
169     }
170 
171     /**
172      * Sets the version of the artifact.
173      *
174      * @param version the version of the artifact.
175      * @return a reference to this builder instance.
176      * @throws IllegalArgumentException if {@code version} is blank.
177      */
178     public Builder withVersion(final String version)
179       throws IllegalArgumentException
180     {
181       Arguments.checkNotBlank("version", version);
182       this.version = version;
183       return this;
184     }
185 
186     /**
187      * Sets the artifact type that is expressed by the artifact's file name
188      * extension.
189      *
190      * @param archiveType the artifact type that is expressed by the artifact's
191      *          file name extension.
192      * @return a reference to this builder instance.
193      * @throws IllegalArgumentException if {@code archiveType} is blank.
194      */
195     public Builder withArchiveType(final String archiveType)
196       throws IllegalArgumentException
197     {
198       Arguments.checkNotBlank("archiveType", archiveType);
199       this.archiveType = archiveType;
200       return this;
201     }
202 
203     /**
204      * Sets the classifier to the artifact.
205      *
206      * @param classifier the classifier to the artifact. May be
207      *          <code>null</code>.
208      * @return a reference to this builder instance.
209      * @throws IllegalArgumentException if classifier is blank except
210      *           <code>null</code>.
211      */
212     public Builder withClassifier(final String classifier)
213       throws IllegalArgumentException
214     {
215       Arguments.checkNotBlankExceptNull("classifier", classifier);
216       this.classifier = classifier;
217       return this;
218     }
219 
220     // --- business -----------------------------------------------------------
221 
222     /**
223      * Creates an instance of {@link ArtifactId}.
224      *
225      * @return an instance.
226      */
227     public ArtifactId build()
228     {
229       Arguments.checkNotBlank("groupId", groupId);
230       Arguments.checkNotBlank("name", name);
231       Arguments.checkNotBlank("version", version);
232       Arguments.checkNotBlank("archiveType", archiveType);
233       Arguments.checkNotBlankExceptNull("classifier", classifier);
234 
235       final ArtifactId instance = new ArtifactId(this);
236       return instance;
237     }
238 
239     // --- object basics ------------------------------------------------------
240   }
241 
242   // ********************************* Methods ********************************
243 
244   // --- init -----------------------------------------------------------------
245 
246   // --- factory --------------------------------------------------------------
247 
248   /**
249    * Helper to create instances of {@link ArtifactId} instead of using the
250    * {@link ArtifactId.Builder}.
251    *
252    * @param groupId the group the artifact belongs to.
253    * @param name the identifier of the artifact.
254    * @param version the version of the artifact.
255    * @param archiveType the artifact type that is expressed by the artifact's
256    *          file name extension.
257    * @param classifier the classifier to the artifact.
258    * @return the instance.
259    * @throws IllegalArgumentException if any of the arguments (except
260    *           {@code classifier}) is blank or {@code classifier} is blank
261    *           except being <code>null</code>.
262    */
263   public static ArtifactId create(final String groupId, final String name,
264       final String version, final String archiveType, final String classifier)
265     throws IllegalArgumentException
266   {
267     final Builder builder = new Builder();
268     builder.withGroupId(groupId).withName(name).withVersion(version)
269         .withArchiveType(archiveType).withClassifier(classifier);
270     return builder.build();
271   }
272 
273   // --- get&set --------------------------------------------------------------
274 
275   /**
276    * Returns the group the artifact belongs to.
277    *
278    * @return the group the artifact belongs to.
279    */
280   public String getGroupId()
281   {
282     return groupId;
283   }
284 
285   /**
286    * Returns the name of the artifact.
287    *
288    * @return the name of the artifact.
289    */
290   public String getName()
291   {
292     return name;
293   }
294 
295   /**
296    * Returns the version of the artifact.
297    *
298    * @return the version of the artifact.
299    */
300   public String getVersion()
301   {
302     return version;
303   }
304 
305   /**
306    * Returns the artifact type that is expressed by the artifact's file name
307    * extension.
308    *
309    * @return the artifact type that is expressed by the artifact's file name
310    *         extension.
311    */
312   public String getArchiveType()
313   {
314     return archiveType;
315   }
316 
317   /**
318    * Returns the classifier to the artifact. May be <code>null</code>.
319    *
320    * @return the classifier to the artifact.
321    */
322   public String getClassifier()
323   {
324     return classifier;
325   }
326 
327   // --- business -------------------------------------------------------------
328 
329   // --- object basics --------------------------------------------------------
330 
331   /**
332    * Returns the hash code of the object.
333    *
334    * @return the hash code.
335    */
336   @Override
337   public int hashCode()
338   {
339     int result = 17;
340     result = 37 * result + ObjectUtils.hashCode(name);
341     result = 37 * result + ObjectUtils.hashCode(groupId);
342     result = 37 * result + ObjectUtils.hashCode(version);
343     return result;
344   }
345 
346   /**
347    * Returns <code>true</code> if the given object is semantically equal to the
348    * given object, <code>false</code> otherwise.
349    *
350    * @param object the instance to compare to.
351    * @return <code>true</code> if the given object is semantically equal to the
352    *         given object, <code>false</code> otherwise.
353    */
354   @Override
355   public boolean equals(final Object object)
356   {
357     if (this == object)
358     {
359       return true;
360     }
361     else if (object == null || getClass() != object.getClass())
362     {
363       return false;
364     }
365 
366     final ArtifactId other = (ArtifactId) object;
367 
368     return (ObjectUtils.equals(name, other.name)
369             && ObjectUtils.equals(groupId, other.groupId)
370             && ObjectUtils.equals(version, other.version)
371             && ObjectUtils.equals(archiveType, other.archiveType) && ObjectUtils
372         .equals(classifier, other.classifier));
373   }
374 
375   /**
376    * {@inheritDoc}
377    *
378    * @see java.lang.Comparable#compareTo(java.lang.Object)
379    */
380   @Override
381   public int compareTo(final ArtifactId o)
382   {
383     int result = ObjectUtils.compare(name, o.name);
384     if (result == 0)
385     {
386       result = ObjectUtils.compare(groupId, o.groupId);
387       if (result == 0)
388       {
389         result = ObjectUtils.compare(version, o.version);
390         if (result == 0)
391         {
392           result = ObjectUtils.compare(archiveType, o.archiveType);
393           // CHECKSTYLE:OFF
394           if (result == 0) // NOPMD
395           {
396             result = ObjectUtils.compare(classifier, o.classifier);
397           }
398           // CHECKSTYLE:ON
399         }
400       }
401     }
402 
403     return result;
404   }
405 
406   /**
407    * Returns the short string representation of the object.
408    *
409    * @return the short string representation of the object.
410    */
411   public String toShortString()
412   {
413     final StringBuilder buffer = new StringBuilder();
414 
415     buffer.append(groupId);
416     buffer.append(':');
417     buffer.append(name);
418     buffer.append(':');
419     buffer.append(version);
420 
421     return buffer.toString();
422   }
423 
424   /**
425    * Returns the string representation of the object.
426    *
427    * @return the string representation of the object.
428    */
429   @Override
430   public String toString()
431   {
432     final StringBuilder buffer = new StringBuilder();
433 
434     buffer.append(groupId);
435     buffer.append(':');
436     buffer.append(name);
437     buffer.append(':');
438     buffer.append(version);
439     buffer.append(':');
440     buffer.append(archiveType);
441     if (classifier != null)
442     {
443       buffer.append(':');
444       buffer.append(classifier);
445     }
446 
447     return buffer.toString();
448   }
449 }