[LANG-1458] Add EMPTY_ARRAY constants to classes in

org.apache.commons.lang3.tuple
This commit is contained in:
Gary Gregory 2019-05-10 09:10:40 -04:00
parent 992e7cf6c8
commit 13354cd132
8 changed files with 121 additions and 4 deletions

View File

@ -53,7 +53,7 @@ public final class ImmutablePair<L, R> extends Pair<L, R> {
* @since 3.10.
*/
@SuppressWarnings("unchecked")
public static final <L, R> ImmutablePair<L, R>[] emptyArray() {
public static <L, R> ImmutablePair<L, R>[] emptyArray() {
return (ImmutablePair<L, R>[]) EMPTY_ARRAY;
}

View File

@ -55,7 +55,7 @@ public final class ImmutableTriple<L, M, R> extends Triple<L, M, R> {
* @since 3.10.
*/
@SuppressWarnings("unchecked")
public static final <L, M, R> ImmutableTriple<L, M, R>[] emptyArray() {
public static <L, M, R> ImmutableTriple<L, M, R>[] emptyArray() {
return (ImmutableTriple<L, M, R>[]) EMPTY_ARRAY;
}

View File

@ -48,7 +48,7 @@ public class MutablePair<L, R> extends Pair<L, R> {
* @since 3.10.
*/
@SuppressWarnings("unchecked")
public static final <L, R> MutablePair<L, R>[] emptyArray() {
public static <L, R> MutablePair<L, R>[] emptyArray() {
return (MutablePair<L, R>[]) EMPTY_ARRAY;
}

View File

@ -50,7 +50,7 @@ public class MutableTriple<L, M, R> extends Triple<L, M, R> {
* @since 3.10.
*/
@SuppressWarnings("unchecked")
public static final <L, M, R> MutableTriple<L, M, R>[] emptyArray() {
public static <L, M, R> MutableTriple<L, M, R>[] emptyArray() {
return (MutableTriple<L, M, R>[]) EMPTY_ARRAY;
}

View File

@ -43,6 +43,51 @@ public abstract class Pair<L, R> implements Map.Entry<L, R>, Comparable<Pair<L,
/** Serialization version */
private static final long serialVersionUID = 4954918890077093841L;
private static final class PairAdapter<L, R> extends Pair<L, R> {
private static final long serialVersionUID = 1L;
@Override
public L getLeft() {
return null;
}
@Override
public R getRight() {
return null;
}
@Override
public R setValue(R value) {
return null;
}
}
/**
* An empty array.
* <p>
* Consider using {@link #emptyArray()} to avoid generics warnings.
* </p>
*
* @since 3.10.
*/
public static final Pair<?, ?>[] EMPTY_ARRAY = new PairAdapter[0];
/**
* Returns the empty array singleton that can be assigned without compiler warning.
*
* @param <L> the left element type
* @param <R> the right element type
* @return the empty array singleton that can be assigned without compiler warning.
*
* @since 3.10.
*/
@SuppressWarnings("unchecked")
public static <L, R> Pair<L, R>[] emptyArray() {
return (Pair<L, R>[]) EMPTY_ARRAY;
}
/**
* <p>Obtains an immutable pair of two objects inferring the generic types.</p>
*

View File

@ -42,6 +42,52 @@ public abstract class Triple<L, M, R> implements Comparable<Triple<L, M, R>>, Se
/** Serialization version */
private static final long serialVersionUID = 1L;
private static final class TripleAdapter<L, M, R> extends Triple<L, M, R> {
private static final long serialVersionUID = 1L;
@Override
public L getLeft() {
return null;
}
@Override
public M getMiddle() {
return null;
}
@Override
public R getRight() {
return null;
}
}
/**
* An empty array.
* <p>
* Consider using {@link #emptyArray()} to avoid generics warnings.
* </p>
*
* @since 3.10.
*/
public static final Triple<?, ?, ?>[] EMPTY_ARRAY = new TripleAdapter[0];
/**
* Returns the empty array singleton that can be assigned without compiler warning.
*
* @param <L> the left element type
* @param <M> the middle element type
* @param <R> the right element type
* @return the empty array singleton that can be assigned without compiler warning.
*
* @since 3.10.
*/
@SuppressWarnings("unchecked")
public static <L, M, R> Triple<L, M, R>[] emptyArray() {
return (Triple<L, M, R>[]) EMPTY_ARRAY;
}
/**
* <p>Obtains an immutable triple of three objects inferring the generic types.</p>
*

View File

@ -33,6 +33,19 @@ import org.junit.jupiter.api.Test;
*/
public class PairTest {
@Test
public void testEmptyArrayLength() {
@SuppressWarnings("unchecked")
final Pair<Integer, String>[] empty = (Pair<Integer, String>[]) Pair.EMPTY_ARRAY;
assertEquals(0, empty.length);
}
@Test
public void testEmptyArrayGenerics() {
final Pair<Integer, String>[] empty = Pair.emptyArray();
assertEquals(0, empty.length);
}
@Test
public void testComparable1() {
final Pair<String, String> pair1 = Pair.of("A", "D");

View File

@ -30,6 +30,19 @@ import org.junit.jupiter.api.Test;
*/
public class TripleTest {
@Test
public void testEmptyArrayLength() {
@SuppressWarnings("unchecked")
final Triple<Integer, String, Boolean>[] empty = (Triple<Integer, String, Boolean>[]) Triple.EMPTY_ARRAY;
assertEquals(0, empty.length);
}
@Test
public void testEmptyArrayGenerics() {
final Triple<Integer, String, Boolean>[] empty = Triple.emptyArray();
assertEquals(0, empty.length);
}
@Test
public void testComparable1() {
final Triple<String, String, String> triple1 = Triple.of("A", "D", "A");