Skip to content

Instantly share code, notes, and snippets.

@xizzhu
Created December 19, 2016 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xizzhu/940728509b0d6282173c078a895a7333 to your computer and use it in GitHub Desktop.
Save xizzhu/940728509b0d6282173c078a895a7333 to your computer and use it in GitHub Desktop.
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