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.domain; 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.Arg; 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 this.groupId = Arg.checkNotBlank("groupId", groupId); 153 return this; 154 } 155 156 /** 157 * Sets the name of the artifact. 158 * 159 * @param name the name of the artifact. 160 * @return a reference to this builder instance. 161 * @throws IllegalArgumentException if {@code name} is blank. 162 */ 163 public Builder withName(final String name) throws IllegalArgumentException 164 { 165 this.name = Arg.checkNotBlank("name", name); 166 return this; 167 } 168 169 /** 170 * Sets the version of the artifact. 171 * 172 * @param version the version of the artifact. 173 * @return a reference to this builder instance. 174 * @throws IllegalArgumentException if {@code version} is blank. 175 */ 176 public Builder withVersion(final String version) 177 throws IllegalArgumentException 178 { 179 this.version = Arg.checkNotBlank("version", version); 180 return this; 181 } 182 183 /** 184 * Sets the artifact type that is expressed by the artifact's file name 185 * extension. 186 * 187 * @param archiveType the artifact type that is expressed by the artifact's 188 * file name extension. 189 * @return a reference to this builder instance. 190 * @throws IllegalArgumentException if {@code archiveType} is blank. 191 */ 192 public Builder withArchiveType(final String archiveType) 193 throws IllegalArgumentException 194 { 195 this.archiveType = Arg.checkNotBlank("archiveType", archiveType); 196 return this; 197 } 198 199 /** 200 * Sets the classifier to the artifact. 201 * 202 * @param classifier the classifier to the artifact. May be 203 * <code>null</code>. 204 * @return a reference to this builder instance. 205 * @throws IllegalArgumentException if classifier is blank except 206 * <code>null</code>. 207 */ 208 public Builder withClassifier(final String classifier) 209 throws IllegalArgumentException 210 { 211 this.classifier = Arg.checkNotBlankExceptNull("classifier", classifier); 212 return this; 213 } 214 215 // --- business ----------------------------------------------------------- 216 217 /** 218 * Creates an instance of {@link ArtifactId}. 219 * 220 * @return an instance. 221 */ 222 public ArtifactId build() 223 { 224 Arg.checkNotBlank("groupId", groupId); 225 Arg.checkNotBlank("name", name); 226 Arg.checkNotBlank("version", version); 227 Arg.checkNotBlank("archiveType", archiveType); 228 Arg.checkNotBlankExceptNull("classifier", classifier); 229 230 final ArtifactId instance = new ArtifactId(this); 231 return instance; 232 } 233 234 // --- object basics ------------------------------------------------------ 235 } 236 237 // ********************************* Methods ******************************** 238 239 // --- init ----------------------------------------------------------------- 240 241 // --- factory -------------------------------------------------------------- 242 243 /** 244 * Helper to create instances of {@link ArtifactId} instead of using the 245 * {@link ArtifactId.Builder}. 246 * 247 * @param groupId the group the artifact belongs to. 248 * @param name the identifier of the artifact. 249 * @param version the version of the artifact. 250 * @param archiveType the artifact type that is expressed by the artifact's 251 * file name extension. 252 * @param classifier the classifier to the artifact. 253 * @return the instance. 254 * @throws IllegalArgumentException if any of the arguments (except 255 * {@code classifier}) is blank or {@code classifier} is blank 256 * except being <code>null</code>. 257 */ 258 public static ArtifactId create(final String groupId, final String name, 259 final String version, final String archiveType, final String classifier) 260 throws IllegalArgumentException 261 { 262 final Builder builder = new Builder(); 263 builder.withGroupId(groupId).withName(name).withVersion(version) 264 .withArchiveType(archiveType).withClassifier(classifier); 265 return builder.build(); 266 } 267 268 // --- get&set -------------------------------------------------------------- 269 270 /** 271 * Returns the group the artifact belongs to. 272 * 273 * @return the group the artifact belongs to. 274 */ 275 public String getGroupId() 276 { 277 return groupId; 278 } 279 280 /** 281 * Returns the name of the artifact. 282 * 283 * @return the name of the artifact. 284 */ 285 public String getName() 286 { 287 return name; 288 } 289 290 /** 291 * Returns the version of the artifact. 292 * 293 * @return the version of the artifact. 294 */ 295 public String getVersion() 296 { 297 return version; 298 } 299 300 /** 301 * Returns the artifact type that is expressed by the artifact's file name 302 * extension. 303 * 304 * @return the artifact type that is expressed by the artifact's file name 305 * extension. 306 */ 307 public String getArchiveType() 308 { 309 return archiveType; 310 } 311 312 /** 313 * Returns the classifier to the artifact. May be <code>null</code>. 314 * 315 * @return the classifier to the artifact. 316 */ 317 public String getClassifier() 318 { 319 return classifier; 320 } 321 322 // --- business ------------------------------------------------------------- 323 324 // --- object basics -------------------------------------------------------- 325 326 /** 327 * Returns the hash code of the object. 328 * 329 * @return the hash code. 330 */ 331 @Override 332 public int hashCode() 333 { 334 int result = 17; 335 result = 37 * result + ObjectUtils.hashCode(name); 336 result = 37 * result + ObjectUtils.hashCode(groupId); 337 result = 37 * result + ObjectUtils.hashCode(version); 338 return result; 339 } 340 341 /** 342 * Returns <code>true</code> if the given object is semantically equal to the 343 * given object, <code>false</code> otherwise. 344 * 345 * @param object the instance to compare to. 346 * @return <code>true</code> if the given object is semantically equal to the 347 * given object, <code>false</code> otherwise. 348 */ 349 @Override 350 public boolean equals(final Object object) 351 { 352 if (this == object) 353 { 354 return true; 355 } 356 else if (object == null || getClass() != object.getClass()) 357 { 358 return false; 359 } 360 361 final ArtifactId other = (ArtifactId) object; 362 363 return (ObjectUtils.equals(name, other.name) 364 && ObjectUtils.equals(groupId, other.groupId) 365 && ObjectUtils.equals(version, other.version) 366 && ObjectUtils.equals(archiveType, other.archiveType) && ObjectUtils 367 .equals(classifier, other.classifier)); 368 } 369 370 /** 371 * {@inheritDoc} 372 * 373 * @see java.lang.Comparable#compareTo(java.lang.Object) 374 */ 375 @Override 376 public int compareTo(final ArtifactId o) 377 { 378 int result = ObjectUtils.compare(name, o.name); 379 if (result == 0) 380 { 381 result = ObjectUtils.compare(groupId, o.groupId); 382 if (result == 0) 383 { 384 result = ObjectUtils.compare(version, o.version); 385 if (result == 0) 386 { 387 result = ObjectUtils.compare(archiveType, o.archiveType); 388 // CHECKSTYLE:OFF 389 if (result == 0) // NOPMD 390 { 391 result = ObjectUtils.compare(classifier, o.classifier); 392 } 393 // CHECKSTYLE:ON 394 } 395 } 396 } 397 398 return result; 399 } 400 401 /** 402 * Returns the short string representation of the object. 403 * 404 * @return the short string representation of the object. 405 */ 406 public String toShortString() 407 { 408 final StringBuilder buffer = new StringBuilder(); 409 410 buffer.append(groupId); 411 buffer.append(':'); 412 buffer.append(name); 413 buffer.append(':'); 414 buffer.append(version); 415 416 return buffer.toString(); 417 } 418 419 /** 420 * Returns the string representation of the object. 421 * 422 * @return the string representation of the object. 423 */ 424 @Override 425 public String toString() 426 { 427 final StringBuilder buffer = new StringBuilder(); 428 429 buffer.append(groupId); 430 buffer.append(':'); 431 buffer.append(name); 432 buffer.append(':'); 433 buffer.append(version); 434 buffer.append(':'); 435 buffer.append(archiveType); 436 if (classifier != null) 437 { 438 buffer.append(':'); 439 buffer.append(classifier); 440 } 441 442 return buffer.toString(); 443 } 444 }