Added ImmutablePair factory methods left() and right().
This commit is contained in:
parent
744da0e255
commit
341aaa797d
|
@ -79,6 +79,7 @@ The <action> type attribute can be add,update,fix,remove.
|
|||
<action issue="LANG-1556" type="add" dev="ggregory" due-to="XenoAmess">Use Java 8 lambdas and Map operations.</action>
|
||||
<action issue="LANG-1565" type="add" dev="ggregory" due-to="XenoAmess">Change removeLastFieldSeparator to use endsWith #550.</action>
|
||||
<action issue="LANG-1557" type="add" dev="ggregory" due-to="XenoAmess, Gary Gregory">Change a Pattern to a static final field, for not letting it compile each time the function invoked. #542.</action>
|
||||
<action type="add" dev="ggregory">Added ImmutablePair factory methods left() and right().</action>
|
||||
</release>
|
||||
|
||||
<release version="3.10" date="2020-03-22" description="New features and bug fixes. Requires Java 8, supports Java 9, 10, 11.">
|
||||
|
|
|
@ -69,6 +69,21 @@ public final class ImmutablePair<L, R> extends Pair<L, R> {
|
|||
return (ImmutablePair<L, R>[]) EMPTY_ARRAY;
|
||||
}
|
||||
|
||||
/**
|
||||
* <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
|
||||
* obtain the generic types.</p>
|
||||
*
|
||||
* @param <L> the left element type
|
||||
* @param left the left element, may be null
|
||||
* @return a pair formed from the two parameters, not null
|
||||
* @since 3.11
|
||||
*/
|
||||
public static <L, R> Pair<L, R> left(final L left) {
|
||||
return ImmutablePair.of(left, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an immutable pair of nulls.
|
||||
*
|
||||
|
@ -122,6 +137,21 @@ public final class ImmutablePair<L, R> extends Pair<L, R> {
|
|||
return new ImmutablePair<>(left, right);
|
||||
}
|
||||
|
||||
/**
|
||||
* <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
|
||||
* obtain the generic types.</p>
|
||||
*
|
||||
* @param <R> the right element type
|
||||
* @param right the right element, may be null
|
||||
* @return a pair formed from the two parameters, not null
|
||||
* @since 3.11
|
||||
*/
|
||||
public static <L, R> Pair<L, R> right(final R right) {
|
||||
return ImmutablePair.of(null, right);
|
||||
}
|
||||
|
||||
/** Left object */
|
||||
public final L left;
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
|||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
@ -209,4 +210,40 @@ public class ImmutablePairTest {
|
|||
assertEquals(item.getLeft() + "" + item.getRight(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComparableLeftOnly() {
|
||||
final Pair<String, String> pair1 = ImmutablePair.left("A");
|
||||
final Pair<String, String> pair2 = ImmutablePair.left("B");
|
||||
assertEquals("A", pair1.getLeft());
|
||||
assertEquals("B", pair2.getLeft());
|
||||
assertEquals(0, pair1.compareTo(pair1));
|
||||
assertTrue(pair1.compareTo(pair2) < 0);
|
||||
assertEquals(0, pair2.compareTo(pair2));
|
||||
assertTrue(pair2.compareTo(pair1) > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComparableRightOnly() {
|
||||
final Pair<String, String> pair1 = ImmutablePair.right("A");
|
||||
final Pair<String, String> pair2 = ImmutablePair.right("B");
|
||||
assertEquals("A", pair1.getRight());
|
||||
assertEquals("B", pair2.getRight());
|
||||
assertEquals(0, pair1.compareTo(pair1));
|
||||
assertTrue(pair1.compareTo(pair2) < 0);
|
||||
assertEquals(0, pair2.compareTo(pair2));
|
||||
assertTrue(pair2.compareTo(pair1) > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToStringLeft() {
|
||||
final Pair<String, String> pair = ImmutablePair.left("Key");
|
||||
assertEquals("(Key,null)", pair.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToStringRight() {
|
||||
final Pair<String, String> pair = ImmutablePair.right("Value");
|
||||
assertEquals("(null,Value)", pair.toString());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue