You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/12/08 13:23:08 UTC
[commons-lang] branch master updated: Add constructors that require non-null inputs in org.apache.commons.lang3.tuple.
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 ae2ba9d Add constructors that require non-null inputs in org.apache.commons.lang3.tuple.
ae2ba9d is described below
commit ae2ba9dfb95e3587129400871e1cc01381219e0e
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Dec 8 08:23:02 2021 -0500
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 ++++++-
.../apache/commons/lang3/tuple/ImmutablePair.java | 19 ++++++++++++++++++
.../commons/lang3/tuple/ImmutableTriple.java | 23 ++++++++++++++++++++++
.../apache/commons/lang3/tuple/MutablePair.java | 19 ++++++++++++++++++
.../apache/commons/lang3/tuple/MutableTriple.java | 23 ++++++++++++++++++++++
.../java/org/apache/commons/lang3/tuple/Pair.java | 18 +++++++++++++++++
.../org/apache/commons/lang3/tuple/Triple.java | 20 +++++++++++++++++++
.../commons/lang3/tuple/ImmutablePairTest.java | 11 +++++++++++
.../commons/lang3/tuple/ImmutableTripleTest.java | 14 +++++++++++++
.../commons/lang3/tuple/MutablePairTest.java | 11 +++++++++++
.../commons/lang3/tuple/MutableTripleTest.java | 15 ++++++++++++++
.../org/apache/commons/lang3/tuple/PairTest.java | 11 +++++++++++
.../org/apache/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 5a50f59..359807e 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -106,7 +106,12 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Maxwell Cody, Gary Gregory">Add EnumUtils.getEnumMap(Class, Function). #730</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add FluentBitSet.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Streams.instancesOf(Class, Collection).</action>
- <!-- UPDATE -->
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add ImmutablePair.ofNonNull(L, R).</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add ImmutableTriple.ofNonNull(L, M, R).</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add MutablePair.ofNonNull(L, R).</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add MutableTriple.ofNonNull(L, M, R).</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add Pair.ofNonNull(L, R).</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add Triple.ofNonNull(L, M, R).</action> <!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump spotbugs-maven-plugin from 4.2.0 to 4.5.0.0 #735, #808, #822, #834.</action>
<action type="update" dev="ggregory" due-to="Dependabot, XenoAmess">Bump actions/cache from v2.1.4 to v2.1.7 #742, #752, #764, #833.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump actions/setup-java from v1.4.3 to v2.</action>
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 ef20541..5b85893 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;
/**
* <p>An immutable pair consisting of two {@code Object} elements.</p>
@@ -139,6 +140,24 @@ public final class ImmutablePair<L, R> extends Pair<L, R> {
}
/**
+ * <p>Creates an immutable pair of two non-null 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 <R> 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 <L, R> ImmutablePair<L, R> ofNonNull(final L left, final R right) {
+ return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(right, "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
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 b8aad55..5ea70e6 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;
+
/**
* <p>An immutable triple consisting of three {@code Object} elements.</p>
*
@@ -99,6 +101,27 @@ public final class ImmutableTriple<L, M, R> extends Triple<L, M, R> {
public static <L, M, R> ImmutableTriple<L, M, R> of(final L left, final M middle, final R right) {
return new ImmutableTriple<>(left, middle, right);
}
+
+ /**
+ * <p>Obtains an immutable triple of three non-null objects inferring the generic types.</p>
+ *
+ * <p>This factory allows the triple to be created using inference to
+ * obtain the generic types.</p>
+ *
+ * @param <L> the left element type
+ * @param <M> the middle element type
+ * @param <R> 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 <L, M, R> ImmutableTriple<L, M, R> 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 b26d4d4..cc23f7f 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;
/**
* <p>A mutable pair consisting of two {@code Object} elements.</p>
@@ -97,6 +98,24 @@ public class MutablePair<L, R> extends Pair<L, R> {
return new MutablePair<>(left, right);
}
+ /**
+ * <p>Creates a mutable pair of two non-null 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 <R> 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 <L, R> MutablePair<L, R> 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 a952a96..f40fb4b 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;
+
/**
* <p>A mutable triple consisting of three {@code Object} elements.</p>
*
@@ -74,6 +76,27 @@ public class MutableTriple<L, M, R> extends Triple<L, M, R> {
public static <L, M, R> MutableTriple<L, M, R> of(final L left, final M middle, final R right) {
return new MutableTriple<>(left, middle, right);
}
+
+ /**
+ * <p>Obtains a mutable triple of three non-null objects inferring the generic types.</p>
+ *
+ * <p>This factory allows the triple to be created using inference to
+ * obtain the generic types.</p>
+ *
+ * @param <L> the left element type
+ * @param <M> the middle element type
+ * @param <R> 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 <L, M, R> MutableTriple<L, M, R> 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 245da3f..628213c 100644
--- a/src/main/java/org/apache/commons/lang3/tuple/Pair.java
+++ b/src/main/java/org/apache/commons/lang3/tuple/Pair.java
@@ -121,6 +121,24 @@ public abstract class Pair<L, R> implements Map.Entry<L, R>, Comparable<Pair<L,
}
/**
+ * <p>Creates an immutable pair of two non-null 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 <R> 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 <L, R> Pair<L, R> ofNonNull(final L left, final R right) {
+ return ImmutablePair.ofNonNull(left, right);
+ }
+
+ /**
* <p>Compares the pair based on the left element followed by the right element.
* The types must be {@code Comparable}.</p>
*
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 bf900e9..790bfb4 100644
--- a/src/main/java/org/apache/commons/lang3/tuple/Triple.java
+++ b/src/main/java/org/apache/commons/lang3/tuple/Triple.java
@@ -107,6 +107,26 @@ public abstract class Triple<L, M, R> implements Comparable<Triple<L, M, R>>, Se
}
/**
+ * <p>Obtains an immutable triple of three non-null objects inferring the generic types.</p>
+ *
+ * <p>This factory allows the triple to be created using inference to
+ * obtain the generic types.</p>
+ *
+ * @param <L> the left element type
+ * @param <M> the middle element type
+ * @param <R> 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 <L, M, R> Triple<L, M, R> ofNonNull(final L left, final M middle, final R right) {
+ return ImmutableTriple.ofNonNull(left, middle, right);
+ }
+
+ /**
* <p>Compares the triple based on the left element, followed by the middle element,
* finally the right element.
* The types must be {@code Comparable}.</p>
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 c1b9f35..2bfa17e 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;
@@ -155,6 +156,16 @@ public class ImmutablePairTest {
}
@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<String, String> pair = ImmutablePair.ofNonNull("x", "y");
+ assertEquals("x", pair.left);
+ assertEquals("y", pair.right);
+ }
+
+ @Test
public void testPairOfMapEntry() {
final HashMap<Integer, String> map = new HashMap<>();
map.put(0, "foo");
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 dc01ac5..ddaea45 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;
@@ -117,6 +118,19 @@ public class ImmutableTripleTest {
}
@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<String, String, String> 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 {
final ImmutableTriple<Integer, String, Boolean> origTriple = ImmutableTriple.of(0, "foo", Boolean.TRUE);
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 045b02c..1497a36 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;
@@ -103,6 +104,16 @@ public class MutablePairTest {
}
@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<String, String> pair = MutablePair.ofNonNull("x", "y");
+ assertEquals("x", pair.left);
+ assertEquals("y", pair.right);
+ }
+
+ @Test
public void testPairOfMapEntry() {
final HashMap<Integer, String> map = new HashMap<>();
map.put(0, "foo");
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 d4de141..059ee8b 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;
@@ -29,6 +30,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<String, String, String> pair = MutableTriple.ofNonNull("x", "y", "z");
+ assertEquals("x", pair.left);
+ assertEquals("y", pair.middle);
+ assertEquals("z", pair.right);
+ }
+
+
+ @Test
public void testBasic() {
final MutableTriple<Integer, String, Boolean> triple = new MutableTriple<>(0, "foo", Boolean.FALSE);
assertEquals(0, triple.getLeft().intValue());
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 80bae12..4640a9c 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;
@@ -104,6 +105,16 @@ public class PairTest {
}
@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<String, String> pair = Pair.ofNonNull("x", "y");
+ assertEquals("x", pair.getLeft());
+ assertEquals("y", pair.getRight());
+ }
+
+ @Test
public void testPairOfMapEntry() {
final HashMap<Integer, String> map = new HashMap<>();
map.put(0, "foo");
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 b9ae9de..4967858 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;
@@ -31,6 +32,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<String, String, String> pair = Triple.ofNonNull("x", "y", "z");
+ assertEquals("x", pair.getLeft());
+ assertEquals("y", pair.getMiddle());
+ assertEquals("z", pair.getRight());
+ }
+
+ @Test
public void testComparable1() {
final Triple<String, String, String> triple1 = Triple.of("A", "D", "A");
final Triple<String, String, String> triple2 = Triple.of("B", "C", "A");