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.api.config.domain.key;
17  
18  import java.io.Serializable;
19  
20  import static org.apache.commons.lang.StringUtils.defaultIfBlank;
21  import org.apache.commons.lang.ObjectUtils;
22  
23  import de.smartics.util.lang.Arg;
24  
25  /**
26   * The coordinates to identify an application.
27   */
28  public final class ApplicationId implements Serializable,
29      Comparable<ApplicationId>
30  {
31    // ********************************* Fields *********************************
32  
33    // --- constants ------------------------------------------------------------
34  
35    /**
36     * The class version identifier.
37     * <p>
38     * The value of this constant is {@value}.
39     * </p>
40     */
41    private static final long serialVersionUID = 1L;
42  
43    /**
44     * Defines the application for any group, name and version.
45     */
46    public static final ApplicationId ANY_APP = new ApplicationId(null, null,
47        null);
48  
49    // --- members --------------------------------------------------------------
50  
51    /**
52     * The group the application belongs to.
53     *
54     * @serial
55     */
56    private final String groupId;
57  
58    /**
59     * The name of the application.
60     *
61     * @serial
62     */
63    private final String artifactId;
64  
65    /**
66     * The version of the application.
67     *
68     * @serial
69     */
70    private final String version;
71  
72    // ****************************** Initializer *******************************
73  
74    // ****************************** Constructors ******************************
75  
76    /**
77     * Default constructor.
78     *
79     * @param groupId the group the application belongs to. May be
80     *          <code>null</code>
81     * @param artifactId the name of the application. May be <code>null</code>.
82     * @param version the version of the application. May be <code>null</code>.
83     * @throws IllegalArgumentException if either parameter is the empty string or
84     *           contains only white spaces.
85     */
86    public ApplicationId(final String groupId, final String artifactId,
87        final String version) throws IllegalArgumentException
88    {
89      this.groupId = Arg.checkNotBlankExceptNull("groupId", groupId);
90      this.artifactId = Arg.checkNotBlankExceptNull("artifactId", artifactId);
91      this.version = Arg.checkNotBlankExceptNull("version", version);
92    }
93  
94    // ****************************** Inner Classes *****************************
95  
96    // ********************************* Methods ********************************
97  
98    // --- init -----------------------------------------------------------------
99  
100   // --- get&set --------------------------------------------------------------
101 
102   /**
103    * Returns the group the application belongs to.
104    *
105    * @return the group the application belongs to. May be <code>null</code>.
106    */
107   public String getGroupId()
108   {
109     return groupId;
110   }
111 
112   /**
113    * Returns the name of the application.
114    *
115    * @return the name of the application. May be <code>null</code>.
116    */
117   public String getArtifactId()
118   {
119     return artifactId;
120   }
121 
122   /**
123    * Returns the version of the application.
124    *
125    * @return the version of the application. May be <code>null</code>.
126    */
127   public String getVersion()
128   {
129     return version;
130   }
131 
132   // --- business -------------------------------------------------------------
133 
134   // --- object basics --------------------------------------------------------
135 
136   /**
137    * Returns the hash code of the object.
138    *
139    * @return the hash code.
140    */
141   @Override
142   public int hashCode()
143   {
144     int result = 17;
145     result = 37 * result + ObjectUtils.hashCode(artifactId);
146     result = 37 * result + ObjectUtils.hashCode(groupId);
147     result = 37 * result + ObjectUtils.hashCode(version);
148     return result;
149   }
150 
151   /**
152    * Returns <code>true</code> if the given object is semantically equal to the
153    * given object, <code>false</code> otherwise.
154    *
155    * @param object the instance to compare to.
156    * @return <code>true</code> if the given object is semantically equal to the
157    *         given object, <code>false</code> otherwise.
158    */
159   @Override
160   public boolean equals(final Object object)
161   {
162     if (this == object)
163     {
164       return true;
165     }
166     else if (object == null || getClass() != object.getClass())
167     {
168       return false;
169     }
170 
171     final ApplicationId other = (ApplicationId) object;
172 
173     return (ObjectUtils.equals(artifactId, other.artifactId)
174             && ObjectUtils.equals(groupId, other.groupId) && ObjectUtils
175         .equals(version, other.version));
176   }
177 
178   /**
179    * {@inheritDoc}
180    *
181    * @see java.lang.Comparable#compareTo(java.lang.Object)
182    */
183   @Override
184   public int compareTo(final ApplicationId o)
185   {
186     int result = ObjectUtils.compare(artifactId, o.artifactId);
187     if (result == 0)
188     {
189       result = ObjectUtils.compare(groupId, o.groupId);
190       if (result == 0)
191       {
192         result = ObjectUtils.compare(version, o.version);
193       }
194     }
195     return result;
196   }
197 
198   /**
199    * Returns the string representation of the object as a path. All elements are
200    * separated by a slash.
201    *
202    * @return the string representation of the object.
203    */
204   public String toPath()
205   {
206     final StringBuilder buffer = new StringBuilder();
207 
208     if (groupId != null)
209     {
210       buffer.append(groupId);
211     }
212 
213     if (artifactId != null)
214     {
215       buffer.append('/');
216       buffer.append(artifactId);
217     }
218 
219     if (version != null)
220     {
221       buffer.append('/');
222       buffer.append(version);
223     }
224 
225     return buffer.toString();
226   }
227 
228   /**
229    * Returns the string representation of the object. The GAV elements are
230    * separated by a colon. If any element is <code>null</code>, the empty string
231    * is appended. If no element is specified, the returned string is
232    * <code>::</code>.
233    *
234    * @return the string representation of the object.
235    */
236   @Override
237   public String toString()
238   {
239     final StringBuilder buffer = new StringBuilder();
240 
241     if (groupId != null)
242     {
243       buffer.append(groupId);
244     }
245     buffer.append(':');
246 
247     if (artifactId != null)
248     {
249       buffer.append(artifactId);
250     }
251     buffer.append(':');
252 
253     if (version != null)
254     {
255       buffer.append(version);
256     }
257 
258     return buffer.toString();
259   }
260 
261   /**
262    * Returns an {@link ApplicationId} for the given String or throws an
263    * {@link IllegalArgumentException} if the String is not valid.
264    *
265    * @param applicationString a string describing an applicationId.
266    * @return an applicationId for the given String.
267    * @throws IllegalArgumentException if an invalid applicationString has been
268    *           passed.
269    */
270   public static ApplicationId valueOf(final String applicationString)
271     throws IllegalArgumentException
272   {
273     if (applicationString == null)
274     {
275       return ApplicationId.ANY_APP;
276     }
277     final String[] applicationParts = applicationString.split(":", -1);
278     final int length = applicationParts.length;
279 
280     if (length < 3)
281     {
282       throw new IllegalArgumentException(
283           String
284               .format(
285                   "Could not create an instance out of the key %s. It has too less delimiters!",
286                   applicationString));
287     }
288     else if (length == 3)
289     {
290       return new ApplicationId(defaultIfBlank(applicationParts[0], null),
291           defaultIfBlank(applicationParts[1], null), defaultIfBlank(
292               applicationParts[2], null));
293     }
294     else
295     {
296       throw new IllegalArgumentException(
297           String
298               .format(
299                   "Could not create an instance out of the key %s. It has too many delimiters!",
300                   applicationString));
301     }
302   }
303 
304 }