Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8301042

Need methods to check null elements in an array or a collection

XMLWordPrintable

      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;
      }


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: