@Override public void aggregate(LanguageDistributionCounter counter) { multiset.addAll(counter.multiset); }
/** * Returns a {@code Collector} that accumulates elements into a multiset created via the specified * {@code Supplier}, whose elements are the result of applying {@code elementFunction} to the * inputs, with counts equal to the result of applying {@code countFunction} to the inputs. * Elements are added in encounter order. * * <p>If the mapped elements contain duplicates (according to {@link Object#equals}), the element * will be added more than once, with the count summed over all appearances of the element. * * <p>Note that {@code stream.collect(toMultiset(function, e -> 1, supplier))} is equivalent to * {@code stream.map(function).collect(Collectors.toCollection(supplier))}. * * @since 22.0 */ public static <T, E, M extends Multiset<E>> Collector<T, ?, M> toMultiset( java.util.function.Function<? super T, E> elementFunction, java.util.function.ToIntFunction<? super T> countFunction, java.util.function.Supplier<M> multisetSupplier) { checkNotNull(elementFunction); checkNotNull(countFunction); checkNotNull(multisetSupplier); return Collector.of( multisetSupplier, (ms, t) -> ms.add(elementFunction.apply(t), countFunction.applyAsInt(t)), (ms1, ms2) -> { ms1.addAll(ms2); return ms1; }); }
void add(Counter counter) { unresolved += counter.unresolved; open += counter.open; reopened += counter.reopened; confirmed += counter.confirmed; falsePositives += counter.falsePositives; wontFix += counter.wontFix; severityBag.addAll(counter.severityBag); typeBag.addAll(counter.typeBag); }
@Benchmark public void guava() { Multiset<Integer> result = HashMultiset.create(); for (int i = 0; i < 1000; i++) { result.addAll(this.integersGuava); } }
/** * Returns a {@code Collector} that accumulates elements into an {@code ImmutableMultiset} whose * elements are the result of applying {@code elementFunction} to the inputs, with counts equal to * the result of applying {@code countFunction} to the inputs. * * <p>If the mapped elements contain duplicates (according to {@link Object#equals}), the first * occurrence in encounter order appears in the resulting multiset, with count equal to the sum of * the outputs of {@code countFunction.applyAsInt(t)} for each {@code t} mapped to that element. * * @since 22.0 */ public static <T, E> Collector<T, ?, ImmutableMultiset<E>> toImmutableMultiset( Function<? super T, ? extends E> elementFunction, ToIntFunction<? super T> countFunction) { checkNotNull(elementFunction); checkNotNull(countFunction); return Collector.of( LinkedHashMultiset::create, (multiset, t) -> multiset.add(checkNotNull(elementFunction.apply(t)), countFunction.applyAsInt(t)), (multiset1, multiset2) -> { multiset1.addAll(multiset2); return multiset1; }, (Multiset<E> multiset) -> copyFromEntries(multiset.entrySet())); }
/** * Returns a {@code Collector} that accumulates elements into an {@code ImmutableSortedMultiset} * whose elements are the result of applying {@code elementFunction} to the inputs, with counts * equal to the result of applying {@code countFunction} to the inputs. * * <p>If the mapped elements contain duplicates (according to {@code comparator}), the first * occurrence in encounter order appears in the resulting multiset, with count equal to the sum of * the outputs of {@code countFunction.applyAsInt(t)} for each {@code t} mapped to that element. * * @since 22.0 */ public static <T, E> Collector<T, ?, ImmutableSortedMultiset<E>> toImmutableSortedMultiset( Comparator<? super E> comparator, Function<? super T, ? extends E> elementFunction, ToIntFunction<? super T> countFunction) { checkNotNull(comparator); checkNotNull(elementFunction); checkNotNull(countFunction); return Collector.of( () -> TreeMultiset.create(comparator), (multiset, t) -> multiset.add(checkNotNull(elementFunction.apply(t)), countFunction.applyAsInt(t)), (multiset1, multiset2) -> { multiset1.addAll(multiset2); return multiset1; }, (Multiset<E> multiset) -> copyOfSortedEntries(comparator, multiset.entrySet())); }
@CollectionFeature.Require(SUPPORTS_ADD) public void testAddAll_emptySet() { assertFalse(getMultiset().addAll(Collections.<E>emptySet())); expectUnchanged(); }
/** * Returns a {@code Collector} that accumulates elements into a multiset created via the specified * {@code Supplier}, whose elements are the result of applying {@code elementFunction} to the * inputs, with counts equal to the result of applying {@code countFunction} to the inputs. * Elements are added in encounter order. * * <p>If the mapped elements contain duplicates (according to {@link Object#equals}), the element * will be added more than once, with the count summed over all appearances of the element. * * <p>Note that {@code stream.collect(toMultiset(function, e -> 1, supplier))} is equivalent to * {@code stream.map(function).collect(Collectors.toCollection(supplier))}. * * @since 22.0 */ public static <T, E, M extends Multiset<E>> Collector<T, ?, M> toMultiset( java.util.function.Function<? super T, E> elementFunction, java.util.function.ToIntFunction<? super T> countFunction, java.util.function.Supplier<M> multisetSupplier) { checkNotNull(elementFunction); checkNotNull(countFunction); checkNotNull(multisetSupplier); return Collector.of( multisetSupplier, (ms, t) -> ms.add(elementFunction.apply(t), countFunction.applyAsInt(t)), (ms1, ms2) -> { ms1.addAll(ms2); return ms1; }); }
@Override protected Multiset<String> create(String[] elements) { Multiset<String> multiset = LinkedHashMultiset.create(); Collections.addAll(multiset, elements); multiset.addAll(ELEMENTS_TO_FILTER_OUT); return Multisets.filter(multiset, PREDICATE); }
/** * Returns a {@code Collector} that accumulates elements into an {@code ImmutableMultiset} whose * elements are the result of applying {@code elementFunction} to the inputs, with counts equal to * the result of applying {@code countFunction} to the inputs. * * <p>If the mapped elements contain duplicates (according to {@link Object#equals}), the first * occurrence in encounter order appears in the resulting multiset, with count equal to the sum of * the outputs of {@code countFunction.applyAsInt(t)} for each {@code t} mapped to that element. * * @since 22.0 */ public static <T, E> Collector<T, ?, ImmutableMultiset<E>> toImmutableMultiset( Function<? super T, ? extends E> elementFunction, ToIntFunction<? super T> countFunction) { checkNotNull(elementFunction); checkNotNull(countFunction); return Collector.of( LinkedHashMultiset::create, (multiset, t) -> multiset.add(checkNotNull(elementFunction.apply(t)), countFunction.applyAsInt(t)), (multiset1, multiset2) -> { multiset1.addAll(multiset2); return multiset1; }, (Multiset<E> multiset) -> copyFromEntries(multiset.entrySet())); }
/** * Returns a {@code Collector} that accumulates elements into an {@code ImmutableSortedMultiset} * whose elements are the result of applying {@code elementFunction} to the inputs, with counts * equal to the result of applying {@code countFunction} to the inputs. * * <p>If the mapped elements contain duplicates (according to {@code comparator}), the first * occurrence in encounter order appears in the resulting multiset, with count equal to the sum of * the outputs of {@code countFunction.applyAsInt(t)} for each {@code t} mapped to that element. * * @since 22.0 */ public static <T, E> Collector<T, ?, ImmutableSortedMultiset<E>> toImmutableSortedMultiset( Comparator<? super E> comparator, Function<? super T, ? extends E> elementFunction, ToIntFunction<? super T> countFunction) { checkNotNull(comparator); checkNotNull(elementFunction); checkNotNull(countFunction); return Collector.of( () -> TreeMultiset.create(comparator), (multiset, t) -> multiset.add(checkNotNull(elementFunction.apply(t)), countFunction.applyAsInt(t)), (multiset1, multiset2) -> { multiset1.addAll(multiset2); return multiset1; }, (Multiset<E> multiset) -> copyOfSortedEntries(comparator, multiset.entrySet())); }
@CollectionFeature.Require(SUPPORTS_ADD) public void testAddAll_emptyMultiset() { assertFalse(getMultiset().addAll(getSubjectGenerator().create())); expectUnchanged(); }
@CollectionFeature.Require(SUPPORTS_ADD) public void testAddAll_nonEmptyList() { assertTrue(getMultiset().addAll(Arrays.asList(e3(), e4(), e3()))); expectAdded(e3(), e4(), e3()); }
public void testFastAddAllMultiset() { final AtomicInteger addCalls = new AtomicInteger(); Multiset<String> multiset = new NoRemoveMultiset<String>() { @Override public int add(String element, int occurrences) { addCalls.incrementAndGet(); return super.add(element, occurrences); } }; ImmutableMultiset<String> adds = new ImmutableMultiset.Builder<String>().addCopies("x", 10).build(); multiset.addAll(adds); assertEquals(1, addCalls.get()); }
@CollectionFeature.Require(SUPPORTS_ADD) public void testAddAll_nonEmptyMultiset() { assertTrue(getMultiset().addAll(getSubjectGenerator().create(e3(), e4(), e3()))); expectAdded(e3(), e4(), e3()); } }
public static Map<String, Multiset<String>> mergeFacets(Map<String, Multiset<String>> facets1, Map<String, Multiset<String>> facets2) { Map<String, Multiset<String>> result = Maps.newHashMap(); Set<String> facets1Cats = facets1.keySet(); for (String category : facets1Cats) { Multiset<String> facet1 = HashMultiset.create(facets1.get(category)); Multiset<String> facet2 = facets2.get(category); if (facet2 != null) { facet1.addAll(facet2); } result.put(category, facet1); } Set<String> facets2Cats = facets2.keySet(); for (String category : facets2Cats) { if (!result.containsKey(category)) { result.put(category, HashMultiset.create(facets2.get(category))); } } return result; } }
@Override public Set<String> tokens(Iterable<?> iterable) { Multiset<String> tokens = HashMultiset.create(); int index = 0; for (Object item : iterable) { tokens.add(String.valueOf(index++)); tokens.addAll(fieldValues(item)); } return tokens.stream() .filter(token -> tokens.count(token) == 1) .collect(toImmutableSet()); }
@CollectionFeature.Require(SUPPORTS_ADD) public void testAddAll_emptyMultiset() { assertFalse(getMultiset().addAll(getSubjectGenerator().create())); expectUnchanged(); }
@CollectionFeature.Require(SUPPORTS_ADD) public void testAddAll_nonEmptyList() { assertTrue(getMultiset().addAll(Arrays.asList(e3(), e4(), e3()))); expectAdded(e3(), e4(), e3()); }
@CollectionFeature.Require(SUPPORTS_ADD) public void testAddAll_nonEmptyMultiset() { assertTrue(getMultiset().addAll(getSubjectGenerator().create(e3(), e4(), e3()))); expectAdded(e3(), e4(), e3()); } }