Copilot commented on code in PR #703:
URL: 
https://github.com/apache/commons-collections/pull/703#discussion_r3531824784


##########
src/main/java/org/apache/commons/collections4/MultiSetUtils.java:
##########
@@ -67,6 +124,100 @@ public static <E> MultiSet<E> predicatedMultiSet(final 
MultiSet<E> multiset,
         return PredicatedMultiSet.predicatedMultiSet(multiset, predicate);
     }
 
+    /**
+     * Returns a predicated (validating) sorted multiset backed by the given 
sorted
+     * multiset.
+     * <p>
+     * Only objects that pass the test in the given predicate can be added to
+     * the multiset. Trying to add an invalid object results in an
+     * IllegalArgumentException. It is important not to use the original 
multiset
+     * after invoking this method, as it is a backdoor for adding invalid
+     * objects.
+     * </p>
+     *
+     * @param <E> The element type
+     * @param multiset the sorted multiset to predicate, must not be null
+     * @param predicate the predicate for the multiset, must not be null
+     * @return a predicated sorted multiset backed by the given sorted multiset
+     * @throws NullPointerException if the SortedMultiSet or Predicate is null
+     * @since 4.6.0
+     */
+    public static <E> SortedMultiSet<E> predicatedSortedMultiSet(final 
SortedMultiSet<E> multiset,
+            final Predicate<? super E> predicate) {
+        return PredicatedSortedMultiSet.predicatedSortedMultiSet(multiset, 
predicate);
+    }
+
+    /**
+     * For each occurrence of an element in {@code occurrencesToRemove}, 
removes
+     * one occurrence of that element from {@code multiSetToModify}, if 
present.
+     * That is, if {@code occurrencesToRemove} contains {@code n} occurrences 
of
+     * an element, {@code multiSetToModify} will have {@code n} fewer 
occurrences,
+     * assuming it had at least {@code n} to begin with.
+     * <p>
+     * This method provides the cardinality-respecting behavior of
+     * {@link Bag#removeAll(java.util.Collection)} under an explicitly named
+     * method. To remove the occurrences of a plain collection, wrap it first,
+     * for example {@code removeOccurrences(multiSet, new 
HashMultiSet<>(coll))}.
+     * </p>
+     *
+     * @param multiSetToModify the multiset to remove occurrences from, must 
not be null
+     * @param occurrencesToRemove the occurrences to remove, must not be null
+     * @return {@code true} if {@code multiSetToModify} was changed as a 
result of this operation
+     * @throws NullPointerException if either MultiSet is null
+     * @since 4.6.0
+     */
+    public static boolean removeOccurrences(final MultiSet<?> 
multiSetToModify, final MultiSet<?> occurrencesToRemove) {
+        Objects.requireNonNull(multiSetToModify, "multiSetToModify");
+        Objects.requireNonNull(occurrencesToRemove, "occurrencesToRemove");
+        if (multiSetToModify == occurrencesToRemove) {
+            final boolean changed = !multiSetToModify.isEmpty();
+            multiSetToModify.clear();
+            return changed;
+        }
+        boolean changed = false;
+        for (final MultiSet.Entry<?> entry : occurrencesToRemove.entrySet()) {
+            if (multiSetToModify.remove(entry.getElement(), entry.getCount()) 
> 0) {
+                changed = true;
+            }
+        }

Review Comment:
   removeOccurrences iterates directly over occurrencesToRemove.entrySet() 
while mutating multiSetToModify. If occurrencesToRemove is backed by the same 
multiset (e.g., an unmodifiable view of multiSetToModify), this will typically 
throw ConcurrentModificationException due to the fail-fast iterator. Iterating 
over a defensive copy avoids this and still preserves the intended semantics.



##########
src/test/java/org/apache/commons/collections4/multiset/UnmodifiableSortedMultiSetTest.java:
##########
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.collections4.multiset;
+
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.Arrays;
+
+import org.apache.commons.collections4.SortedMultiSet;
+import org.apache.commons.collections4.Unmodifiable;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Extension of {@link AbstractSortedMultiSetTest} for exercising the
+ * {@link UnmodifiableSortedMultiSet} implementation.
+ */
+public class UnmodifiableSortedMultiSetTest<E> extends 
AbstractSortedMultiSetTest<E> {
+
+    @Override
+    public String getCompatibilityVersion() {
+        return "4.6";
+    }
+
+    @Override
+    public boolean isAddSupported() {
+        return false;
+    }
+
+    @Override
+    public boolean isRemoveSupported() {
+        return false;
+    }
+
+    @Override
+    public SortedMultiSet<E> makeFullCollection() {
+        final SortedMultiSet<E> multiset = new TreeMultiSet<>();
+        multiset.addAll(Arrays.asList(getFullElements()));
+        return UnmodifiableSortedMultiSet.unmodifiableSortedMultiSet(multiset);
+    }
+
+    @Override
+    public SortedMultiSet<E> makeObject() {
+        return UnmodifiableSortedMultiSet.unmodifiableSortedMultiSet(new 
TreeMultiSet<>());
+    }
+
+    @Test
+    void testAdd() {
+        final SortedMultiSet<E> multiset = makeFullCollection();
+        final SortedMultiSet<E> unmodifiableMultiSet = 
UnmodifiableSortedMultiSet.unmodifiableSortedMultiSet(multiset);
+        assertThrows(UnsupportedOperationException.class, () -> 
unmodifiableMultiSet.add((E) "One", 1));
+    }
+
+    @Test
+    void testDecorateFactory() {
+        final SortedMultiSet<E> multiset = makeFullCollection();
+        assertSame(multiset, 
UnmodifiableSortedMultiSet.unmodifiableSortedMultiSet(multiset));
+
+        assertThrows(NullPointerException.class, () -> 
UnmodifiableSortedMultiSet.unmodifiableSortedMultiSet(null));
+    }
+
+    @Test
+    void testEntrySet() {
+        final SortedMultiSet<E> multiset = makeFullCollection();
+        final SortedMultiSet<E> unmodifiableMultiSet = 
UnmodifiableSortedMultiSet.unmodifiableSortedMultiSet(multiset);
+        assertSame(unmodifiableMultiSet.entrySet().size(), 
multiset.entrySet().size());
+    }

Review Comment:
   assertSame compares object identity, not numeric equality. Here it boxes the 
int sizes to Integer and relies on caching; this is fragile and can fail if 
sizes exceed the cache range or if boxing behavior changes. Use assertEquals to 
validate the sizes are equal.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to