Created
December 19, 2016 18:39
-
-
Save xizzhu/940728509b0d6282173c078a895a7333 to your computer and use it in GitHub Desktop.
Triple for Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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