@Override
public int hashCode() {
if (this.hashCode == 0) {
HashCodeBuilder builder = new HashCodeBuilder(3, 5);
builder.append(entityName.hashCode());
if (key != null) {
builder.append(key);
}
else if (singleKey != null) {
builder.append(singleKey.hashCode());
// must reconcile all possible numeric types
if (singleValue instanceof Number) {
builder.append(((Number) singleValue).longValue());
}
else {
builder.append(singleValue);
}
}
else if (objectIdKeys != null) {
int len = objectIdKeys.size();
// handle multiple keys - must sort the keys to use with HashCodeBuilder
Object[] keys = objectIdKeys.keySet().toArray();
Arrays.sort(keys);
for (int i = 0; i < len; i++) {
// HashCodeBuilder will take care of processing object if it
// happens to be a primitive array such as byte[]
// also we don't have to append the key hashcode, its index will
// work
builder.append(i);
Object value = objectIdKeys.get(keys[i]);
// must reconcile all possible numeric types
if (value instanceof Number) {
builder.append(((Number) value).longValue());
}
else {
builder.append(value);
}
}
}
this.hashCode = builder.toHashCode();
assert hashCode != 0 : "Generated zero hashCode";
}
return hashCode;
}