This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push: new 341aaa7 Added ImmutablePair factory methods left() and right(). 341aaa7 is described below commit 341aaa797d515a1fce5def6b41cdc481701ad389 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Tue Jun 23 11:07:22 2020 -0400 Added ImmutablePair factory methods left() and right(). --- src/changes/changes.xml | 1 + .../apache/commons/lang3/tuple/ImmutablePair.java | 30 ++++++++++++++++++ .../commons/lang3/tuple/ImmutablePairTest.java | 37 ++++++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 0ecd04e..03989d5 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -79,6 +79,7 @@ The <action> type attribute can be add,update,fix,remove. <action issue="LANG-1556" type="add" dev="ggregory" due-to="XenoAmess">Use Java 8 lambdas and Map operations.</action> <action issue="LANG-1565" type="add" dev="ggregory" due-to="XenoAmess">Change removeLastFieldSeparator to use endsWith #550.</action> <action issue="LANG-1557" type="add" dev="ggregory" due-to="XenoAmess, Gary Gregory">Change a Pattern to a static final field, for not letting it compile each time the function invoked. #542.</action> + <action type="add" dev="ggregory">Added ImmutablePair factory methods left() and right().</action> </release> <release version="3.10" date="2020-03-22" description="New features and bug fixes. Requires Java 8, supports Java 9, 10, 11."> diff --git a/src/main/java/org/apache/commons/lang3/tuple/ImmutablePair.java b/src/main/java/org/apache/commons/lang3/tuple/ImmutablePair.java index 9c106e6..e6d4b92 100644 --- a/src/main/java/org/apache/commons/lang3/tuple/ImmutablePair.java +++ b/src/main/java/org/apache/commons/lang3/tuple/ImmutablePair.java @@ -70,6 +70,21 @@ public final class ImmutablePair<L, R> extends Pair<L, R> { } /** + * <p>Creates an immutable pair of two objects inferring the generic types.</p> + * + * <p>This factory allows the pair to be created using inference to + * obtain the generic types.</p> + * + * @param <L> the left element type + * @param left the left element, may be null + * @return a pair formed from the two parameters, not null + * @since 3.11 + */ + public static <L, R> Pair<L, R> left(final L left) { + return ImmutablePair.of(left, null); + } + + /** * Returns an immutable pair of nulls. * * @param <L> the left element of this pair. Value is {@code null}. @@ -122,6 +137,21 @@ public final class ImmutablePair<L, R> extends Pair<L, R> { return new ImmutablePair<>(left, right); } + /** + * <p>Creates an immutable pair of two objects inferring the generic types.</p> + * + * <p>This factory allows the pair to be created using inference to + * obtain the generic types.</p> + * + * @param <R> the right element type + * @param right the right element, may be null + * @return a pair formed from the two parameters, not null + * @since 3.11 + */ + public static <L, R> Pair<L, R> right(final R right) { + return ImmutablePair.of(null, right); + } + /** Left object */ public final L left; diff --git a/src/test/java/org/apache/commons/lang3/tuple/ImmutablePairTest.java b/src/test/java/org/apache/commons/lang3/tuple/ImmutablePairTest.java index f4e189a..6dafe71 100644 --- a/src/test/java/org/apache/commons/lang3/tuple/ImmutablePairTest.java +++ b/src/test/java/org/apache/commons/lang3/tuple/ImmutablePairTest.java @@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -209,4 +210,40 @@ public class ImmutablePairTest { assertEquals(item.getLeft() + "" + item.getRight(), entry.getValue()); } } + + @Test + public void testComparableLeftOnly() { + final Pair<String, String> pair1 = ImmutablePair.left("A"); + final Pair<String, String> pair2 = ImmutablePair.left("B"); + assertEquals("A", pair1.getLeft()); + assertEquals("B", pair2.getLeft()); + assertEquals(0, pair1.compareTo(pair1)); + assertTrue(pair1.compareTo(pair2) < 0); + assertEquals(0, pair2.compareTo(pair2)); + assertTrue(pair2.compareTo(pair1) > 0); + } + + @Test + public void testComparableRightOnly() { + final Pair<String, String> pair1 = ImmutablePair.right("A"); + final Pair<String, String> pair2 = ImmutablePair.right("B"); + assertEquals("A", pair1.getRight()); + assertEquals("B", pair2.getRight()); + assertEquals(0, pair1.compareTo(pair1)); + assertTrue(pair1.compareTo(pair2) < 0); + assertEquals(0, pair2.compareTo(pair2)); + assertTrue(pair2.compareTo(pair1) > 0); + } + + @Test + public void testToStringLeft() { + final Pair<String, String> pair = ImmutablePair.left("Key"); + assertEquals("(Key,null)", pair.toString()); + } + + @Test + public void testToStringRight() { + final Pair<String, String> pair = ImmutablePair.right("Value"); + assertEquals("(null,Value)", pair.toString()); + } }