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

Collections mutator methods should all be marked as optional operations

XMLWordPrintable

      In order to reduce interface proliferation, some of the methods on the collection interfaces are designated as optional operations. Most of these are mutator methods.

      Unfortunately, some of the mutator methods that were added recently, most notably the default methods added in Java 8, are not specified to be optional. In fact, they are optional, and they'll throw UnsupportedOperationException when called on an unmodifiable collection. Strictly speaking, then, this is a violation of the specification. The specifications of such methods should be modified to say that they are optional operations, and UnsupportedOperationException should be specified to be thrown if the implementing collection doesn't support that operation.

      The list of methods that need to be updated is as follows:

      Collection.removeIf
      List.replaceAll
      List.sort
      NavigableSet.pollFirst
      NavigableSet.pollLast

      Map.compute
      Map.computeIfAbsent
      Map.computeIfPresent
      Map.merge
      Map.putIfAbsent
      Map.remove (2-arg)
      Map.replace (2-arg)
      Map.replace (3-arg)
      Map.replaceAll
      NavigableMap.pollFirstEntry
      NavigableMap.pollLastEntry

      Most cases simply need "(optional operation)" to be added to the end of the leading sentence fragment in the specification.

      In a few cases, the doc for UnsupportedOperationException should be augmented to allow UOE to be thrown if the implementation doesn't support the operation in general.

      Navigable{Map,Set}.poll* need the most work.

            smarks Stuart Marks
            smarks Stuart Marks
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: