[LANG-1503] Add factory methods to Pair classes with Map.Entry input.

#454.

Also adds tests that were not in the PR.
This commit is contained in:
Gary Gregory 2019-12-21 17:28:30 -05:00
parent e910e9c908
commit 670a832d4d
7 changed files with 164 additions and 24 deletions

View File

@ -86,6 +86,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="update" dev="ggregory" due-to="Gary Gregory">Update POM parent: org.apache.commons:commons-parent 48 -> 50.</action> <action type="update" dev="ggregory" due-to="Gary Gregory">Update POM parent: org.apache.commons:commons-parent 48 -> 50.</action>
<action type="update" dev="ggregory" due-to="Peter Verhas">BooleanUtils Javadoc #469.</action> <action type="update" dev="ggregory" due-to="Peter Verhas">BooleanUtils Javadoc #469.</action>
<action type="update" dev="ggregory" due-to="Peter Verhas">Functions Javadoc #466.</action> <action type="update" dev="ggregory" due-to="Peter Verhas">Functions Javadoc #466.</action>
<action issue="LANG-1503" type="add" dev="ggregory" due-to="XenoAmess, Gary Gregory">Add factory methods to Pair classes with Map.Entry input. #454.</action>
</release> </release>
<release version="3.9" date="2019-04-09" description="New features and bug fixes. Requires Java 8, supports Java 9, 10, 11."> <release version="3.9" date="2019-04-09" description="New features and bug fixes. Requires Java 8, supports Java 9, 10, 11.">

View File

@ -16,6 +16,8 @@
*/ */
package org.apache.commons.lang3.tuple; package org.apache.commons.lang3.tuple;
import java.util.Map;
/** /**
* <p>An immutable pair consisting of two {@code Object} elements.</p> * <p>An immutable pair consisting of two {@code Object} elements.</p>
* *
@ -80,7 +82,7 @@ public static <L, R> ImmutablePair<L, R> nullPair() {
} }
/** /**
* <p>Obtains an immutable pair of two objects inferring the generic types.</p> * <p>Creates an immutable pair of two objects inferring the generic types.</p>
* *
* <p>This factory allows the pair to be created using inference to * <p>This factory allows the pair to be created using inference to
* obtain the generic types.</p> * obtain the generic types.</p>
@ -94,6 +96,32 @@ public static <L, R> ImmutablePair<L, R> nullPair() {
public static <L, R> ImmutablePair<L, R> of(final L left, final R right) { public static <L, R> ImmutablePair<L, R> of(final L left, final R right) {
return new ImmutablePair<>(left, right); return new ImmutablePair<>(left, right);
} }
/**
* <p>Creates an immutable pair from an existing pair.</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 pair the existing pair.
* @return a pair formed from the two parameters, not null
* @since 3.10
*/
public static <L, R> ImmutablePair<L, R> of(final Map.Entry<L, R> pair) {
final L left;
final R right;
if (pair != null) {
left = pair.getKey();
right = pair.getValue();
} else {
left = null;
right = null;
}
return new ImmutablePair<>(left, right);
}
/** Left object */ /** Left object */
public final L left; public final L left;
@ -112,7 +140,6 @@ public ImmutablePair(final L left, final R right) {
this.right = right; this.right = right;
} }
//-----------------------------------------------------------------------
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View File

@ -16,6 +16,8 @@
*/ */
package org.apache.commons.lang3.tuple; package org.apache.commons.lang3.tuple;
import java.util.Map;
/** /**
* <p>A mutable pair consisting of two {@code Object} elements.</p> * <p>A mutable pair consisting of two {@code Object} elements.</p>
* *
@ -56,7 +58,7 @@ public static <L, R> MutablePair<L, R>[] emptyArray() {
} }
/** /**
* <p>Obtains a mutable pair of two objects inferring the generic types.</p> * <p>Creates a mutable pair of two objects inferring the generic types.</p>
* *
* <p>This factory allows the pair to be created using inference to * <p>This factory allows the pair to be created using inference to
* obtain the generic types.</p> * obtain the generic types.</p>
@ -70,6 +72,31 @@ public static <L, R> MutablePair<L, R>[] emptyArray() {
public static <L, R> MutablePair<L, R> of(final L left, final R right) { public static <L, R> MutablePair<L, R> of(final L left, final R right) {
return new MutablePair<>(left, right); return new MutablePair<>(left, right);
} }
/**
* <p>Creates a mutable pair from an existing pair.</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 pair the existing pair.
* @return a pair formed from the two parameters, not null
*/
public static <L, R> MutablePair<L, R> of(final Map.Entry<L, R> pair) {
final L left;
final R right;
if (pair != null) {
left = pair.getKey();
right = pair.getValue();
} else {
left = null;
right = null;
}
return new MutablePair<>(left, right);
}
/** Left object */ /** Left object */
public L left; public L left;

View File

@ -89,7 +89,7 @@ public static <L, R> Pair<L, R>[] emptyArray() {
} }
/** /**
* <p>Obtains an immutable pair of two objects inferring the generic types.</p> * <p>Creates an immutable pair of two objects inferring the generic types.</p>
* *
* <p>This factory allows the pair to be created using inference to * <p>This factory allows the pair to be created using inference to
* obtain the generic types.</p> * obtain the generic types.</p>
@ -101,7 +101,23 @@ public static <L, R> Pair<L, R>[] emptyArray() {
* @return a pair formed from the two parameters, not null * @return a pair formed from the two parameters, not null
*/ */
public static <L, R> Pair<L, R> of(final L left, final R right) { public static <L, R> Pair<L, R> of(final L left, final R right) {
return new ImmutablePair<>(left, right); return ImmutablePair.of(left, right);
}
/**
* <p>Creates an immutable pair from an existing pair.</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 pair the existing pair.
* @return a pair formed from the two parameters, not null
* @since 3.10
*/
public static <L, R> Pair<L, R> of(final Map.Entry<L, R> pair) {
return ImmutablePair.of(pair);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View File

@ -54,16 +54,28 @@ public void testEmptyArrayGenerics() {
@Test @Test
public void testBasic() { public void testBasic() {
final ImmutablePair<Integer, String> pair = new ImmutablePair<>(0, "foo"); ImmutablePair<Integer, String> oldPair = new ImmutablePair<>(0, "foo");
assertEquals(0, pair.left.intValue()); ImmutablePair<Integer, String> nowPair;
assertEquals(0, pair.getLeft().intValue()); for (int i=0; i<4; i++) {
assertEquals("foo", pair.right); nowPair = ImmutablePair.of(oldPair);
assertEquals("foo", pair.getRight()); assertEquals(0, nowPair.left.intValue());
final ImmutablePair<Object, String> pair2 = new ImmutablePair<>(null, "bar"); assertEquals(0, nowPair.getLeft().intValue());
assertNull(pair2.left); assertEquals("foo", nowPair.right);
assertNull(pair2.getLeft()); assertEquals("foo", nowPair.getRight());
assertEquals("bar", pair2.right); assertEquals(oldPair, nowPair);
assertEquals("bar", pair2.getRight()); oldPair = nowPair;
}
ImmutablePair<Object, String> oldPair2 = new ImmutablePair<>(null, "bar");
ImmutablePair<Object, String> nowPair2;
for (int i=0; i<4; i++) {
nowPair2 = ImmutablePair.of(oldPair2);
assertNull(nowPair2.left);
assertNull(nowPair2.getLeft());
assertEquals("bar", nowPair2.right);
assertEquals("bar", nowPair2.getRight());
oldPair2 = nowPair2;
}
} }
@Test @Test
@ -121,7 +133,17 @@ public void testNullPairValue() {
} }
@Test @Test
public void testPairOf() { public void testPairOfMapEntry() {
final HashMap<Integer, String> map = new HashMap<>();
map.put(0, "foo");
final Entry<Integer, String> entry = map.entrySet().iterator().next();
final Pair<Integer, String> pair = ImmutablePair.of(entry);
assertEquals(entry.getKey(), pair.getLeft());
assertEquals(entry.getValue(), pair.getRight());
}
@Test
public void testPairOfObjects() {
final ImmutablePair<Integer, String> pair = ImmutablePair.of(0, "foo"); final ImmutablePair<Integer, String> pair = ImmutablePair.of(0, "foo");
assertEquals(0, pair.left.intValue()); assertEquals(0, pair.left.intValue());
assertEquals(0, pair.getLeft().intValue()); assertEquals(0, pair.getLeft().intValue());
@ -132,6 +154,9 @@ public void testPairOf() {
assertNull(pair2.getLeft()); assertNull(pair2.getLeft());
assertEquals("bar", pair2.right); assertEquals("bar", pair2.right);
assertEquals("bar", pair2.getRight()); assertEquals("bar", pair2.getRight());
ImmutablePair pair3 = ImmutablePair.of(null, null);
assertNull(pair3.left);
assertNull(pair3.right);
} }
@Test @Test

View File

@ -24,6 +24,8 @@
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map.Entry;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -47,12 +49,28 @@ public void testEmptyArrayGenerics() {
@Test @Test
public void testBasic() { public void testBasic() {
final MutablePair<Integer, String> pair = new MutablePair<>(0, "foo"); MutablePair<Integer, String> oldPair = new MutablePair<>(0, "foo");
assertEquals(0, pair.getLeft().intValue()); MutablePair<Integer, String> nowPair;
assertEquals("foo", pair.getRight()); for (int i=0; i<4; i++) {
final MutablePair<Object, String> pair2 = new MutablePair<>(null, "bar"); nowPair = MutablePair.of(oldPair);
assertNull(pair2.getLeft()); assertEquals(0, nowPair.left.intValue());
assertEquals("bar", pair2.getRight()); assertEquals(0, nowPair.getLeft().intValue());
assertEquals("foo", nowPair.right);
assertEquals("foo", nowPair.getRight());
assertEquals(oldPair, nowPair);
oldPair = nowPair;
}
MutablePair<Object, String> oldPair2 = new MutablePair<>(null, "bar");
MutablePair<Object, String> nowPair2;
for (int i=0; i<4; i++) {
nowPair2 = MutablePair.of(oldPair2);
assertNull(nowPair2.left);
assertNull(nowPair2.getLeft());
assertEquals("bar", nowPair2.right);
assertEquals("bar", nowPair2.getRight());
oldPair2 = nowPair2;
}
} }
@Test @Test
@ -88,13 +106,26 @@ public void testMutate() {
} }
@Test @Test
public void testPairOf() { public void testPairOfMapEntry() {
final HashMap<Integer, String> map = new HashMap<>();
map.put(0, "foo");
final Entry<Integer, String> entry = map.entrySet().iterator().next();
final Pair<Integer, String> pair = MutablePair.of(entry);
assertEquals(entry.getKey(), pair.getLeft());
assertEquals(entry.getValue(), pair.getRight());
}
@Test
public void testPairOfObjects() {
final MutablePair<Integer, String> pair = MutablePair.of(0, "foo"); final MutablePair<Integer, String> pair = MutablePair.of(0, "foo");
assertEquals(0, pair.getLeft().intValue()); assertEquals(0, pair.getLeft().intValue());
assertEquals("foo", pair.getRight()); assertEquals("foo", pair.getRight());
final MutablePair<Object, String> pair2 = MutablePair.of(null, "bar"); final MutablePair<Object, String> pair2 = MutablePair.of(null, "bar");
assertNull(pair2.getLeft()); assertNull(pair2.getLeft());
assertEquals("bar", pair2.getRight()); assertEquals("bar", pair2.getRight());
MutablePair pair3 = MutablePair.of(null, null);
assertNull(pair3.left);
assertNull(pair3.right);
} }
@Test @Test

View File

@ -104,7 +104,17 @@ public void testMapEntry() {
} }
@Test @Test
public void testPairOf() { public void testPairOfMapEntry() {
final HashMap<Integer, String> map = new HashMap<>();
map.put(0, "foo");
final Entry<Integer, String> entry = map.entrySet().iterator().next();
final Pair<Integer, String> pair = Pair.of(entry);
assertEquals(entry.getKey(), pair.getLeft());
assertEquals(entry.getValue(), pair.getRight());
}
@Test
public void testPairOfObjects() {
final Pair<Integer, String> pair = Pair.of(0, "foo"); final Pair<Integer, String> pair = Pair.of(0, "foo");
assertTrue(pair instanceof ImmutablePair<?, ?>); assertTrue(pair instanceof ImmutablePair<?, ?>);
assertEquals(0, ((ImmutablePair<Integer, String>) pair).left.intValue()); assertEquals(0, ((ImmutablePair<Integer, String>) pair).left.intValue());
@ -113,6 +123,9 @@ public void testPairOf() {
assertTrue(pair2 instanceof ImmutablePair<?, ?>); assertTrue(pair2 instanceof ImmutablePair<?, ?>);
assertNull(((ImmutablePair<Object, String>) pair2).left); assertNull(((ImmutablePair<Object, String>) pair2).left);
assertEquals("bar", ((ImmutablePair<Object, String>) pair2).right); assertEquals("bar", ((ImmutablePair<Object, String>) pair2).right);
Pair pair3 = Pair.of(null, null);
assertNull(pair3.getLeft());
assertNull(pair3.getRight());
} }
@Test @Test