Skip to content

Commit 61dbccf

Browse files
etellmanGoogle Java Core Libraries
authored and
Google Java Core Libraries
committedSep 12, 2023
Make UnmodifiableMultiset.removeIf(Predicate) unsupported
Even if there's nothing to remove, `removeIf(Predicate)` should throw an `UnsupportedOperationException`. This is theoretically an incompatible change, in that some existing code might have been calling `removeIf` with a predicate that is never true. None of Google's internal uses show this problem in tests. Fixes #6702. Closes #6703. RELNOTES=`Multisets.unmodifiableMultiset(set).removeIf(predicate)` now throws an exception always, even if nothing matches `predicate`. PiperOrigin-RevId: 564750701
1 parent a770aea commit 61dbccf

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed
 

‎guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableCollection.java

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Collections;
2626
import java.util.Iterator;
2727
import java.util.Spliterator;
28+
import java.util.function.Predicate;
2829
import org.checkerframework.checker.nullness.qual.Nullable;
2930

3031
/**
@@ -64,6 +65,10 @@ public final boolean removeAll(Collection<?> oldElements) {
6465
throw new UnsupportedOperationException();
6566
}
6667

68+
public final boolean removeIf(Predicate<? super E> predicate) {
69+
throw new UnsupportedOperationException();
70+
}
71+
6772
public final boolean retainAll(Collection<?> elementsToKeep) {
6873
throw new UnsupportedOperationException();
6974
}

‎guava-testlib/src/com/google/common/collect/testing/google/UnmodifiableCollectionTests.java

+5
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ public static <E> void assertMultisetIsUnmodifiable(Multiset<E> multiset, E samp
224224
}
225225
assertCollectionsAreEquivalent(multiset, copy);
226226

227+
try {
228+
multiset.removeIf(x -> false);
229+
fail("removeIf(Predicate) succeeded on unmodifiable collection");
230+
} catch (UnsupportedOperationException expected) {
231+
}
227232
assertCollectionsAreEquivalent(multiset, copy);
228233

229234
assertSetIsUnmodifiable(multiset.elementSet(), sampleElement);

‎guava/src/com/google/common/collect/Multisets.java

+5
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ public boolean removeAll(Collection<?> elementsToRemove) {
195195
throw new UnsupportedOperationException();
196196
}
197197

198+
@Override
199+
public boolean removeIf(java.util.function.Predicate<? super E> filter) {
200+
throw new UnsupportedOperationException();
201+
}
202+
198203
@Override
199204
public boolean retainAll(Collection<?> elementsToRetain) {
200205
throw new UnsupportedOperationException();

0 commit comments

Comments
 (0)
Please sign in to comment.