Skip to content

Instantly share code, notes, and snippets.

@xizzhu
Created Dec 19, 2016
Embed
What would you like to do?
Triple for Java
public class Triple<F, S, T> {
public final F first;
public final S second;
public final T third;
public Triple(F first, S second, T third) {
this.first = first;
this.second = second;
this.third = third;
}
@Override
public int hashCode() {
return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode())
^ (third == null ? 0 : third.hashCode());
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Triple)) {
return false;
}
final Triple<?, ?, ?> other = (Triple<?, ?, ?>) o;
return objectsEqual(other.first, first) && objectsEqual(other.second, second)
&& objectsEqual(other.third, third);
}
private static boolean objectsEqual(Object a, Object b) {
return a == b || (a != null && a.equals(b));
}
@Override
public String toString() {
return "Triple{" + String.valueOf(first) + " " + String.valueOf(second) + " " + String.valueOf(third) + "}";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment