RFR: 8301042: Need methods to check null elements in an array or a collection [v5]

Stuart Marks smarks at openjdk.org
Sun Jan 29 21:59:17 UTC 2023


On Sun, 29 Jan 2023 10:39:22 GMT, Tingjun Yuan <duke at openjdk.org> wrote:

>> Adding the following methods to check the nullity of elements of an array or a collection:
>> 
>> 
>> java.util.Arrays:
>> public static <E> E[] requireNoNulls(E[] array)
>> public static <E> E[] requireNoNulls(E[] array, String message)
>> public static <E> E[] requireNoNulls(E[] array, IntFunction<String> messageGenerator)
>> public static <E> E[] requireNoNullsElseReplace(E[] array, IntFunction<? extends E> replaceFunction)
>> public static <E> E[] requireNoNullsCopied(E[] array)
>> public static <E> E[] requireNoNullsCopied(E[] array, IntFunction<String> messageGenerator)
>> public static <E> E[] requireNoNullsCopied(E[] array, String message)
>> public static <E> E[] requireNoNullsCopiedElseReplace(E[] array, IntFunction<? extends E> replaceFunction)
>> 
>> java.util.Collections:
>> public static <C extends Iterable<?>> C requireNoNulls(C collection)
>> public static <M extends Map<?, ?>> M requireNoNulls(M map)
>> public static <C extends Iterable<?>> C requireNoNulls(C collection, String message)
>> public static <M extends Map<?, ?>> M requireNoNulls(M map, String message)
>> public static <C extends Iterable<?>> C requireNoNulls(C collection, Supplier<String> messageSupplier)
>> public static <M extends Map<?, ?>> M requireNoNulls(M map, Supplier<String> messageSupplier)
>> public static <L extends List<?>> L requireNoNulls(L list, IntFunction<String> messageGenerator)
>> public static <E, L extends List<? super E>> L requireNoNullsElseReplace(L list, IntFunction<? extends E> replaceFunction)
>
> Tingjun Yuan has updated the pull request incrementally with one additional commit since the last revision:
> 
>   `requireNoNullsCopiedElseReplace` should modify `result`

I don't think we need these methods. Most of what they do can be done by the following:

    coll.forEach(Objects::requireNonNull)
    Arrays.asList(array).forEach(Objects::requireNonNull)

-------------

PR: https://git.openjdk.org/jdk/pull/12276


More information about the core-libs-dev mailing list