This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-collections.git
The following commit(s) were added to refs/heads/master by this push: new 01f5fdd0a [COLLECTIONS-860] Javadoc CollectionBag.add* to throw ClassCastException 01f5fdd0a is described below commit 01f5fdd0a9fa01aacf7a17910e942b9168b1998f Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Fri Oct 18 12:08:53 2024 -0400 [COLLECTIONS-860] Javadoc CollectionBag.add* to throw ClassCastException --- src/changes/changes.xml | 1 + .../java/org/apache/commons/collections4/Bag.java | 1 + .../commons/collections4/bag/CollectionBag.java | 19 ++++++++-------- .../collections4/bag/CollectionBagTest.java | 26 +++++++++++++--------- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index d37f3fbcb..dff6b1825 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -37,6 +37,7 @@ <action type="fix" dev="ggregory" due-to="Dávid Szigecsán">Use the Junit (Jupiter) API #518.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory, Alex Herbert">BloomFilterExtractor.flatten() should throw an exception instead of returning null.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory, Claude Warren">Improve WrappedBloomFilterTest. All tests now assert copy() the same way.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory, Daniele" issue="COLLECTIONS-860">Javadoc CollectionBag.add* to throw ClassCastException.</action> <!-- ADD --> <action type="add" dev="ggregory" due-to="Gary Gregory">LayerManager.Builder implements Supplier.</action> <action type="add" dev="ggregory" due-to="Gary Gregory, hemanth0525">Add CollectionUtils.duplicateList(Collection).</action> diff --git a/src/main/java/org/apache/commons/collections4/Bag.java b/src/main/java/org/apache/commons/collections4/Bag.java index 186934016..4b1b9d39d 100644 --- a/src/main/java/org/apache/commons/collections4/Bag.java +++ b/src/main/java/org/apache/commons/collections4/Bag.java @@ -78,6 +78,7 @@ public interface Bag<E> extends Collection<E> { * @param object the object to add * @param nCopies the number of copies to add * @return {@code true} if the object was not already in the {@code uniqueSet} + * @throws ClassCastException if the class of the specified element prevents it from being added to this collection */ boolean add(E object, int nCopies); diff --git a/src/main/java/org/apache/commons/collections4/bag/CollectionBag.java b/src/main/java/org/apache/commons/collections4/bag/CollectionBag.java index e5d3f137a..d46f1ac6d 100644 --- a/src/main/java/org/apache/commons/collections4/bag/CollectionBag.java +++ b/src/main/java/org/apache/commons/collections4/bag/CollectionBag.java @@ -67,14 +67,14 @@ public final class CollectionBag<E> extends AbstractBagDecorator<E> { } /** - * <em>(Change)</em> - * Adds one copy of the specified object to the Bag. + * <em>(Change)</em> Adds one copy of the specified object to the Bag. * <p> - * Since this method always increases the size of the bag, it - * will always return {@code true}. + * Since this method always increases the size of the bag, it will always return {@code true}. + * </p> * - * @param object the object to add + * @param object the object to add * @return {@code true}, always + * @throws ClassCastException if the class of the specified element prevents it from being added to this collection */ @Override public boolean add(final E object) { @@ -91,6 +91,7 @@ public final class CollectionBag<E> extends AbstractBagDecorator<E> { * @param object the object to add * @param count the number of copies to add * @return {@code true}, always + * @throws ClassCastException if the class of the specified element prevents it from being added to this collection */ @Override public boolean add(final E object, final int count) { @@ -98,8 +99,6 @@ public final class CollectionBag<E> extends AbstractBagDecorator<E> { return true; } - // Collection interface - @Override public boolean addAll(final Collection<? extends E> coll) { boolean changed = false; @@ -145,6 +144,7 @@ public final class CollectionBag<E> extends AbstractBagDecorator<E> { * <p> * This will also remove the object from the {@link #uniqueSet()} if the * bag contains no occurrence anymore of the object after this operation. + * </p> * * @param object the object to remove * @return {@code true} if this call changed the collection @@ -190,6 +190,7 @@ public final class CollectionBag<E> extends AbstractBagDecorator<E> { * If it's not contained, it's removed from this bag. As a consequence, * it is advised to use a collection type for {@code coll} that provides * a fast (e.g. O(1)) implementation of {@link Collection#contains(Object)}. + * </p> * * @param coll the collection to retain * @return {@code true} if this call changed the collection @@ -211,10 +212,8 @@ public final class CollectionBag<E> extends AbstractBagDecorator<E> { return decorated().retainAll(null); } - // Bag interface - /** - * Write the collection out using a custom routine. + * Writes the collection out using a custom routine. * * @param out the output stream * @throws IOException if an error occurs while writing to the stream diff --git a/src/test/java/org/apache/commons/collections4/bag/CollectionBagTest.java b/src/test/java/org/apache/commons/collections4/bag/CollectionBagTest.java index 6c3022291..40d2ef889 100644 --- a/src/test/java/org/apache/commons/collections4/bag/CollectionBagTest.java +++ b/src/test/java/org/apache/commons/collections4/bag/CollectionBagTest.java @@ -18,18 +18,19 @@ package org.apache.commons.collections4.bag; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Comparator; import org.apache.commons.collections4.Bag; import org.apache.commons.collections4.Predicate; import org.apache.commons.collections4.collection.AbstractCollectionTest; import org.apache.commons.collections4.functors.NonePredicate; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; /** @@ -40,15 +41,6 @@ import org.junit.jupiter.api.Test; */ public class CollectionBagTest<T> extends AbstractCollectionTest<T> { - @Test - @Disabled - public void testAdd_Always() throws Throwable { - TreeBag<Predicate<Object>> treeBagOfPredicateOfObject = new TreeBag<>(); - CollectionBag<Predicate<Object>> collectionBagOfPredicateOfObject = new CollectionBag<>(treeBagOfPredicateOfObject); - Predicate<Object> predicate0 = NonePredicate.nonePredicate(collectionBagOfPredicateOfObject); - collectionBagOfPredicateOfObject.add(predicate0, 24); - } - /** * JUnit constructor. */ @@ -93,6 +85,20 @@ public class CollectionBagTest<T> extends AbstractCollectionTest<T> { return CollectionBag.collectionBag(new HashBag<>()); } + @Test + public void testAdd_Predicate_ComparatorCustom() throws Throwable { + final TreeBag<Predicate<Object>> treeBagOfPredicateOfObject = new TreeBag<>(Comparator.comparing(Predicate::toString)); + final CollectionBag<Predicate<Object>> collectionBagOfPredicateOfObject = new CollectionBag<>(treeBagOfPredicateOfObject); + collectionBagOfPredicateOfObject.add(NonePredicate.nonePredicate(collectionBagOfPredicateOfObject), 24); + } + + @Test + public void testAdd_Predicate_ComparatorDefault() throws Throwable { + final TreeBag<Predicate<Object>> treeBagOfPredicateOfObject = new TreeBag<>(); + final CollectionBag<Predicate<Object>> collectionBagOfPredicateOfObject = new CollectionBag<>(treeBagOfPredicateOfObject); + assertThrows(ClassCastException.class, () -> collectionBagOfPredicateOfObject.add(NonePredicate.nonePredicate(collectionBagOfPredicateOfObject), 24)); + } + // public void testCreate() throws Exception { // resetEmpty(); // writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/CollectionBag.emptyCollection.version4.obj");