Reuse constant for immutable cases.

This commit is contained in:
Gary Gregory 2021-12-08 08:56:40 -05:00
parent 766a8daa99
commit bdaaeb5b1b
2 changed files with 5 additions and 14 deletions

View File

@ -51,7 +51,7 @@ public final class ImmutablePair<L, R> extends Pair<L, R> {
*/ */
// This is not defined with generics to avoid warnings in call sites. // This is not defined with generics to avoid warnings in call sites.
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
private static final ImmutablePair NULL = of(null, null); private static final ImmutablePair NULL = new ImmutablePair<>(null, null);
/** Serialization version */ /** Serialization version */
private static final long serialVersionUID = 4954918890077093841L; private static final long serialVersionUID = 4954918890077093841L;
@ -111,7 +111,7 @@ public final class ImmutablePair<L, R> extends Pair<L, R> {
* @return a pair formed from the two parameters, not null * @return a pair formed from the two parameters, not null
*/ */
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 left != null || right != null ? new ImmutablePair<>(left, right) : NULL;
} }
/** /**
@ -127,16 +127,7 @@ public final class ImmutablePair<L, R> extends Pair<L, R> {
* @since 3.10 * @since 3.10
*/ */
public static <L, R> ImmutablePair<L, R> of(final Map.Entry<L, R> pair) { public static <L, R> ImmutablePair<L, R> of(final Map.Entry<L, R> pair) {
final L left; return pair != null ? new ImmutablePair<>(pair.getKey(), pair.getValue()) : ImmutablePair.NULL;
final R right;
if (pair != null) {
left = pair.getKey();
right = pair.getValue();
} else {
left = null;
right = null;
}
return new ImmutablePair<>(left, right);
} }
/** /**

View File

@ -51,7 +51,7 @@ public final class ImmutableTriple<L, M, R> extends Triple<L, M, R> {
*/ */
// This is not defined with generics to avoid warnings in call sites. // This is not defined with generics to avoid warnings in call sites.
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
private static final ImmutableTriple NULL = of(null, null, null); private static final ImmutableTriple NULL = new ImmutableTriple<>(null, null, null);
/** Serialization version */ /** Serialization version */
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ -99,7 +99,7 @@ public final class ImmutableTriple<L, M, R> extends Triple<L, M, R> {
* @return a triple formed from the three parameters, not null * @return a triple formed from the three parameters, not null
*/ */
public static <L, M, R> ImmutableTriple<L, M, R> of(final L left, final M middle, final R right) { public static <L, M, R> ImmutableTriple<L, M, R> of(final L left, final M middle, final R right) {
return new ImmutableTriple<>(left, middle, right); return left != null | middle != null || right != null ? new ImmutableTriple<>(left, middle, right) : NULL;
} }
/** /**