-
Bug
-
Resolution: Fixed
-
P3
-
None
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.
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.
- csr for
-
JDK-8313649 Collections mutator methods should all be marked as optional operations
- Closed
- duplicates
-
JDK-8226726 Map.remove(Object key, Object value) should be marked as optional operation
- Closed
-
JDK-7042037 Make NavigableSet's contract permit immutable implementations
- Closed
- relates to
-
JDK-8159404 throw UnsupportedOperationException unconditionally for mutator methods
- Resolved
-
JDK-8313644 ConcurrentMap should use {@inheritDoc} consistently
- Open
-
JDK-8313647 add anchor for (optional operation)
- Open
(1 relates to, 2 links to)