Skip to content

Commit

Permalink
Implement identicalTwin() and identicalTriplet() methods on the Tuple…
Browse files Browse the repository at this point in the history
…s factory class.

Signed-off-by: Rajiv Sethumadhavan <rajiv.sethumadhavan@bnymellon.com>
  • Loading branch information
rajivsethumadhavan committed Aug 7, 2020
1 parent b129f0d commit 9977009
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public static <T> Twin<T> twin(T one, T two)
return new TwinImpl<>(one, two);
}

/**
* This method returns a {@link Twin} where both elements in the {@link Twin} are the same as the input element.
*/
public static <T> Twin<T> identicalTwin(T each)
{
return new TwinImpl<>(each, each);
}

public static <T1, T2, T3> Triple<T1, T2, T3> triple(T1 one, T2 two, T3 three)
{
return new TripleImpl<>(one, two, three);
Expand All @@ -57,4 +65,12 @@ public static <T> Triplet<T> triplet(T one, T two, T three)
{
return new TripletImpl<>(one, two, three);
}

/**
* This method returns a {@link Triplet} where the three elements in the {@link Triplet} are the same as the input element.
*/
public static <T> Triplet<T> identicalTriplet(T each)
{
return new TripletImpl<>(each, each, each);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ public void twin()
Assert.assertEquals("2", twin.getTwo());
}

@Test
public void identicalTwin()
{
Twin<String> twin = Tuples.identicalTwin("1");
Assert.assertEquals("1", twin.getOne());
Assert.assertEquals("1", twin.getTwo());
Assert.assertEquals(twin.getOne(), twin.getTwo());
}

@Test
public void triple()
{
Expand All @@ -68,15 +77,30 @@ public void triplet()
Assert.assertEquals("3", triplet.getThree());
}

@Test
public void identicalTriplet()
{
Triplet<String> triplet = Tuples.identicalTriplet("1");
Assert.assertEquals("1", triplet.getOne());
Assert.assertEquals("1", triplet.getTwo());
Assert.assertEquals("1", triplet.getThree());
Assert.assertEquals(triplet.getOne(), triplet.getTwo());
Assert.assertEquals(triplet.getTwo(), triplet.getThree());
Assert.assertEquals(triplet.getThree(), triplet.getOne());
}

@Test
public void equalsHashCode()
{
Twin<String> pair1 = Tuples.twin("1", "1");
Pair<String, String> pair1a = Tuples.pair("1", "1");
Pair<String, String> pair2 = Tuples.pair("2", "2");
Twin<String> pair3 = Tuples.identicalTwin("1");

Verify.assertEqualsAndHashCode(pair1, pair1);
Verify.assertEqualsAndHashCode(pair1, pair1a);
Verify.assertEqualsAndHashCode(pair3, pair3);
Verify.assertEqualsAndHashCode(pair1, pair3);
Assert.assertNotEquals(pair1, pair2);
Assert.assertNotEquals(pair1, new Object());
}
Expand All @@ -87,9 +111,12 @@ public void equalsHashCodeTriple()
Triplet<String> triple1 = Tuples.triplet("1", "1", "1");
Triple<String, String, String> triple1a = Tuples.triple("1", "1", "1");
Triple<String, String, String> triple2 = Tuples.triple("2", "2", "2");
Triplet<String> triple3 = Tuples.identicalTriplet("1");

Verify.assertEqualsAndHashCode(triple1, triple1);
Verify.assertEqualsAndHashCode(triple1, triple1a);
Verify.assertEqualsAndHashCode(triple3, triple3);
Verify.assertEqualsAndHashCode(triple1, triple3);
Assert.assertNotEquals(triple1, triple2);
Assert.assertNotEquals(triple1, new Object());
}
Expand All @@ -112,6 +139,12 @@ public void testToString()

Triple<String, String, String> triple = Tuples.triple("1", "2", "3");
Assert.assertEquals("1:2:3", triple.toString());

Twin<String> identicalTwin = Tuples.identicalTwin("1");
Assert.assertEquals("1:1", identicalTwin.toString());

Triplet<String> identicalTriplet = Tuples.identicalTriplet("1");
Assert.assertEquals("1:1:1", identicalTriplet.toString());
}

@Test
Expand Down Expand Up @@ -139,6 +172,13 @@ public void swap()
Assert.assertEquals("1", swappedTwin.getOne());
Assert.assertEquals("One", swappedTwin.getTwo());
Assert.assertEquals(expectedTwin, swappedTwin);

Twin<String> identicalTwin = Tuples.identicalTwin("1");
Twin<String> swappedIdenticalTwin = identicalTwin.swap();
Twin<String> expectedIdenticalTwin = Tuples.identicalTwin("1");
Assert.assertEquals("1", swappedIdenticalTwin.getOne());
Assert.assertEquals("1", swappedIdenticalTwin.getTwo());
Assert.assertEquals(expectedIdenticalTwin, swappedIdenticalTwin);
}

@Test
Expand All @@ -159,5 +199,13 @@ public void reverse()
Assert.assertEquals("2", reversedTriplet.getTwo());
Assert.assertEquals("One", reversedTriplet.getThree());
Assert.assertEquals(expectedTriplet, reversedTriplet);

Triplet<String> identicalTriplet = Tuples.identicalTriplet("One");
Triplet<String> reversedIdenticalTriplet = identicalTriplet.reverse();
Triplet<String> expectedIdenticalTriplet = Tuples.identicalTriplet("One");
Assert.assertEquals("One", reversedIdenticalTriplet.getOne());
Assert.assertEquals("One", reversedIdenticalTriplet.getTwo());
Assert.assertEquals("One", reversedIdenticalTriplet.getThree());
Assert.assertEquals(expectedIdenticalTriplet, reversedIdenticalTriplet);
}
}

0 comments on commit 9977009

Please sign in to comment.