diff --git a/src/changes/changes.xml b/src/changes/changes.xml index c9ff7eb17..6c09ffcdb 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -202,6 +202,8 @@ The type attribute can be add,update,fix,remove. Add missing exception javadoc/tests for some null arguments #869. Add ClassLoaderUtils.getSystemURLs() and getThreadURLs(). Add RegExUtils.dotAll() and dotAllMatcher(). + Add Pair.accept(FailableBiConsumer). + Add Pair.apply(FailableBiFunction). Bump actions/cache from 2.1.4 to 3.0.10 #742, #752, #764, #833, #867, #959, #964. Bump actions/checkout from 2 to 3.1.0 #819, #825, #859, #963. 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 3b97e061b..17552f98c 100644 --- a/src/main/java/org/apache/commons/lang3/tuple/Pair.java +++ b/src/main/java/org/apache/commons/lang3/tuple/Pair.java @@ -21,6 +21,8 @@ import java.util.Map; import java.util.Objects; import org.apache.commons.lang3.builder.CompareToBuilder; +import org.apache.commons.lang3.function.FailableBiConsumer; +import org.apache.commons.lang3.function.FailableBiFunction; /** * A pair consisting of two elements. @@ -117,6 +119,32 @@ public abstract class Pair implements Map.Entry, Comparable The kind of thrown exception or error. + * @param consumer the consumer to call. + * @throws E Thrown when the consumer fails. + * @since 3.13.0 + */ + public void accept(final FailableBiConsumer consumer) throws E { + consumer.accept(getKey(), getValue()); + } + + /** + * Applies this key and value as arguments to the given function. + * + * @param The function return type. + * @param The kind of thrown exception or error. + * @param function the consumer to call. + * @return the function's return value. + * @throws E Thrown when the consumer fails. + * @since 3.13.0 + */ + public V apply(final FailableBiFunction function) throws E { + return function.apply(getKey(), getValue()); + } + /** * Compares the pair based on the left element followed by the right element. * The types must be {@link Comparable}. 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 235f07a61..f5c92a76d 100644 --- a/src/test/java/org/apache/commons/lang3/tuple/PairTest.java +++ b/src/test/java/org/apache/commons/lang3/tuple/PairTest.java @@ -25,6 +25,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Calendar; import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Map.Entry; import org.apache.commons.lang3.AbstractLangTest; @@ -35,6 +36,36 @@ import org.junit.jupiter.api.Test; */ public class PairTest extends AbstractLangTest { + @Test + public void testAccept() { + final Pair pair1 = Pair.of("A", "D"); + final Pair pair2 = Pair.of("B", "C"); + final Map map = new HashMap<>(); + pair1.accept(map::put); + pair2.accept(map::put); + assertEquals("D", map.get("A")); + assertEquals("C", map.get("B")); + pair1.accept(map::put); + pair2.accept(map::put); + assertEquals("D", map.get("A")); + assertEquals("C", map.get("B")); + } + + @Test + public void testApply() { + final Pair pair1 = Pair.of("A", "D"); + final Pair pair2 = Pair.of("B", "C"); + final Map map = new HashMap<>(); + assertEquals(null, pair1.apply(map::put)); + assertEquals(null, pair2.apply(map::put)); + assertEquals("D", map.get("A")); + assertEquals("C", map.get("B")); + assertEquals("D", pair1.apply(map::put)); + assertEquals("C", pair2.apply(map::put)); + assertEquals("D", map.get("A")); + assertEquals("C", map.get("B")); + } + @Test public void testComparable1() { final Pair pair1 = Pair.of("A", "D");