@CollectionFeature.Require(KNOWN_ORDER) public void testForEachEntryOrdered() { List<Entry<E>> expected = new ArrayList<>(getMultiset().entrySet()); List<Entry<E>> actual = new ArrayList<>(); getMultiset() .forEachEntry((element, count) -> actual.add(Multisets.immutableEntry(element, count))); assertEquals(expected, actual); }
@Override public Multiset<K> keys() { Multiset<K> result = keys; if (result == null) { keys = result = Multisets.unmodifiableMultiset(delegate.keys()); } return result; }
/** * Creates a new {@code HashMultiset} containing the specified elements. * * <p>This implementation is highly efficient when {@code elements} is itself a {@link Multiset}. * * @param elements the elements that the multiset should contain */ public static <E> HashMultiset<E> create(Iterable<? extends E> elements) { HashMultiset<E> multiset = create(Multisets.inferDistinctElements(elements)); Iterables.addAll(multiset, elements); return multiset; }
/** An implementation of {@link Multiset#addAll}. */ static <E> boolean addAllImpl(Multiset<E> self, Collection<? extends E> elements) { checkNotNull(self); checkNotNull(elements); if (elements instanceof Multiset) { return addAllImpl(self, cast(elements)); } else if (elements.isEmpty()) { return false; } else { return Iterators.addAll(self, elements.iterator()); } }
/** * {@inheritDoc} * * <p>This implementation returns {@code true} if {@code object} is a multiset * of the same size and if, for each element, the two multisets have the same * count. */ @Override public boolean equals(@Nullable Object object) { return Multisets.equalsImpl(this, object); }
@CollectionSize.Require(SEVERAL) public void testKeys() { resetContainer( Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v1()), Helpers.mapEntry(k1(), v0())); Multiset<K> keys = multimap().keys(); assertEquals(2, keys.count(k0())); assertEquals(1, keys.count(k1())); assertEquals(3, keys.size()); assertContainsAllOf(keys, k0(), k1()); assertContainsAllOf( keys.entrySet(), Multisets.immutableEntry(k0(), 2), Multisets.immutableEntry(k1(), 1)); }
/** * Adds each element of {@code elements} to the {@code ImmutableMultiset}. * * @param elements the {@code Iterable} to add to the {@code ImmutableMultiset} * @return this {@code Builder} object * @throws NullPointerException if {@code elements} is null or contains a null element */ @CanIgnoreReturnValue @Override public Builder<E> addAll(Iterable<? extends E> elements) { if (elements instanceof Multiset) { Multiset<? extends E> multiset = Multisets.cast(elements); multiset.forEachEntry((e, n) -> contents.add(checkNotNull(e), n)); } else { super.addAll(elements); } return this; }
@CollectionSize.Require(absent = ZERO) @CollectionFeature.Require(SUPPORTS_REMOVE) public void testEntrySet_removeAllPresent() { assertTrue( "multiset.entrySet.removeAll(presentEntry) returned false", getMultiset() .entrySet() .removeAll(Collections.singleton(Multisets.immutableEntry(e0(), 1)))); assertFalse("multiset contains element after removing its entry", getMultiset().contains(e0())); }
@SuppressWarnings("deprecation") public void testUnmodifiableMultisetShortCircuit() { Multiset<String> mod = HashMultiset.create(); Multiset<String> unmod = Multisets.unmodifiableMultiset(mod); assertNotSame(mod, unmod); assertSame(unmod, Multisets.unmodifiableMultiset(unmod)); ImmutableMultiset<String> immutable = ImmutableMultiset.of("a", "a", "b", "a"); assertSame(immutable, Multisets.unmodifiableMultiset(immutable)); assertSame(immutable, Multisets.unmodifiableMultiset((Multiset<String>) immutable)); }
public void testHighestCountFirst() { Multiset<String> multiset = HashMultiset.create(Arrays.asList("a", "a", "a", "b", "c", "c")); ImmutableMultiset<String> sortedMultiset = Multisets.copyHighestCountFirst(multiset); assertThat(sortedMultiset.entrySet()) .containsExactly( Multisets.immutableEntry("a", 3), Multisets.immutableEntry("c", 2), Multisets.immutableEntry("b", 1)) .inOrder(); assertThat(sortedMultiset).containsExactly("a", "a", "a", "c", "c", "b").inOrder(); assertThat(Multisets.copyHighestCountFirst(ImmutableMultiset.of())).isEmpty(); }
/** * Returns an immutable multiset containing the given elements, in the "grouped iteration order" * described in the class documentation. * * @throws NullPointerException if any of {@code elements} is null */ public static <E> ImmutableMultiset<E> copyOf(Iterable<? extends E> elements) { if (elements instanceof ImmutableMultiset) { @SuppressWarnings("unchecked") // all supported methods are covariant ImmutableMultiset<E> result = (ImmutableMultiset<E>) elements; if (!result.isPartialView()) { return result; } } Multiset<? extends E> multiset = (elements instanceof Multiset) ? Multisets.cast(elements) : LinkedHashMultiset.create(elements); return copyFromEntries(multiset.entrySet()); }
/** * Implementation of the {@code equals}, {@code hashCode}, and {@code toString} methods of {@link * Multiset.Entry}. */ abstract static class AbstractEntry<E> implements Multiset.Entry<E> { /** * Indicates whether an object equals this entry, following the behavior specified in {@link * Multiset.Entry#equals}. */ @Override public boolean equals(@NullableDecl 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; } /** * Return this entry's hash code, following the behavior specified in {@link * Multiset.Entry#hashCode}. */ @Override public int hashCode() { E e = getElement(); return ((e == null) ? 0 : e.hashCode()) ^ getCount(); } /**
public void testEntrySet_contains_count0() { assertFalse( "multiset.entrySet() contains [missing, 0]", getMultiset().entrySet().contains(Multisets.immutableEntry(e3(), 0))); }
/** * 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()); }
@Override public void setUp() throws Exception { super.setUp(); sortedMultiset = cast(getMultiset()); entries = copyToList( getSubjectGenerator() .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements())); Collections.sort(entries, sortedMultiset.comparator()); // some tests assume SEVERAL == 3 if (entries.size() >= 1) { a = Multisets.immutableEntry(entries.get(0), sortedMultiset.count(entries.get(0))); if (entries.size() >= 3) { b = Multisets.immutableEntry(entries.get(1), sortedMultiset.count(entries.get(1))); c = Multisets.immutableEntry(entries.get(2), sortedMultiset.count(entries.get(2))); } } }
@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; }
/** * A sensible definition of {@link #setCount(Object, int)} in terms of {@link #count(Object)}, * {@link #add(Object, int)}, and {@link #remove(Object, int)}. {@link #entrySet()}. If you * override any of these methods, you may wish to override {@link #setCount(Object, int)} to * forward to this implementation. * * @since 7.0 */ protected int standardSetCount(E element, int count) { return Multisets.setCountImpl(this, element, count); }
/** * A sensible definition of {@link #iterator} in terms of {@link #entrySet} and {@link * #remove(Object)}. If you override either of these methods, you may wish to override {@link * #iterator} to forward to this implementation. * * @since 7.0 */ protected Iterator<E> standardIterator() { return Multisets.iteratorImpl(this); }