@Override public void forEachEntry(ObjIntConsumer<? super E> action) { checkNotNull(action); backingMap.forEach((element, count) -> action.accept(element, count.get())); }
public void testGetAndSet() { Count holder = new Count(10); assertEquals(10, holder.getAndSet(20)); assertEquals(20, holder.get()); }
@Override public int count(@Nullable Object element) { Count frequency = Maps.safeGet(backingMap, element); return (frequency == null) ? 0 : frequency.get(); }
public void testGetAndSet() { Count holder = new Count(10); assertEquals(10, holder.getAndSet(20)); assertEquals(20, holder.get()); }
@Override public void forEachEntry(ObjIntConsumer<? super E> action) { checkNotNull(action); backingMap.forEach((element, count) -> action.accept(element, count.get())); }
@CanIgnoreReturnValue @Override public int remove(@Nullable Object element, int occurrences) { if (occurrences == 0) { return count(element); } checkArgument(occurrences > 0, "occurrences cannot be negative: %s", occurrences); Count frequency = backingMap.get(element); if (frequency == null) { return 0; } int oldCount = frequency.get(); int numberRemoved; if (oldCount > occurrences) { numberRemoved = occurrences; } else { numberRemoved = oldCount; backingMap.remove(element); } frequency.add(-numberRemoved); size -= numberRemoved; return oldCount; }
/** * {@inheritDoc} * * @throws IllegalArgumentException if the call would result in more than {@link * Integer#MAX_VALUE} occurrences of {@code element} in this multiset. */ @CanIgnoreReturnValue @Override public int add(@Nullable E element, int occurrences) { if (occurrences == 0) { return count(element); } checkArgument(occurrences > 0, "occurrences cannot be negative: %s", occurrences); Count frequency = backingMap.get(element); int oldCount; if (frequency == null) { oldCount = 0; backingMap.put(element, new Count(occurrences)); } else { oldCount = frequency.get(); long newCount = (long) oldCount + (long) occurrences; checkArgument(newCount <= Integer.MAX_VALUE, "too many occurrences: %s", newCount); frequency.add(occurrences); } size += occurrences; return oldCount; }
@Override public int count(@NullableDecl Object element) { Count frequency = Maps.safeGet(backingMap, element); return (frequency == null) ? 0 : frequency.get(); }
public void testGet() { assertEquals(20, new Count(20).get()); }
@CanIgnoreReturnValue @Override public int remove(@NullableDecl Object element, int occurrences) { if (occurrences == 0) { return count(element); } checkArgument(occurrences > 0, "occurrences cannot be negative: %s", occurrences); Count frequency = backingMap.get(element); if (frequency == null) { return 0; } int oldCount = frequency.get(); int numberRemoved; if (oldCount > occurrences) { numberRemoved = occurrences; } else { numberRemoved = oldCount; backingMap.remove(element); } frequency.add(-numberRemoved); size -= numberRemoved; return oldCount; }
/** * {@inheritDoc} * * @throws IllegalArgumentException if the call would result in more than {@link * Integer#MAX_VALUE} occurrences of {@code element} in this multiset. */ @CanIgnoreReturnValue @Override public int add(@NullableDecl E element, int occurrences) { if (occurrences == 0) { return count(element); } checkArgument(occurrences > 0, "occurrences cannot be negative: %s", occurrences); Count frequency = backingMap.get(element); int oldCount; if (frequency == null) { oldCount = 0; backingMap.put(element, new Count(occurrences)); } else { oldCount = frequency.get(); long newCount = (long) oldCount + (long) occurrences; checkArgument(newCount <= Integer.MAX_VALUE, "too many occurrences: %s", newCount); frequency.add(occurrences); } size += occurrences; return oldCount; }
public void testSet() { Count holder = new Count(10); holder.set(20); assertEquals(20, holder.get()); } }
@Override public int count(@Nullable Object element) { try { Count frequency = backingMap.get(element); return (frequency == null) ? 0 : frequency.get(); } catch (NullPointerException e) { return 0; } catch (ClassCastException e) { return 0; } }
@Override public int count(@Nullable Object element) { Count frequency = Maps.safeGet(backingMap, element); return (frequency == null) ? 0 : frequency.get(); }
@Override public int count(@Nullable Object element) { Count frequency = Maps.safeGet(backingMap, element); return (frequency == null) ? 0 : frequency.get(); }
@Override public void forEachEntry(ObjIntConsumer<? super E> action) { checkNotNull(action); backingMap.forEach((element, count) -> action.accept(element, count.get())); }
@Override public void forEachEntry(ObjIntConsumer<? super E> action) { checkNotNull(action); backingMap.forEach((element, count) -> action.accept(element, count.get())); }
@Override public int count(@Nullable Object element) { Count frequency = Maps.safeGet(backingMap, element); return (frequency == null) ? 0 : frequency.get(); }
@Override public int count(@NullableDecl Object element) { Count frequency = Maps.safeGet(backingMap, element); return (frequency == null) ? 0 : frequency.get(); }