UnsupportedOperationException needs to be documented for the LinkedTransferQueue.drainTo function

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      The drainTo(Collection c) and drainTo(Collection c, int maxElements) https://docs.oracle.com/en/java/javase/25/docs/api//java.base/java/util/concurrent/LinkedTransferQueue.html#drainTo(java.util.Collection) may throw UnsupportedOperationException if the input collection c is unmodifiable (ex: List.of("hello", "world")). This behavior is not described in the Javadoc of this function. It should be documented because LinkedTransferQueue implements BlockingQueue, whose drainTo function is documented as may throw UnsupportedOperationException.

      ---------- BEGIN SOURCE ----------
      public static void testLinkedTransferQueueDrainToUOE() {
              System.out.println("\nTesting LinkedTransferQueue.drainTo with unmodifiable collection...");
              LinkedTransferQueue<String> queue = new LinkedTransferQueue<>();
              queue.add("element");
              
              Collection<String> unmodifiableSink = Collections.unmodifiableList(new ArrayList<>());
              
              try {
                  queue.drainTo(unmodifiableSink); // throw
                  System.out.println("Failed: drainTo did not throw UnsupportedOperationException");
              } catch (UnsupportedOperationException e) {
                  System.out.println("Passed: drainTo threw UnsupportedOperationException");
              }
          }

          public static void testLinkedTransferQueueDrainToMaxUOE() {
              System.out.println("\nTesting LinkedTransferQueue.drainTo(max) with unmodifiable collection...");
              LinkedTransferQueue<String> queue = new LinkedTransferQueue<>();
              queue.add("element");
              
              Collection<String> unmodifiableSink = Collections.unmodifiableList(new ArrayList<>());
              
              try {
                  queue.drainTo(unmodifiableSink, 10); // throw
                  System.out.println("Failed: drainTo(max) did not throw UnsupportedOperationException");
              } catch (UnsupportedOperationException e) {
                  System.out.println("Passed: drainTo(max) threw UnsupportedOperationException");
              }
          }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Add
      UnsupportedOperationException - if addition of elements is not supported by the specified collection
      ClassCastException - if the class of an element of this queue prevents it from being added to the specified collection
      to the javadoc of LinkedTransferQueue.drainTo for consistency.

      FREQUENCY :
      ALWAYS

            Assignee:
            Stuart Marks
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: