1 56 package org.objectstyle.cayenne.distribution; 57 58 import java.io.Serializable ; 59 import java.util.Arrays ; 60 import java.util.Collections ; 61 import java.util.Iterator ; 62 import java.util.Map ; 63 64 import org.apache.commons.lang.builder.EqualsBuilder; 65 import org.apache.commons.lang.builder.HashCodeBuilder; 66 import org.apache.commons.lang.builder.ToStringBuilder; 67 import org.objectstyle.cayenne.util.IDUtil; 68 import org.objectstyle.cayenne.util.Util; 69 70 76 public class GlobalID implements Serializable { 78 79 protected String entityName; 80 protected Map objectIdKeys; 81 82 protected byte[] key; 83 84 87 public GlobalID(String entityName) { 90 this.entityName = entityName; 91 this.key = IDUtil.pseudoUniqueByteSequence16(); 92 } 93 94 public GlobalID(String entityName, byte[] key) { 95 this.entityName = entityName; 96 this.key = key; 97 } 98 99 102 public GlobalID(String entityName, Map idKeys) { 103 this.entityName = entityName; 104 this.objectIdKeys = Collections.unmodifiableMap(idKeys); 105 } 106 107 public boolean isTemporary() { 108 return key != null; 109 } 110 111 public String getEntityName() { 112 return entityName; 113 } 114 115 public byte[] getKey() { 116 return key; 117 } 118 119 public Map getObjectIdKeys() { 120 return objectIdKeys; 121 } 122 123 public boolean equals(Object object) { 124 if (this == object) { 125 return true; 126 } 127 128 if (!(object instanceof GlobalID)) { 129 return false; 130 } 131 132 GlobalID id = (GlobalID) object; 133 134 if (isTemporary()) { 135 return new EqualsBuilder() 136 .append(entityName, entityName) 137 .append(key, id.key) 138 .isEquals(); 139 } 140 141 if (!Util.nullSafeEquals(entityName, id.entityName)) { 142 return false; 143 } 144 145 if (id.objectIdKeys == null && objectIdKeys == null) { 146 return true; 147 } 148 149 if (id.objectIdKeys == null || objectIdKeys == null) { 150 return false; 151 } 152 153 if (id.objectIdKeys.size() != objectIdKeys.size()) { 154 return false; 155 } 156 157 EqualsBuilder builder = new EqualsBuilder(); 158 Iterator entries = objectIdKeys.entrySet().iterator(); 159 while (entries.hasNext()) { 160 Map.Entry entry = (Map.Entry ) entries.next(); 161 162 Object key = entry.getKey(); 163 Object value = entry.getValue(); 164 if (value == null) { 165 if (id.objectIdKeys.get(key) != null || !id.objectIdKeys.containsKey(key)) { 166 return false; 167 } 168 } 169 else { 170 builder.append(value, id.objectIdKeys.get(key)); 172 if (!builder.isEquals()) { 173 return false; 174 } 175 } 176 } 177 178 return true; 179 } 180 181 public int hashCode() { 182 183 185 HashCodeBuilder builder = new HashCodeBuilder(3, 5); 186 builder.append(entityName.hashCode()); 187 188 if (key != null) { 189 builder.append(key); 190 } 191 192 if (objectIdKeys != null) { 193 int len = objectIdKeys.size(); 194 195 if (len == 1) { 197 Iterator entries = objectIdKeys.entrySet().iterator(); 198 Map.Entry entry = (Map.Entry ) entries.next(); 199 builder.append(entry.getKey()).append(entry.getValue()); 200 } 201 else { 203 Object [] keys = objectIdKeys.keySet().toArray(); 204 Arrays.sort(keys); 205 206 for (int i = 0; i < len; i++) { 207 210 builder.append(i).append(objectIdKeys.get(keys[i])); 213 } 214 } 215 } 216 217 return builder.toHashCode(); 218 } 219 220 public String toString() { 221 ToStringBuilder builder = new ToStringBuilder(this); 222 223 builder.append("entityName", entityName); 224 builder.append("temporary", isTemporary()); 225 226 if (isTemporary()) { 227 builder.append("key", key); 228 } 229 else if (objectIdKeys != null) { 230 Iterator it = objectIdKeys.entrySet().iterator(); 231 while (it.hasNext()) { 232 Map.Entry entry = (Map.Entry ) it.next(); 233 builder.append(String.valueOf(entry.getKey()), entry.getValue()); 234 } 235 } 236 return builder.toString(); 237 } 238 } 239 | Popular Tags |