@Override public void setPriority(int priority) { int oldPriority = getPriority(); super.setPriority(priority); int newPriority = getPriority(); if (newPriority != oldPriority) { INT_PRIORITY.incrementAndGet(); GROUPED_PRIORITY.add(priority); } }
/** * 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<T, E> elementFunction, java.util.function.ToIntFunction<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; }); }
/** * Populates a multiset by reading an input stream, as part of * deserialization. See {@link #writeMultiset} for the data format. The number * of distinct elements is determined by a prior call to {@link #readCount}. */ static <E> void populateMultiset( Multiset<E> multiset, ObjectInputStream stream, int distinctElements) throws IOException, ClassNotFoundException { for (int i = 0; i < distinctElements; i++) { @SuppressWarnings("unchecked") // reading data stored by writeMultiset E element = (E) stream.readObject(); int count = stream.readInt(); multiset.add(element, count); } }
/** * 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())); }
@Override public int add(E e, int n) { synchronized (mutex) { return delegate().add(e, n); } }
/** * 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())); }
@Override public int add(@Nullable E element, int occurrences) { checkArgument( predicate.apply(element), "Element %s does not match predicate %s", element, predicate); return unfiltered.add(element, occurrences); }
@CanIgnoreReturnValue @Override public int add(E element, int occurrences) { return delegate().add(element, occurrences); }
/** * Adds {@code element} to the {@code ImmutableMultiset}. * * @param element the element to add * @return this {@code Builder} object * @throws NullPointerException if {@code element} is null */ @CanIgnoreReturnValue @Override public Builder<E> add(E element) { contents.add(checkNotNull(element)); return this; }
/** * Adds a number of occurrences of an element to this {@code * ImmutableMultiset}. * * @param element the element to add * @param occurrences the number of occurrences of the element to add. May * be zero, in which case no change will be made. * @return this {@code Builder} object * @throws NullPointerException if {@code element} is null * @throws IllegalArgumentException if {@code occurrences} is negative, or * if this operation would result in more than {@link Integer#MAX_VALUE} * occurrences of the element */ @CanIgnoreReturnValue public Builder<E> addCopies(E element, int occurrences) { contents.add(checkNotNull(element), occurrences); return this; }
@Override public void addOutEdge(E edge, N node) { super.addOutEdge(edge, node); Multiset<N> adjacentNodes = getReference(adjacentNodesReference); if (adjacentNodes != null) { checkState(adjacentNodes.add(node)); } }
@Override public void addInEdge(E edge, N node, boolean isSelfLoop) { super.addInEdge(edge, node, isSelfLoop); Multiset<N> predecessors = getReference(predecessorsReference); if (predecessors != null) { checkState(predecessors.add(node)); } }
@Override public void addOutEdge(E edge, N node) { super.addOutEdge(edge, node); Multiset<N> successors = getReference(successorsReference); if (successors != null) { checkState(successors.add(node)); } }
/** * An implementation of {@link Multiset#setCount(Object, int)}. */ static <E> int setCountImpl(Multiset<E> self, E element, int count) { checkNonnegative(count, "count"); int oldCount = self.count(element); int delta = count - oldCount; if (delta > 0) { self.add(element, delta); } else if (delta < 0) { self.remove(element, -delta); } return oldCount; }
/** * An implementation of {@link Multiset#addAll}. */ static <E> boolean addAllImpl(Multiset<E> self, Collection<? extends E> elements) { if (elements.isEmpty()) { return false; } if (elements instanceof Multiset) { Multiset<? extends E> that = cast(elements); for (Entry<? extends E> entry : that.entrySet()) { self.add(entry.getElement(), entry.getCount()); } } else { Iterators.addAll(self, elements.iterator()); } return true; }
@Override public void setPriority(int priority) { int oldPriority = getPriority(); super.setPriority(priority); int newPriority = getPriority(); if (newPriority != oldPriority) { INT_PRIORITY.incrementAndGet(); GROUPED_PRIORITY.add(priority); } }
@Override public void setPriority(int priority) { int oldPriority = getPriority(); super.setPriority(priority); int newPriority = getPriority(); if (newPriority != oldPriority) { INT_PRIORITY.incrementAndGet(); GROUPED_PRIORITY.add(priority); } }