From ae2ba9dfb95e3587129400871e1cc01381219e0e Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Wed, 8 Dec 2021 08:23:02 -0500 Subject: [PATCH] Add constructors that require non-null inputs in org.apache.commons.lang3.tuple. - Add ImmutablePair.ofNonNull(L, R) - Add ImmutableTriple.ofNonNull(L, M, R) - Add MutablePair.ofNonNull(L, R) - Add MutableTriple.ofNonNull(L, M, R) - Add Pair.ofNonNull(L, R) - Add Triple.ofNonNull(L, M, R) --- src/changes/changes.xml | 7 +++++- .../commons/lang3/tuple/ImmutablePair.java | 19 +++++++++++++++ .../commons/lang3/tuple/ImmutableTriple.java | 23 +++++++++++++++++++ .../commons/lang3/tuple/MutablePair.java | 19 +++++++++++++++ .../commons/lang3/tuple/MutableTriple.java | 23 +++++++++++++++++++ .../org/apache/commons/lang3/tuple/Pair.java | 18 +++++++++++++++ .../apache/commons/lang3/tuple/Triple.java | 20 ++++++++++++++++ .../lang3/tuple/ImmutablePairTest.java | 11 +++++++++ .../lang3/tuple/ImmutableTripleTest.java | 14 +++++++++++ .../commons/lang3/tuple/MutablePairTest.java | 11 +++++++++ .../lang3/tuple/MutableTripleTest.java | 15 ++++++++++++ .../apache/commons/lang3/tuple/PairTest.java | 11 +++++++++ .../commons/lang3/tuple/TripleTest.java | 14 +++++++++++ 13 files changed, 204 insertions(+), 1 deletion(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 5a50f5931..359807e5a 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -106,7 +106,12 @@ The type attribute can be add,update,fix,remove. Add EnumUtils.getEnumMap(Class, Function). #730 Add FluentBitSet. Add Streams.instancesOf(Class, Collection). - + Add ImmutablePair.ofNonNull(L, R). + Add ImmutableTriple.ofNonNull(L, M, R). + Add MutablePair.ofNonNull(L, R). + Add MutableTriple.ofNonNull(L, M, R). + Add Pair.ofNonNull(L, R). + Add Triple.ofNonNull(L, M, R). Bump spotbugs-maven-plugin from 4.2.0 to 4.5.0.0 #735, #808, #822, #834. Bump actions/cache from v2.1.4 to v2.1.7 #742, #752, #764, #833. Bump actions/setup-java from v1.4.3 to v2. 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 ef2054137..5b858930b 100644 --- a/src/main/java/org/apache/commons/lang3/tuple/ImmutablePair.java +++ b/src/main/java/org/apache/commons/lang3/tuple/ImmutablePair.java @@ -17,6 +17,7 @@ package org.apache.commons.lang3.tuple; import java.util.Map; +import java.util.Objects; /** *

An immutable pair consisting of two {@code Object} elements.

@@ -138,6 +139,24 @@ public final class ImmutablePair extends Pair { return new ImmutablePair<>(left, right); } + /** + *

Creates an immutable pair of two non-null objects inferring the generic types.

+ * + *

This factory allows the pair to be created using inference to + * obtain the generic types.

+ * + * @param the left element type + * @param the right element type + * @param left the left element, may not be null + * @param right the right element, may not be null + * @return a pair formed from the two parameters, not null + * @throws NullPointerException if any input is null + * @since 3.13.0 + */ + public static ImmutablePair ofNonNull(final L left, final R right) { + return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(right, "right")); + } + /** *

Creates an immutable pair of two objects inferring the generic types.

* diff --git a/src/main/java/org/apache/commons/lang3/tuple/ImmutableTriple.java b/src/main/java/org/apache/commons/lang3/tuple/ImmutableTriple.java index b8aad5505..5ea70e612 100644 --- a/src/main/java/org/apache/commons/lang3/tuple/ImmutableTriple.java +++ b/src/main/java/org/apache/commons/lang3/tuple/ImmutableTriple.java @@ -16,6 +16,8 @@ */ package org.apache.commons.lang3.tuple; +import java.util.Objects; + /** *

An immutable triple consisting of three {@code Object} elements.

* @@ -99,6 +101,27 @@ public final class ImmutableTriple extends Triple { public static ImmutableTriple of(final L left, final M middle, final R right) { return new ImmutableTriple<>(left, middle, right); } + + /** + *

Obtains an immutable triple of three non-null objects inferring the generic types.

+ * + *

This factory allows the triple to be created using inference to + * obtain the generic types.

+ * + * @param the left element type + * @param the middle element type + * @param the right element type + * @param left the left element, may not be null + * @param middle the middle element, may not be null + * @param right the right element, may not be null + * @return a triple formed from the three parameters, not null + * @throws NullPointerException if any input is null + * @since 3.13.0 + */ + public static ImmutableTriple ofNonNull(final L left, final M middle, final R right) { + return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(middle, "middle"), Objects.requireNonNull(right, "right")); + } + /** Left object */ public final L left; /** Middle object */ diff --git a/src/main/java/org/apache/commons/lang3/tuple/MutablePair.java b/src/main/java/org/apache/commons/lang3/tuple/MutablePair.java index b26d4d484..cc23f7f5b 100644 --- a/src/main/java/org/apache/commons/lang3/tuple/MutablePair.java +++ b/src/main/java/org/apache/commons/lang3/tuple/MutablePair.java @@ -17,6 +17,7 @@ package org.apache.commons.lang3.tuple; import java.util.Map; +import java.util.Objects; /** *

A mutable pair consisting of two {@code Object} elements.

@@ -97,6 +98,24 @@ public class MutablePair extends Pair { return new MutablePair<>(left, right); } + /** + *

Creates a mutable pair of two non-null objects inferring the generic types.

+ * + *

This factory allows the pair to be created using inference to + * obtain the generic types.

+ * + * @param the left element type + * @param the right element type + * @param left the left element, may not be null + * @param right the right element, may not be null + * @return a pair formed from the two parameters, not null + * @throws NullPointerException if any input is null + * @since 3.13.0 + */ + public static MutablePair ofNonNull(final L left, final R right) { + return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(right, "right")); + } + /** Left object */ public L left; diff --git a/src/main/java/org/apache/commons/lang3/tuple/MutableTriple.java b/src/main/java/org/apache/commons/lang3/tuple/MutableTriple.java index a952a9683..f40fb4b42 100644 --- a/src/main/java/org/apache/commons/lang3/tuple/MutableTriple.java +++ b/src/main/java/org/apache/commons/lang3/tuple/MutableTriple.java @@ -16,6 +16,8 @@ */ package org.apache.commons.lang3.tuple; +import java.util.Objects; + /** *

A mutable triple consisting of three {@code Object} elements.

* @@ -74,6 +76,27 @@ public class MutableTriple extends Triple { public static MutableTriple of(final L left, final M middle, final R right) { return new MutableTriple<>(left, middle, right); } + + /** + *

Obtains a mutable triple of three non-null objects inferring the generic types.

+ * + *

This factory allows the triple to be created using inference to + * obtain the generic types.

+ * + * @param the left element type + * @param the middle element type + * @param the right element type + * @param left the left element, may not be null + * @param middle the middle element, may not be null + * @param right the right element, may not be null + * @return a triple formed from the three parameters, not null + * @throws NullPointerException if any input is null + * @since 3.13.0 + */ + public static MutableTriple ofNonNull(final L left, final M middle, final R right) { + return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(middle, "middle"), Objects.requireNonNull(right, "right")); + } + /** Left object */ public L left; /** Middle object */ diff --git a/src/main/java/org/apache/commons/lang3/tuple/Pair.java b/src/main/java/org/apache/commons/lang3/tuple/Pair.java index 245da3f1e..628213ce1 100644 --- a/src/main/java/org/apache/commons/lang3/tuple/Pair.java +++ b/src/main/java/org/apache/commons/lang3/tuple/Pair.java @@ -120,6 +120,24 @@ public abstract class Pair implements Map.Entry, ComparableCreates an immutable pair of two non-null objects inferring the generic types.

