Delegate implementation which cares about the element type.
(Multiset<E> multisetToModify, Multiset<?> occurrencesToRetain)
| 709 | */ |
| 710 | |
| 711 | private static <E> boolean retainOccurrencesImpl(Multiset<E> multisetToModify, Multiset<?> occurrencesToRetain) { |
| 712 | checkNotNull(multisetToModify); |
| 713 | checkNotNull(occurrencesToRetain); |
| 714 | // Avoiding ConcurrentModificationExceptions is tricky. |
| 715 | Iterator<Entry<E>> entryIterator = multisetToModify.entrySet().iterator(); |
| 716 | boolean changed = false; |
| 717 | while (entryIterator.hasNext()) { |
| 718 | Entry<E> entry = entryIterator.next(); |
| 719 | int retainCount = occurrencesToRetain.count(entry.getElement()); |
| 720 | if (retainCount == 0) { |
| 721 | entryIterator.remove(); |
| 722 | changed = true; |
| 723 | } else if (retainCount < entry.getCount()) { |
| 724 | multisetToModify.setCount(entry.getElement(), retainCount); |
| 725 | changed = true; |
| 726 | } |
| 727 | } |
| 728 | return changed; |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * For each occurrence of an element {@code e} in {@code occurrencesToRemove}, |
no test coverage detected