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)
This commit is contained in:
Gary Gregory 2021-12-08 08:23:02 -05:00
parent 656d2023dc
commit ae2ba9dfb9
13 changed files with 204 additions and 1 deletions

View File

@ -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>

View File

@ -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>
@ -138,6 +139,24 @@ public final class ImmutablePair<L, R> extends Pair<L, R> {
return new ImmutablePair<>(left, right);
}
/**
* <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>
*

View File

@ -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 */

View File

@ -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;

View File

@ -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 */

View File

@ -120,6 +120,24 @@ public abstract class Pair<L, R> implements Map.Entry<L, R>, Comparable<Pair<L,
return ImmutablePair.of(pair);
}
/**
* <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>

View File

@ -106,6 +106,26 @@ public abstract class Triple<L, M, R> implements Comparable<Triple<L, M, R>>, Se
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> 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.

View File

@ -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<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<>();

View File

@ -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<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 {

View File

@ -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<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<>();

View File

@ -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<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);

View File

@ -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<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<>();

View File

@ -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<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");