use of org.apache.cayenne.util.HashCodeBuilder in project cayenne by apache.
the class ObjectId method hashCode.
@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
String[] keys = objectIdKeys.keySet().toArray(new String[0]);
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;
}
use of org.apache.cayenne.util.HashCodeBuilder in project cayenne by apache.
the class Expression method hashCode.
@Override
public int hashCode() {
HashCodeBuilder builder = new HashCodeBuilder().append(getType());
int opCount = getOperandCount();
for (int i = 0; i < opCount; i++) {
builder.append(getOperand(i));
}
return builder.toHashCode();
}
use of org.apache.cayenne.util.HashCodeBuilder in project cayenne by apache.
the class DbArcId method hashCode.
@Override
public int hashCode() {
if (this.hashCode == 0) {
HashCodeBuilder builder = new HashCodeBuilder(3, 5);
builder.append(sourceId);
builder.append(incomingArc.getName());
this.hashCode = builder.toHashCode();
}
return hashCode;
}
use of org.apache.cayenne.util.HashCodeBuilder in project cayenne by apache.
the class FlattenedArcKey method hashCode.
@Override
public int hashCode() {
// order ids in array for hashcode consistency purposes. The actual
// order direction is not important, as long as it
// is consistent across invocations
int compare = id1.getSourceId().getEntityName().compareTo(id2.getSourceId().getEntityName());
if (compare == 0) {
compare = id1.getIncominArc().getName().compareTo(id2.getIncominArc().getName());
if (compare == 0) {
// since ordering is mostly important for detecting equivalent
// FlattenedArc keys coming from 2 opposite directions, the name
// of ObjRelationship can be a good criteria
ObjRelationship or2 = relationship.getReverseRelationship();
compare = or2 != null ? relationship.getName().compareTo(or2.getName()) : 1;
// TODO: if(compare == 0) ??
}
}
DbArcId[] ordered;
if (compare < 0) {
ordered = new DbArcId[] { id1, id2 };
} else {
ordered = new DbArcId[] { id2, id1 };
}
return new HashCodeBuilder().append(ordered).toHashCode();
}
Aggregations