Merge pull request #525 from sharwell/tuple-features

Implement hashCode, equals, and toString for Pair and Triple
This commit is contained in:
Terence Parr 2014-03-29 11:32:35 -07:00
commit a3a9d727b3
2 changed files with 55 additions and 0 deletions

View File

@ -38,4 +38,31 @@ public class Pair<A,B> {
this.a = a; this.a = a;
this.b = b; 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; 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);
}
} }