Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="William Degrange, Gary Gregory, Rob Spoor">Use accessors in ToStringStyle so subclasses can effectively override them.</action>
<action type="fix" dev="ggregory" due-to="aaaaaa, Gary Gregory">`LocaleUtils.toLocale(String)` for a 2 letter country code now returns a value instead of throwing an `IllegalArgumentException`.</action>
<!-- ADD -->
<action type="add" dev="jack5505" due-to="Sabirov Jakhongir">Add MutablePair.copyOf(Map.Entry).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemProperties.getPath(String, Supplier&lt;Path&gt;).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add JavaVersion.JAVA_25.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemUtils.IS_JAVA_25.</action>
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/apache/commons/lang3/tuple/MutablePair.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ public static <L, R> MutablePair<L, R> of(final L left, final R right) {
return new MutablePair<>(left, right);
}

/**
* Creates a mutable pair from an existing pair.
*
* <p>This factory allows the pair to be created using inference to
* obtain the generic types.</p>
*
* @param pair the existing pair.
* @param <L> the left element type.
* @param <R> the right element type.
* @return a pair formed from the two parameters, not null.
* @throws NullPointerException if {@code pair} is null.
* @since 3.20.0
*/
public static <L, R> MutablePair<L, R> copyOf(final Map.Entry<L, R> pair) {
of(Objects.requireNonNull(pair, "pair"));
final L left = pair.getKey();
final R right = pair.getValue();
return new MutablePair<>(left, right);
}

/**
* Creates a mutable pair from a map entry.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ void testEquals() {
assertNotEquals(p, new Object());
}

@Test
void testCopyOf() {
assertNullPointerException(() -> MutablePair.copyOf(null));
final Pair<Integer, String> pair = Pair.of(0, "foo");
final MutablePair<Integer, String> mutablePair = MutablePair.copyOf(pair);
assertEquals(pair.getLeft(), mutablePair.getLeft());
assertEquals(pair.getRight(), mutablePair.getRight());
}

@Test
void testHashCode() {
assertEquals(MutablePair.of(null, "foo").hashCode(), MutablePair.of(null, "foo").hashCode());
Expand Down
Loading