Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ public void remove() {
if (!canRemove) {
throw new IllegalStateException("Iterator remove() can only be called once after next()");
}
final int count = last.getCount();
decorated.remove();
parent.size -= count;
parent.modCount++;
last = null;
canRemove = false;
}
Expand Down Expand Up @@ -267,7 +270,8 @@ public void remove() {
}
final int count = parent.getCount(lastElement);
super.remove();
parent.remove(lastElement, count);
parent.size -= count;
parent.modCount++;
lastElement = null;
canRemove = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
*/
package org.apache.commons.collections4.multiset;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.InvalidObjectException;
import java.util.Iterator;

import org.apache.commons.collections4.MultiSet;
import org.junit.jupiter.api.Test;
Expand All @@ -44,6 +47,26 @@ public MultiSet<T> makeObject() {
return new HashMultiSet<>();
}

@Test
void testViewIteratorRemoveKeepsSizeConsistent() {
final HashMultiSet<String> multiset = new HashMultiSet<>();
multiset.add("a", 3);
final Iterator<String> unique = multiset.uniqueSet().iterator();
unique.next();
unique.remove();
assertEquals(0, multiset.size());
assertTrue(multiset.isEmpty());
assertEquals(0, multiset.toArray().length);

multiset.add("b", 4);
final Iterator<MultiSet.Entry<String>> entries = multiset.entrySet().iterator();
entries.next();
entries.remove();
assertEquals(0, multiset.size());
assertTrue(multiset.isEmpty());
assertEquals(0, multiset.toArray().length);
}

@Test
void testDeserializeRejectsNonPositiveCount() throws Exception {
final int marker = 0x11223344;
Expand Down
Loading