@Override public boolean setCount(E element, int oldCount, int newCount) { synchronized (mutex) { return delegate().setCount(element, oldCount, newCount); } }
@Override public int setCount(E element, int count) { synchronized (mutex) { return delegate().setCount(element, count); } }
private int setCount(E element, int count) { return getMultiset().setCount(element, count); } }
/** * Adds or removes the necessary occurrences of an element such that the element attains the * desired count. * * @param element the element to add or remove occurrences of * @param count the desired count of the element in this multiset * @return this {@code Builder} object * @throws NullPointerException if {@code element} is null * @throws IllegalArgumentException if {@code count} is negative */ @CanIgnoreReturnValue public Builder<E> setCount(E element, int count) { contents.setCount(checkNotNull(element), count); return this; }
@CanIgnoreReturnValue @Override public int setCount(E element, int count) { return delegate().setCount(element, count); }
@CanIgnoreReturnValue @Override public boolean setCount(E element, int oldCount, int newCount) { return delegate().setCount(element, oldCount, newCount); }
@Override public int setCount(E element, int count) { synchronized (mutex) { return delegate().setCount(element, count); } }
/** Delegate implementation which cares about the element type. */ private static <E> boolean retainOccurrencesImpl( Multiset<E> multisetToModify, Multiset<?> occurrencesToRetain) { checkNotNull(multisetToModify); checkNotNull(occurrencesToRetain); // Avoiding ConcurrentModificationExceptions is tricky. Iterator<Entry<E>> entryIterator = multisetToModify.entrySet().iterator(); boolean changed = false; while (entryIterator.hasNext()) { Entry<E> entry = entryIterator.next(); int retainCount = occurrencesToRetain.count(entry.getElement()); if (retainCount == 0) { entryIterator.remove(); changed = true; } else if (retainCount < entry.getCount()) { multisetToModify.setCount(entry.getElement(), retainCount); changed = true; } } return changed; }
private void assertSetCountNegativeOldCount() { try { getMultiset().setCount(e3(), -1, 1); fail("calling setCount() with a negative oldCount should throw IllegalArgumentException"); } catch (IllegalArgumentException expected) { } }
/** An implementation of {@link Multiset#setCount(Object, int, int)}. */ static <E> boolean setCountImpl(Multiset<E> self, E element, int oldCount, int newCount) { checkNonnegative(oldCount, "oldCount"); checkNonnegative(newCount, "newCount"); if (self.count(element) == oldCount) { self.setCount(element, newCount); return true; } else { return false; } }
@CanIgnoreReturnValue @Override public boolean setCount(E element, int oldCount, int newCount) { return delegate().setCount(element, oldCount, newCount); }
@CanIgnoreReturnValue @Override public int setCount(E element, int count) { return delegate().setCount(element, count); }
private boolean setCount(E element, int count) { return getMultiset().setCount(element, getMultiset().count(element), count); }
@SuppressWarnings("cast") @Override public boolean remove(Object object) { if (object instanceof Multiset.Entry) { Entry<?> entry = (Entry<?>) object; Object element = entry.getElement(); int entryCount = entry.getCount(); if (entryCount != 0) { // Safe as long as we never add a new entry, which we won't. @SuppressWarnings("unchecked") Multiset<Object> multiset = (Multiset) multiset(); return multiset.setCount(element, entryCount, 0); } } return false; }
@CanIgnoreReturnValue @Override public int setCount(E element, int count) { return delegate().setCount(element, count); }
@CanIgnoreReturnValue @Override public boolean setCount(E element, int oldCount, int newCount) { return delegate().setCount(element, oldCount, newCount); }
@CollectionFeature.Require(SUPPORTS_ADD) public void testSetCountConditional_oldCountTooLarge() { assertFalse( "setCount() with a too-large oldCount should return false", getMultiset().setCount(e0(), 2, 3)); expectUnchanged(); }
@SuppressWarnings("cast") @Override public boolean remove(Object object) { if (object instanceof Multiset.Entry) { Entry<?> entry = (Entry<?>) object; Object element = entry.getElement(); int entryCount = entry.getCount(); if (entryCount != 0) { // Safe as long as we never add a new entry, which we won't. @SuppressWarnings("unchecked") Multiset<Object> multiset = (Multiset) multiset(); return multiset.setCount(element, entryCount, 0); } } return false; }
@CollectionSize.Require(SEVERAL) @CollectionFeature.Require(SUPPORTS_ADD) public void testSetCountConditional_oldCountTooSmallNonzero() { initThreeCopies(); assertFalse( "setCount() with a too-small oldCount should return false", getMultiset().setCount(e0(), 1, 5)); expectContents(nCopies(3, e0())); }
@CollectionSize.Require(absent = ZERO) @CollectionFeature.Require(SUPPORTS_ADD) public void testSetCountConditional_oldCountTooSmallZero() { assertFalse( "setCount() with a too-small oldCount should return false", getMultiset().setCount(e0(), 0, 2)); expectUnchanged(); }