Refine search
/** * Stores the contents of a multiset in an output stream, as part of serialization. It does not * support concurrent multisets whose content may change while the method is running. * * <p>The serialized output consists of the number of distinct elements, the first element, its * count, the second element, its count, and so on. */ static <E> void writeMultiset(Multiset<E> multiset, ObjectOutputStream stream) throws IOException { int entryCount = multiset.entrySet().size(); stream.writeInt(entryCount); for (Multiset.Entry<E> entry : multiset.entrySet()) { stream.writeObject(entry.getElement()); stream.writeInt(entry.getCount()); } }
private static <E> ImmutableSortedMultiset<E> copyOfSortedEntries( Comparator<? super E> comparator, Collection<Entry<E>> entries) { if (entries.isEmpty()) { return emptyMultiset(comparator); } ImmutableList.Builder<E> elementsBuilder = new ImmutableList.Builder<E>(entries.size()); long[] cumulativeCounts = new long[entries.size() + 1]; int i = 0; for (Entry<E> entry : entries) { elementsBuilder.add(entry.getElement()); cumulativeCounts[i + 1] = cumulativeCounts[i] + entry.getCount(); i++; } return new RegularImmutableSortedMultiset<E>( new RegularImmutableSortedSet<E>(elementsBuilder.build(), comparator), cumulativeCounts, 0, entries.size()); }
/** * A sensible definition of {@link #pollFirstEntry()} in terms of {@code entrySet().iterator()}. * * <p>If you override {@link #entrySet()}, you may wish to override {@link #pollFirstEntry()} to * forward to this implementation. */ protected Entry<E> standardPollFirstEntry() { Iterator<Entry<E>> entryIterator = entrySet().iterator(); if (!entryIterator.hasNext()) { return null; } Entry<E> entry = entryIterator.next(); entry = Multisets.immutableEntry(entry.getElement(), entry.getCount()); entryIterator.remove(); return entry; }
@Override public E next() { if (remaining <= 0) { Entry<E> entry = entryIterator.next(); element = entry.getElement(); remaining = entry.getCount(); } remaining--; return element; } };
@Override public Entry<E> pollFirstEntry() { Iterator<Entry<E>> entryIterator = entryIterator(); if (entryIterator.hasNext()) { Entry<E> result = entryIterator.next(); result = Multisets.immutableEntry(result.getElement(), result.getCount()); entryIterator.remove(); return result; } return null; }
@Override public Entry<E> pollLastEntry() { Iterator<Entry<E>> entryIterator = descendingEntryIterator(); if (entryIterator.hasNext()) { Entry<E> result = entryIterator.next(); result = Multisets.immutableEntry(result.getElement(), result.getCount()); entryIterator.remove(); return result; } return null; }
@GwtIncompatible // not present in emulated superclass @Override int copyIntoArray(Object[] dst, int offset) { for (Multiset.Entry<E> entry : entrySet()) { Arrays.fill(dst, offset, offset + entry.getCount(), entry.getElement()); offset += entry.getCount(); } return offset; }
/** Resets the contents of sortedMultiset to have entries a, c, for the navigation tests. */ @SuppressWarnings("unchecked") // Needed to stop Eclipse whining private void resetWithHole() { List<E> container = new ArrayList<E>(); container.addAll(Collections.nCopies(a.getCount(), a.getElement())); container.addAll(Collections.nCopies(c.getCount(), c.getElement())); super.resetContainer(getSubjectGenerator().create(container.toArray())); sortedMultiset = (SortedMultiset<E>) getMultiset(); }
void expectRemoveZero(SortedMultiset<E> multiset, Entry<E> entry) { assertEquals(0, multiset.remove(entry.getElement(), entry.getCount())); assertFalse(multiset.remove(entry.getElement())); assertFalse(multiset.elementSet().remove(entry.getElement())); }
@Override public int count(@Nullable Object element) { for (Entry<E> entry : entrySet()) { if (Objects.equal(entry.getElement(), element)) { return entry.getCount(); } } return 0; }
private List<E> snapshot() { List<E> list = Lists.newArrayListWithExpectedSize(size()); for (Multiset.Entry<E> entry : entrySet()) { E element = entry.getElement(); for (int i = entry.getCount(); i > 0; i--) { list.add(element); } } return list; }
/** * Indicates whether an object equals this entry, following the behavior specified in {@link * Multiset.Entry#equals}. */ @Override public boolean equals(@Nullable Object object) { if (object instanceof Multiset.Entry) { Multiset.Entry<?> that = (Multiset.Entry<?>) object; return this.getCount() == that.getCount() && Objects.equal(this.getElement(), that.getElement()); } return false; }
@SuppressWarnings("unchecked") SerializedForm(SortedMultiset<E> multiset) { this.comparator = multiset.comparator(); int n = multiset.entrySet().size(); elements = (E[]) new Object[n]; counts = new int[n]; int i = 0; for (Entry<E> entry : multiset.entrySet()) { elements[i] = entry.getElement(); counts[i] = entry.getCount(); i++; } }
@Override public E next() { if (!hasNext()) { throw new NoSuchElementException(); } if (laterCount == 0) { currentEntry = entryIterator.next(); totalCount = laterCount = currentEntry.getCount(); } laterCount--; canRemove = true; return currentEntry.getElement(); }
/** * A sensible definition of {@link #firstEntry()} in terms of {@code entrySet().iterator()}. * * <p>If you override {@link #entrySet()}, you may wish to override {@link #firstEntry()} to * forward to this implementation. */ protected Entry<E> standardFirstEntry() { Iterator<Entry<E>> entryIterator = entrySet().iterator(); if (!entryIterator.hasNext()) { return null; } Entry<E> entry = entryIterator.next(); return Multisets.immutableEntry(entry.getElement(), entry.getCount()); }
/** * A sensible definition of {@link #lastEntry()} in terms of {@code * descendingMultiset().entrySet().iterator()}. * * <p>If you override {@link #descendingMultiset} or {@link #entrySet()}, you may wish to override * {@link #firstEntry()} to forward to this implementation. */ protected Entry<E> standardLastEntry() { Iterator<Entry<E>> entryIterator = descendingMultiset().entrySet().iterator(); if (!entryIterator.hasNext()) { return null; } Entry<E> entry = entryIterator.next(); return Multisets.immutableEntry(entry.getElement(), entry.getCount()); }
/** * A sensible, albeit inefficient, definition of {@link #count} in terms of {@link #entrySet}. If * you override {@link #entrySet}, you may wish to override {@link #count} to forward to this * implementation. * * @since 7.0 */ @Beta protected int standardCount(@Nullable Object object) { for (Entry<?> entry : this.entrySet()) { if (Objects.equal(entry.getElement(), object)) { return entry.getCount(); } } return 0; }
@Override public boolean contains(@Nullable Object o) { if (o instanceof Entry) { /* * The GWT compiler wrongly issues a warning here. */ @SuppressWarnings("cast") Entry<?> entry = (Entry<?>) o; if (entry.getCount() <= 0) { return false; } int count = multiset().count(entry.getElement()); return count == entry.getCount(); } return false; }
@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; }
void expectAddFailure(SortedMultiset<E> multiset, Entry<E> entry) { try { multiset.add(entry.getElement(), entry.getCount()); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } try { multiset.add(entry.getElement()); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } try { multiset.addAll(Collections.singletonList(entry.getElement())); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } }