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.StringUtils; 21 22 import de.smartics.util.lang.Arguments; 23 import de.smartics.util.lang.NullArgumentException; 24 25 /** 26 * The configuration key identifies a configuration. With this key all 27 * configuration values can be accessed by their local configuration keys. 28 */ 29 public final class ConfigurationKey implements Serializable, 30 Comparable<ConfigurationKey> 31 { 32 // ********************************* Fields ********************************* 33 34 // --- constants ------------------------------------------------------------ 35 36 /** 37 * The class version identifier. 38 * <p> 39 * The value of this constant is {@value}. 40 * </p> 41 */ 42 private static final long serialVersionUID = 1L; 43 44 // --- members -------------------------------------------------------------- 45 46 /** 47 * The identifier of the environment the application is deployed in. 48 * 49 * @serial 50 */ 51 private final EnvironmentId environmentId; 52 53 /** 54 * The identifier of the application whose configuration is requested. 55 * 56 * @serial 57 */ 58 private final ApplicationId applicationId; 59 60 /** 61 * The pre-calculated hash code of this instance. 62 * 63 * @serial 64 */ 65 private final int hashCodeValue; 66 67 // ****************************** Initializer ******************************* 68 69 // ****************************** Constructors ****************************** 70 71 /** 72 * Default constructor. 73 * 74 * @param applicationId the identifier of the application whose configuration 75 * is requested. 76 * @param environmentId the identifier of the environment the application is 77 * deployed in. 78 * @throws NullArgumentException if {@code applicationId} or 79 * {@code environmentId} is <code>null</code>. 80 */ 81 public ConfigurationKey(final EnvironmentId environmentId, final ApplicationId applicationId 82 ) throws NullArgumentException 83 { 84 Arguments.checkNotNull("environmentId", environmentId); 85 Arguments.checkNotNull("applicationId", applicationId); 86 87 this.environmentId = environmentId; 88 this.applicationId = applicationId; 89 this.hashCodeValue = calcHashCode(); 90 } 91 92 // ****************************** Inner Classes ***************************** 93 94 // ********************************* Methods ******************************** 95 96 // --- init ----------------------------------------------------------------- 97 98 private int calcHashCode() 99 { 100 int result = 17; 101 result = 37 * result + environmentId.hashCode(); 102 result = 37 * result + applicationId.hashCode(); 103 104 return result; 105 } 106 107 // --- get&set -------------------------------------------------------------- 108 109 /** 110 * Returns the identifier of the environment the application is deployed in. 111 * 112 * @return the identifier of the environment the application is deployed in. 113 */ 114 public EnvironmentId getEnvironmentId() 115 { 116 return environmentId; 117 } 118 119 /** 120 * Returns the identifier of the application whose configuration is requested. 121 * 122 * @return the identifier of the application whose configuration is requested. 123 */ 124 public ApplicationId getApplicationId() 125 { 126 return applicationId; 127 } 128 129 // --- business ------------------------------------------------------------- 130 131 // --- object basics -------------------------------------------------------- 132 133 /** 134 * Returns the hash code of the object. 135 * 136 * @return the hash code. 137 */ 138 @Override 139 public int hashCode() 140 { 141 return hashCodeValue; 142 } 143 144 /** 145 * Returns <code>true</code> if the given object is semantically equal to the 146 * given object, <code>false</code> otherwise. 147 * 148 * @param object the instance to compare to. 149 * @return <code>true</code> if the given object is semantically equal to the 150 * given object, <code>false</code> otherwise. 151 */ 152 @Override 153 public boolean equals(final Object object) 154 { 155 if (this == object) 156 { 157 return true; 158 } 159 else if (object == null || getClass() != object.getClass()) 160 { 161 return false; 162 } 163 164 final ConfigurationKey other = (ConfigurationKey) object; 165 166 return (applicationId.equals(other.applicationId) && environmentId 167 .equals(other.environmentId)); 168 } 169 170 /** 171 * {@inheritDoc} 172 * 173 * @see java.lang.Comparable#compareTo(java.lang.Object) 174 */ 175 @Override 176 public int compareTo(final ConfigurationKey o) 177 { 178 final int result = environmentId.compareTo(o.environmentId); 179 if (result == 0) 180 { 181 return applicationId.compareTo(o.applicationId); 182 } 183 return result; 184 } 185 186 /** 187 * Returns the string representation of the object. 188 * 189 * @return the string representation of the object. 190 */ 191 @Override 192 public String toString() 193 { 194 final StringBuilder buffer = new StringBuilder(); 195 196 final String environmentIdString = environmentId.toString(); 197 if(StringUtils.isNotBlank(environmentIdString)) 198 { 199 buffer.append(environmentIdString).append('/'); 200 } 201 202 final String applicationIdString = applicationId.toString(); 203 if(StringUtils.isNotBlank(applicationIdString)) 204 { 205 buffer.append(applicationIdString); 206 } 207 208 if(buffer.length() == 0) 209 { 210 buffer.append("<no named key>"); 211 } 212 213 return buffer.toString(); 214 } 215 }