Skip to content

Commit c140fc8

Browse files
committed
Add MutablePair.ofNonNull(Map.Entry)
A variation of PR #1461
1 parent 5572527 commit c140fc8

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ The <action> type attribute can be add,update,fix,remove.
5555
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemProperties.getPath(String, Supplier&lt;Path&gt;).</action>
5656
<action type="add" dev="ggregory" due-to="Gary Gregory">Add JavaVersion.JAVA_25.</action>
5757
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemUtils.IS_JAVA_25.</action>
58+
<action type="add" dev="ggregory" due-to="jack5505, Gary Gregory">Add MutablePair.ofNonNull(Map.Entry).</action>
5859
<!-- UPDATE -->
5960
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 88 to 89.</action>
6061
</release>

src/main/java/org/apache/commons/lang3/tuple/MutablePair.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@ public static <L, R> MutablePair<L, R> ofNonNull(final L left, final R right) {
114114
return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(right, "right"));
115115
}
116116

117+
/**
118+
* Creates a mutable pair from a map entry.
119+
*
120+
* @param <L> the left element type
121+
* @param <R> the right element type
122+
* @param pair the existing map entry.
123+
* @return a mutable pair formed from the map entry
124+
* @throws NullPointerException if the pair is null.
125+
* @since 3.20
126+
*/
127+
public static <L, R> MutablePair<L, R> ofNonNull(final Map.Entry<L, R> pair) {
128+
return of(Objects.requireNonNull(pair, "pair"));
129+
}
130+
117131
/** Left object. */
118132
public L left;
119133

src/test/java/org/apache/commons/lang3/tuple/MutablePairTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ void testOfNonNull() {
114114
assertEquals("y", pair.right);
115115
}
116116

117+
@Test
118+
void testOfNonNullMapEntry() {
119+
assertNullPointerException(() -> MutablePair.ofNonNull(null));
120+
final Pair<Integer, String> pair = Pair.of(0, "foo");
121+
final MutablePair<Integer, String> mutablePair = MutablePair.ofNonNull(pair);
122+
assertEquals(pair.getLeft(), mutablePair.getLeft());
123+
assertEquals(pair.getRight(), mutablePair.getRight());
124+
}
125+
117126
@Test
118127
void testPairOfMapEntry() {
119128
assertNull(MutablePair.of(null).getLeft());

0 commit comments

Comments
 (0)