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