1 56 package org.objectstyle.cayenne; 57 58 import java.io.Serializable ; 59 import java.util.Arrays ; 60 import java.util.Collections ; 61 import java.util.HashMap ; 62 import java.util.Iterator ; 63 import java.util.Map ; 64 65 import org.apache.commons.lang.builder.EqualsBuilder; 66 import org.apache.commons.lang.builder.HashCodeBuilder; 67 68 79 public class ObjectId implements Serializable { 80 81 protected Map objectIdKeys; 84 protected Class objectClass; 85 86 89 protected Map replacementIdMap; 90 91 94 int hashCode = Integer.MIN_VALUE; 96 97 100 public ObjectId(Class objectClass, String keyName, int id) { 101 this(objectClass, keyName, new Integer (id)); 102 } 103 104 107 public ObjectId(Class objectClass, String keyName, Object id) { 108 this.objectClass = objectClass; 109 this.setIdKeys(Collections.singletonMap(keyName, id)); 110 } 111 112 115 public ObjectId(Class objectClass, Map idKeys) { 116 this.objectClass = objectClass; 117 if (idKeys != null) { 118 this.setIdKeys(Collections.unmodifiableMap(idKeys)); 119 } 120 else { 121 this.setIdKeys(Collections.EMPTY_MAP); 122 } 123 } 124 125 protected void setIdKeys(Map idKeys) { 126 this.objectIdKeys = idKeys; 127 } 128 129 public boolean equals(Object object) { 130 if (!(object instanceof ObjectId)) { 131 return false; 132 } 133 134 if (this == object) { 135 return true; 136 } 137 138 ObjectId id = (ObjectId) object; 139 140 if (!objectClass.getName().equals(id.objectClass.getName())) { 143 return false; 144 } 145 146 if (id.objectIdKeys == null && objectIdKeys == null) { 147 return true; 148 } 149 150 if (id.objectIdKeys == null || objectIdKeys == null) { 151 return false; 152 } 153 154 if (id.objectIdKeys.size() != objectIdKeys.size()) { 155 return false; 156 } 157 158 EqualsBuilder builder = new EqualsBuilder(); 159 Iterator entries = objectIdKeys.entrySet().iterator(); 160 while (entries.hasNext()) { 161 Map.Entry entry = (Map.Entry ) entries.next(); 162 163 Object key = entry.getKey(); 164 Object value = entry.getValue(); 165 if (value == null) { 166 if (id.objectIdKeys.get(key) != null || !id.objectIdKeys.containsKey(key)) { 167 return false; 168 } 169 } 170 else { 171 builder.append(value, id.objectIdKeys.get(key)); 173 if (!builder.isEquals()) { 174 return false; 175 } 176 } 177 } 178 179 return true; 180 } 181 182 186 public Map getIdSnapshot() { 187 return objectIdKeys; 188 } 189 190 193 public Object getValueForAttribute(String attrName) { 194 return getIdSnapshot().get(attrName); 195 } 196 197 200 public boolean isTemporary() { 201 return false; 202 } 203 204 public String toString() { 205 StringBuffer buf = new StringBuffer (objectClass.getName()); 206 if (isTemporary()) 207 buf.append(" (temp)"); 208 buf.append(": "); 209 if (objectIdKeys != null) { 210 Iterator it = objectIdKeys.keySet().iterator(); 211 while (it.hasNext()) { 212 String nextKey = (String ) it.next(); 213 Object value = objectIdKeys.get(nextKey); 214 buf.append(" <").append(nextKey).append(": ").append(value).append('>'); 215 } 216 } 217 return buf.toString(); 218 } 219 220 223 public int hashCode() { 224 if (this.hashCode == Integer.MIN_VALUE) { 225 227 HashCodeBuilder builder = new HashCodeBuilder(3, 5); 228 229 builder.append(objectClass.getName().hashCode()); 232 233 if (objectIdKeys != null) { 234 int len = objectIdKeys.size(); 235 236 if (len == 1) { 238 Iterator entries = objectIdKeys.entrySet().iterator(); 239 Map.Entry entry = (Map.Entry ) entries.next(); 240 builder.append(entry.getKey()).append(entry.getValue()); 241 } 242 else { 244 Object [] keys = objectIdKeys.keySet().toArray(); 245 Arrays.sort(keys); 246 247 for (int i = 0; i < len; i++) { 248 251 builder.append(i).append(objectIdKeys.get(keys[i])); 254 } 255 } 256 } 257 258 this.hashCode = builder.toHashCode(); 259 } 260 261 return this.hashCode; 262 } 263 264 269 public Class getObjectClass() { 270 return objectClass; 271 } 272 273 276 public Class getObjClass() { 277 return getObjectClass(); 278 } 279 280 287 public ObjectId getReplacementId() { 288 return (isReplacementIdAttached()) ? createReplacementId() : null; 289 } 290 291 296 public void setReplacementId(ObjectId replacementId) { 297 if(replacementId == null) { 298 replacementIdMap = null; 299 } 300 else { 301 Map map = getReplacementIdMap(); 302 map.clear(); 303 map.putAll(replacementId.getIdSnapshot()); 304 } 305 } 306 307 313 public Map getReplacementIdMap() { 314 if (replacementIdMap == null) { 315 replacementIdMap = new HashMap (); 316 } 317 318 return replacementIdMap; 319 } 320 321 326 public ObjectId createReplacementId() { 327 return new ObjectId(getObjectClass(), replacementIdMap); 328 } 329 330 337 public boolean isReplacementIdAttached() { 338 return replacementIdMap != null && !replacementIdMap.isEmpty(); 339 } 340 } | Popular Tags |