Add Pair.accept(FailableBiConsumer)

Add Pair.apply(FailableBiFunction)
This commit is contained in:
Gary Gregory 2023-05-06 15:43:58 -04:00
parent 02e55fc072
commit 8eef2fc922
3 changed files with 61 additions and 0 deletions

View File

@ -202,6 +202,8 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Diego Marcilio, Bruno P. Kinoshita, Gary Gregory">Add missing exception javadoc/tests for some null arguments #869.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ClassLoaderUtils.getSystemURLs() and getThreadURLs().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add RegExUtils.dotAll() and dotAllMatcher().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Pair.accept(FailableBiConsumer).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Pair.apply(FailableBiFunction).</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot, XenoAmess, Gary Gregory">Bump actions/cache from 2.1.4 to 3.0.10 #742, #752, #764, #833, #867, #959, #964.</action>
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump actions/checkout from 2 to 3.1.0 #819, #825, #859, #963.</action>

View File

@ -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<L, R> implements Map.Entry<L, R>, Comparable<Pair<L,
return ImmutablePair.ofNonNull(left, right);
}
/**
* Accepts this key and value as arguments to the given consumer.
*
* @param <E> 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 <E extends Throwable> void accept(final FailableBiConsumer<L, R, E> consumer) throws E {
consumer.accept(getKey(), getValue());
}
/**
* Applies this key and value as arguments to the given function.
*
* @param <V> The function return type.
* @param <E> 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, E extends Throwable> V apply(final FailableBiFunction<L, R, V, E> 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}.

View File

@ -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<String, String> pair1 = Pair.of("A", "D");
final Pair<String, String> pair2 = Pair.of("B", "C");
final Map<String, String> 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<String, String> pair1 = Pair.of("A", "D");
final Pair<String, String> pair2 = Pair.of("B", "C");
final Map<String, String> 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<String, String> pair1 = Pair.of("A", "D");