+ * + *

This factory allows the pair to be created using inference to + * obtain the generic types.

+ * + * @param the left element type + * @param the right element type + * @param left the left element, may not be null + * @param right the right element, may not be null + * @return a pair formed from the two parameters, not null + * @throws NullPointerException if any input is null + * @since 3.13.0 + */ + public static Pair ofNonNull(final L left, final R right) { + return ImmutablePair.ofNonNull(left, right); + } + /** *

Compares the pair based on the left element followed by the right element. * The types must be {@code Comparable}.

diff --git a/src/main/java/org/apache/commons/lang3/tuple/Triple.java b/src/main/java/org/apache/commons/lang3/tuple/Triple.java index bf900e9c6..790bfb440 100644 --- a/src/main/java/org/apache/commons/lang3/tuple/Triple.java +++ b/src/main/java/org/apache/commons/lang3/tuple/Triple.java @@ -106,6 +106,26 @@ public abstract class Triple implements Comparable>, Se return new ImmutableTriple<>(left, middle, right); } + /** + *

Obtains an immutable triple of three non-null objects inferring the generic types.

+ * + *

This factory allows the triple to be created using inference to + * obtain the generic types.

+ * + * @param the left element type + * @param the middle element type + * @param the right element type + * @param left the left element, may not be null + * @param middle the middle element, may not be null + * @param right the right element, may not be null + * @return a triple formed from the three parameters, not null + * @throws NullPointerException if any input is null + * @since 3.13.0 + */ + public static Triple ofNonNull(final L left, final M middle, final R right) { + return ImmutableTriple.ofNonNull(left, middle, right); + } + /** *

Compares the triple based on the left element, followed by the middle element, * finally the right element. 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 c1b9f357c..2bfa17e91 100644 --- a/src/test/java/org/apache/commons/lang3/tuple/ImmutablePairTest.java +++ b/src/test/java/org/apache/commons/lang3/tuple/ImmutablePairTest.java @@ -22,6 +22,7 @@ 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 static org.junit.jupiter.api.Assertions.assertThrows; import java.util.ArrayList; import java.util.HashMap; @@ -154,6 +155,16 @@ public class ImmutablePairTest { assertNull(ImmutablePair.nullPair().getValue()); } + @Test + public void testOfNonNull() { + assertThrows(NullPointerException.class, () -> ImmutablePair.ofNonNull(null, null)); + assertThrows(NullPointerException.class, () -> ImmutablePair.ofNonNull(null, "x")); + assertThrows(NullPointerException.class, () -> ImmutablePair.ofNonNull("x", null)); + final ImmutablePair pair = ImmutablePair.ofNonNull("x", "y"); + assertEquals("x", pair.left); + assertEquals("y", pair.right); + } + @Test public void testPairOfMapEntry() { final HashMap map = new HashMap<>(); diff --git a/src/test/java/org/apache/commons/lang3/tuple/ImmutableTripleTest.java b/src/test/java/org/apache/commons/lang3/tuple/ImmutableTripleTest.java index dc01ac5b3..ddaea459d 100644 --- a/src/test/java/org/apache/commons/lang3/tuple/ImmutableTripleTest.java +++ b/src/test/java/org/apache/commons/lang3/tuple/ImmutableTripleTest.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.assertThrows; import java.util.ArrayList; import java.util.HashMap; @@ -116,6 +117,19 @@ public class ImmutableTripleTest { assertNotNull(triple); } + @Test + public void testOfNonNull() { + assertThrows(NullPointerException.class, () -> ImmutableTriple.ofNonNull(null, null, null)); + assertThrows(NullPointerException.class, () -> ImmutableTriple.ofNonNull(null, null, "z")); + assertThrows(NullPointerException.class, () -> ImmutableTriple.ofNonNull(null, "y", "z")); + assertThrows(NullPointerException.class, () -> ImmutableTriple.ofNonNull("x", null, null)); + assertThrows(NullPointerException.class, () -> ImmutableTriple.ofNonNull("x", "y", null)); + final ImmutableTriple pair = ImmutableTriple.ofNonNull("x", "y", "z"); + assertEquals("x", pair.left); + assertEquals("y", pair.middle); + assertEquals("z", pair.right); + } + @Test @SuppressWarnings("unchecked") public void testSerialization() throws Exception { diff --git a/src/test/java/org/apache/commons/lang3/tuple/MutablePairTest.java b/src/test/java/org/apache/commons/lang3/tuple/MutablePairTest.java index 045b02c30..1497a3614 100644 --- a/src/test/java/org/apache/commons/lang3/tuple/MutablePairTest.java +++ b/src/test/java/org/apache/commons/lang3/tuple/MutablePairTest.java @@ -19,6 +19,7 @@ package org.apache.commons.lang3.tuple; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.HashMap; import java.util.Map.Entry; @@ -102,6 +103,16 @@ public class MutablePairTest { assertEquals("bar", pair.getRight()); } + @Test + public void testOfNonNull() { + assertThrows(NullPointerException.class, () -> MutablePair.ofNonNull(null, null)); + assertThrows(NullPointerException.class, () -> MutablePair.ofNonNull(null, "x")); + assertThrows(NullPointerException.class, () -> MutablePair.ofNonNull("x", null)); + final MutablePair pair = MutablePair.ofNonNull("x", "y"); + assertEquals("x", pair.left); + assertEquals("y", pair.right); + } + @Test public void testPairOfMapEntry() { final HashMap map = new HashMap<>(); diff --git a/src/test/java/org/apache/commons/lang3/tuple/MutableTripleTest.java b/src/test/java/org/apache/commons/lang3/tuple/MutableTripleTest.java index d4de141a3..059ee8baf 100644 --- a/src/test/java/org/apache/commons/lang3/tuple/MutableTripleTest.java +++ b/src/test/java/org/apache/commons/lang3/tuple/MutableTripleTest.java @@ -19,6 +19,7 @@ package org.apache.commons.lang3.tuple; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.apache.commons.lang3.SerializationUtils; import org.junit.jupiter.api.Test; @@ -28,6 +29,20 @@ import org.junit.jupiter.api.Test; */ public class MutableTripleTest { + @Test + public void testOfNonNull() { + assertThrows(NullPointerException.class, () -> MutableTriple.ofNonNull(null, null, null)); + assertThrows(NullPointerException.class, () -> MutableTriple.ofNonNull(null, null, "z")); + assertThrows(NullPointerException.class, () -> MutableTriple.ofNonNull(null, "y", "z")); + assertThrows(NullPointerException.class, () -> MutableTriple.ofNonNull("x", null, null)); + assertThrows(NullPointerException.class, () -> MutableTriple.ofNonNull("x", "y", null)); + final MutableTriple pair = MutableTriple.ofNonNull("x", "y", "z"); + assertEquals("x", pair.left); + assertEquals("y", pair.middle); + assertEquals("z", pair.right); + } + + @Test public void testBasic() { final MutableTriple triple = new MutableTriple<>(0, "foo", Boolean.FALSE); diff --git a/src/test/java/org/apache/commons/lang3/tuple/PairTest.java b/src/test/java/org/apache/commons/lang3/tuple/PairTest.java index 80bae1210..4640a9c07 100644 --- a/src/test/java/org/apache/commons/lang3/tuple/PairTest.java +++ b/src/test/java/org/apache/commons/lang3/tuple/PairTest.java @@ -19,6 +19,7 @@ package org.apache.commons.lang3.tuple; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Calendar; @@ -103,6 +104,16 @@ public class PairTest { assertEquals(pair.hashCode(), entry.hashCode()); } + @Test + public void testOfNonNull() { + assertThrows(NullPointerException.class, () -> Pair.ofNonNull(null, null)); + assertThrows(NullPointerException.class, () -> Pair.ofNonNull(null, "x")); + assertThrows(NullPointerException.class, () -> Pair.ofNonNull("x", null)); + final Pair pair = Pair.ofNonNull("x", "y"); + assertEquals("x", pair.getLeft()); + assertEquals("y", pair.getRight()); + } + @Test public void testPairOfMapEntry() { final HashMap map = new HashMap<>(); diff --git a/src/test/java/org/apache/commons/lang3/tuple/TripleTest.java b/src/test/java/org/apache/commons/lang3/tuple/TripleTest.java index b9ae9de68..496785873 100644 --- a/src/test/java/org/apache/commons/lang3/tuple/TripleTest.java +++ b/src/test/java/org/apache/commons/lang3/tuple/TripleTest.java @@ -18,6 +18,7 @@ package org.apache.commons.lang3.tuple; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Calendar; @@ -30,6 +31,19 @@ import org.junit.jupiter.api.Test; */ public class TripleTest { + @Test + public void testOfNonNull() { + assertThrows(NullPointerException.class, () -> Triple.ofNonNull(null, null, null)); + assertThrows(NullPointerException.class, () -> Triple.ofNonNull(null, null, "z")); + assertThrows(NullPointerException.class, () -> Triple.ofNonNull(null, "y", "z")); + assertThrows(NullPointerException.class, () -> Triple.ofNonNull("x", null, null)); + assertThrows(NullPointerException.class, () -> Triple.ofNonNull("x", "y", null)); + final Triple pair = Triple.ofNonNull("x", "y", "z"); + assertEquals("x", pair.getLeft()); + assertEquals("y", pair.getMiddle()); + assertEquals("z", pair.getRight()); + } + @Test public void testComparable1() { final Triple triple1 = Triple.of("A", "D", "A");