From ea11bf98b05e47d747f634e5d4a479c9d970ccde Mon Sep 17 00:00:00 2001 From: Bobby Wertman Date: Sun, 20 Jan 2019 09:07:01 -0500 Subject: [PATCH] [COLLECTIONS-706] Set Entry count to 0 after remove. --- src/changes/changes.xml | 3 +++ .../multiset/AbstractMapMultiSet.java | 1 + .../multiset/AbstractMultiSetTest.java | 16 ++++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index de297c097..248b32781 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -45,6 +45,9 @@ Add SetUtils.unmodifiableSet(T... items) method. + + MultiSet.Entry::getCount() isn't 0 after removing the last element. + diff --git a/src/main/java/org/apache/commons/collections4/multiset/AbstractMapMultiSet.java b/src/main/java/org/apache/commons/collections4/multiset/AbstractMapMultiSet.java index 97bcc3a76..0a92d6460 100644 --- a/src/main/java/org/apache/commons/collections4/multiset/AbstractMapMultiSet.java +++ b/src/main/java/org/apache/commons/collections4/multiset/AbstractMapMultiSet.java @@ -265,6 +265,7 @@ public abstract class AbstractMapMultiSet extends AbstractMultiSet { } else { map.remove(object); size -= mut.value; + mut.value = 0; } } return oldCount; diff --git a/src/test/java/org/apache/commons/collections4/multiset/AbstractMultiSetTest.java b/src/test/java/org/apache/commons/collections4/multiset/AbstractMultiSetTest.java index 579aff287..1ab2dbc23 100644 --- a/src/test/java/org/apache/commons/collections4/multiset/AbstractMultiSetTest.java +++ b/src/test/java/org/apache/commons/collections4/multiset/AbstractMultiSetTest.java @@ -462,6 +462,22 @@ public abstract class AbstractMultiSetTest extends AbstractCollectionTest assertEquals(false, it2.hasNext()); } + @SuppressWarnings("unchecked") + public void testMultiSetEntrySetUpdatedToZero() { + if (!isAddSupported()) { + return; + } + final MultiSet multiset = makeObject(); + multiset.add((T) "A"); + multiset.add((T) "A"); + final MultiSet.Entry entry = multiset.entrySet().iterator().next(); + assertEquals(2, entry.getCount()); + multiset.remove((T) "A"); + assertEquals(1, entry.getCount()); + multiset.remove((T) "A"); + assertEquals(0, entry.getCount()); + } + @SuppressWarnings("unchecked") public void testMultiSetToArray() { if (!isAddSupported()) {