Last active
September 12, 2020 12:05
-
-
Save disktnk/4461080 to your computer and use it in GitHub Desktop.
C++の標準ライブラリにあるPairみたいなやつ.
javadocに書いてある通り,StackOverflowにあったコードほぼママ. What is the equivalent of the C++ Pair<L,R> in Java? - Stack Overflow
http://stackoverflow.com/questions/156275/what-is-the-equivalent-of-the-c-pairl-r-in-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
/** | |
* immutable pair class, like c++'s Pair<br> | |
* from <a href= | |
* "http://stackoverflow.com/questions/156275/what-is-the-equivalent-of-the-c-pairl-r-in-java" | |
* > What is the equivalent of the C++ Pair<L,R> in Java? - Stack Overflow </a> | |
* | |
* @param <A> | |
* first | |
* @param <B> | |
* second | |
*/ | |
public class Pair<A, B> { | |
private final A first; | |
private final B second; | |
public Pair(A first, B second) { | |
this.first = first; | |
this.second = second; | |
} | |
@Override | |
public int hashCode() { | |
int hashFirst = first == null ? 0 : first.hashCode(); | |
int hashSecond = second == null ? 0 : second.hashCode(); | |
return (hashFirst + hashSecond) * hashSecond + hashFirst; | |
} | |
@Override | |
public boolean equals(Object other) { | |
if (!(other instanceof Pair)) { | |
return false; | |
} | |
Pair<?, ?> otherPair = (Pair<?, ?>) other; | |
if ((first == otherPair.first) && (second == otherPair.second)) { | |
return true; | |
} | |
if (first == null || otherPair.first == null) { | |
return false; | |
} | |
if (second == null || otherPair.second == null) { | |
return false; | |
} | |
return first.equals(otherPair.first) && second.equals(otherPair.second); | |
} | |
@Override | |
public String toString() { | |
return "(" + first + ", " + second + ")"; | |
} | |
public A getFirst() { | |
return first; | |
} | |
public B getSecond() { | |
return second; | |
} | |
} |
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
import static org.hamcrest.core.IsEqual.equalTo; | |
import static org.hamcrest.core.IsNot.not; | |
import static org.junit.Assert.assertThat; | |
import org.junit.Test; | |
public class PairTest { | |
@Test | |
public void testEquals() { | |
Pair<Integer, String> pair1 = new Pair<Integer, String>(1, "abc"); | |
Pair<Integer, String> pair2 = new Pair<Integer, String>(1, "abd"); | |
Pair<Integer, String> pair3 = new Pair<Integer, String>(1, "abc"); | |
assertThat(pair1, equalTo(pair1)); | |
assertThat(pair1, not(pair2)); | |
assertThat(pair2, not(pair1)); | |
assertThat(pair1, equalTo(pair3)); | |
assertThat(pair3, equalTo(pair1)); | |
Pair<Integer, String> pairNull = new Pair<Integer, String>(null, null); | |
assertThat(pairNull, equalTo(pairNull)); | |
Pair<Integer, String> pair4 = new Pair<Integer, String>(null, "xyz"); | |
Pair<Integer, String> pair5 = new Pair<Integer, String>(null, "xyz"); | |
Pair<Integer, String> pair6 = new Pair<Integer, String>(2, null); | |
Pair<Integer, String> pair7 = new Pair<Integer, String>(2, null); | |
assertThat(pair4, equalTo(pair4)); | |
assertThat(pair6, equalTo(pair6)); | |
assertThat(pair4, equalTo(pair5)); | |
assertThat(pair5, equalTo(pair4)); | |
assertThat(pair6, equalTo(pair7)); | |
assertThat(pair7, equalTo(pair6)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment