Implement hashCode, equals, and toString for Pair and Triple

This commit is contained in:
Sam Harwell 2014-03-29 13:19:40 -05:00
parent afb3390dab
commit c0d158294a
2 changed files with 55 additions and 0 deletions

View File

@ -38,4 +38,31 @@ public class Pair<A,B> {
this.a = a;
this.b = b;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
else if (!(obj instanceof Pair<?, ?>)) {
return false;
}
Pair<?, ?> other = (Pair<?, ?>)obj;
return ObjectEqualityComparator.INSTANCE.equals(a, other.a)
&& ObjectEqualityComparator.INSTANCE.equals(b, other.b);
}
@Override
public int hashCode() {
int hash = MurmurHash.initialize();
hash = MurmurHash.update(hash, a);
hash = MurmurHash.update(hash, b);
return MurmurHash.finish(hash, 2);
}
@Override
public String toString() {
return String.format("(%s, %s)", a, b);
}
}

View File

@ -41,4 +41,32 @@ public class Triple<A,B,C> {
this.c = c;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
else if (!(obj instanceof Triple<?, ?, ?>)) {
return false;
}
Triple<?, ?, ?> other = (Triple<?, ?, ?>)obj;
return ObjectEqualityComparator.INSTANCE.equals(a, other.a)
&& ObjectEqualityComparator.INSTANCE.equals(b, other.b)
&& ObjectEqualityComparator.INSTANCE.equals(c, other.c);
}
@Override
public int hashCode() {
int hash = MurmurHash.initialize();
hash = MurmurHash.update(hash, a);
hash = MurmurHash.update(hash, b);
hash = MurmurHash.update(hash, c);
return MurmurHash.finish(hash, 3);
}
@Override
public String toString() {
return String.format("(%s, %s, %s)", a, b, c);
}
}