-
Enhancement
-
Resolution: Not an Issue
-
P4
-
None
-
21
-
generic
-
generic
A DESCRIPTION OF THE PROBLEM :
We already have Objects.requireNonNull family to simplify null-checking of a single reference. However, many classes and methods require an array or a collection containing no nulls, and it have no such convenient method yet to check that. So it would be great to add these methods doing that.
Implementation is simple for these methods:
public static <E> E[] requireNoNulls(E[] array) {
Objects.requireNonNull(array);
for (E e : array) {
Objects.requireNonNull(e);
}
return array;
}
By the way, you can also provide a method returning a defensive copy of a null-free array to avoid TOCTOU:
public static <E> E[] requireNoNullsAndCopy(E[] array) {
Objects.requireNonNull(array);
array = array.clone();
for (E e : array) {
Objects.requireNonNull(e);
}
return array;
}
We already have Objects.requireNonNull family to simplify null-checking of a single reference. However, many classes and methods require an array or a collection containing no nulls, and it have no such convenient method yet to check that. So it would be great to add these methods doing that.
Implementation is simple for these methods:
public static <E> E[] requireNoNulls(E[] array) {
Objects.requireNonNull(array);
for (E e : array) {
Objects.requireNonNull(e);
}
return array;
}
By the way, you can also provide a method returning a defensive copy of a null-free array to avoid TOCTOU:
public static <E> E[] requireNoNullsAndCopy(E[] array) {
Objects.requireNonNull(array);
array = array.clone();
for (E e : array) {
Objects.requireNonNull(e);
}
return array;
}
- links to
-
Review openjdk/jdk/12276