Refine search
@Override Iterator<Multiset.Entry<K>> entryIterator() { return new TransformedIterator<Map.Entry<K, Collection<V>>, Multiset.Entry<K>>( multimap.asMap().entrySet().iterator()) { @Override Multiset.Entry<K> transform(final Map.Entry<K, Collection<V>> backingEntry) { return new Multisets.AbstractEntry<K>() { @Override public K getElement() { return backingEntry.getKey(); } @Override public int getCount() { return backingEntry.getValue().size(); } }; } }; }
@Override public Map<K, Collection<V>> create(Object... elements) { Set<K> keySet = new HashSet<>(); List<Entry<K, V>> builder = new ArrayList<>(); for (Object o : elements) { Entry<K, Collection<V>> entry = (Entry<K, Collection<V>>) o; keySet.add(entry.getKey()); for (V v : entry.getValue()) { builder.add(mapEntry(entry.getKey(), v)); } } checkArgument(keySet.size() == elements.length, "Duplicate keys"); return multimapGenerator.create(builder.toArray()).asMap(); }
/** * Stores the contents of a multimap in an output stream, as part of serialization. It does not * support concurrent multimaps whose content may change while the method is running. The {@link * Multimap#asMap} view determines the ordering in which data is written to the stream. * * <p>The serialized output consists of the number of distinct keys, and then for each distinct * key: the key, the number of values for that key, and the key's values. */ static <K, V> void writeMultimap(Multimap<K, V> multimap, ObjectOutputStream stream) throws IOException { stream.writeInt(multimap.asMap().size()); for (Map.Entry<K, Collection<V>> entry : multimap.asMap().entrySet()) { stream.writeObject(entry.getKey()); stream.writeInt(entry.getValue().size()); for (V value : entry.getValue()) { stream.writeObject(value); } } }
public void testAsMapGet() { for (K key : sampleKeys()) { List<V> expectedValues = new ArrayList<>(); for (Entry<K, V> entry : getSampleElements()) { if (entry.getKey().equals(key)) { expectedValues.add(entry.getValue()); } } Collection<V> collection = multimap().asMap().get(key); if (expectedValues.isEmpty()) { assertNull(collection); } else { assertEqualIgnoringOrder(expectedValues, collection); } } }
public static void serialize(SerializationStreamWriter writer, Multimap<?, ?> instance) throws SerializationException { writer.writeInt(instance.asMap().size()); for (Entry<?, ? extends Collection<?>> entry : instance.asMap().entrySet()) { writer.writeObject(entry.getKey()); writer.writeInt(entry.getValue().size()); for (Object value : entry.getValue()) { writer.writeObject(value); } } }
@MapFeature.Require(SUPPORTS_REMOVE) @CollectionSize.Require(absent = ZERO) public void testRemovePropagatesToAsMap() { List<Entry<K, V>> entries = Helpers.copyToList(multimap().entries()); for (Entry<K, V> entry : entries) { resetContainer(); K key = entry.getKey(); V value = entry.getValue(); Collection<V> collection = multimap().asMap().get(key); assertNotNull(collection); Collection<V> expectedCollection = Helpers.copyToList(collection); multimap().remove(key, value); expectedCollection.remove(value); assertEqualIgnoringOrder(expectedCollection, collection); assertEquals(!expectedCollection.isEmpty(), multimap().containsKey(key)); } }
"Not true that %s contains entry <%s>. However, it has a mapping from <%s> to " + "<%s>", actualAsString(), entry, key, actual().asMap().get(key)))); } else if (actual().containsValue(value)) { Set<Object> keys = new LinkedHashSet<>(); for (Entry<?, ?> actualEntry : actual().entries()) { if (Objects.equal(actualEntry.getValue(), value)) { keys.add(actualEntry.getKey());
/** Registers all subscriber methods on the given listener object. */ void register(Object listener) { Multimap<Class<?>, Subscriber> listenerMethods = findAllSubscribers(listener); for (Entry<Class<?>, Collection<Subscriber>> entry : listenerMethods.asMap().entrySet()) { Class<?> eventType = entry.getKey(); Collection<Subscriber> eventMethodsInListener = entry.getValue(); CopyOnWriteArraySet<Subscriber> eventSubscribers = subscribers.get(eventType); if (eventSubscribers == null) { CopyOnWriteArraySet<Subscriber> newSet = new CopyOnWriteArraySet<>(); eventSubscribers = MoreObjects.firstNonNull(subscribers.putIfAbsent(eventType, newSet), newSet); } eventSubscribers.addAll(eventMethodsInListener); } }
boolean removeEntriesIf(Predicate<? super Entry<K, Collection<V>>> predicate) { Iterator<Entry<K, Collection<V>>> entryIterator = unfiltered.asMap().entrySet().iterator(); boolean changed = false; while (entryIterator.hasNext()) { Entry<K, Collection<V>> entry = entryIterator.next(); K key = entry.getKey(); Collection<V> collection = filterCollection(entry.getValue(), new ValuePredicate(key)); if (!collection.isEmpty() && predicate.apply(Maps.immutableEntry(key, collection))) { if (collection.size() == entry.getValue().size()) { entryIterator.remove(); } else { collection.clear(); } changed = true; } } return changed; }
/** * Stores another multimap's entries in the built multimap. The generated multimap's key and * value orderings correspond to the iteration ordering of the {@code multimap.asMap()} view, * with new keys and values following any existing keys and values. * * @throws NullPointerException if any key or value in {@code multimap} is null. The builder is * left in an invalid state. */ @CanIgnoreReturnValue public Builder<K, V> putAll(Multimap<? extends K, ? extends V> multimap) { for (Entry<? extends K, ? extends Collection<? extends V>> entry : multimap.asMap().entrySet()) { putAll(entry.getKey(), entry.getValue()); } return this; }
/** Unregisters all subscribers on the given listener object. */ void unregister(Object listener) { Multimap<Class<?>, Subscriber> listenerMethods = findAllSubscribers(listener); for (Entry<Class<?>, Collection<Subscriber>> entry : listenerMethods.asMap().entrySet()) { Class<?> eventType = entry.getKey(); Collection<Subscriber> listenerMethodsForType = entry.getValue(); CopyOnWriteArraySet<Subscriber> currentSubscribers = subscribers.get(eventType); if (currentSubscribers == null || !currentSubscribers.removeAll(listenerMethodsForType)) { // if removeAll returns true, all we really know is that at least one subscriber was // removed... however, barring something very strange we can assume that if at least one // subscriber was removed, all subscribers on listener for that event type were... after // all, the definition of subscribers on a particular class is totally static throw new IllegalArgumentException( "missing event subscriber for an annotated method. Is " + listener + " registered?"); } // don't try to remove the set if it's empty; that can't be done safely without a lock // anyway, if the set is empty it'll just be wrapping an array of length 0 } }
/** * Stores the contents of a multimap in an output stream, as part of serialization. It does not * support concurrent multimaps whose content may change while the method is running. The {@link * Multimap#asMap} view determines the ordering in which data is written to the stream. * * <p>The serialized output consists of the number of distinct keys, and then for each distinct * key: the key, the number of values for that key, and the key's values. */ static <K, V> void writeMultimap(Multimap<K, V> multimap, ObjectOutputStream stream) throws IOException { stream.writeInt(multimap.asMap().size()); for (Map.Entry<K, Collection<V>> entry : multimap.asMap().entrySet()) { stream.writeObject(entry.getKey()); stream.writeInt(entry.getValue().size()); for (V value : entry.getValue()) { stream.writeObject(value); } } }
private static void checkUniqueHandlerPerCeTaskType(Multimap<String, CeTaskProcessor> permissiveIndex) { for (Map.Entry<String, Collection<CeTaskProcessor>> entry : permissiveIndex.asMap().entrySet()) { checkArgument( entry.getValue().size() == 1, format( "There can be only one CeTaskProcessor instance registered as the processor for CeTask type %s. " + "More than one found. Please fix your configuration: %s", entry.getKey(), COMMA_JOINER.join(from(entry.getValue()).transform(ToClassName.INSTANCE).toSortedList(CASE_INSENSITIVE_ORDER)))); } }
private static void notify(String author, Context context, Multimap<String, NotificationChannel> subscribedRecipients) { for (Map.Entry<String, Collection<NotificationChannel>> channelsByRecipients : subscribedRecipients.asMap().entrySet()) { String login = channelsByRecipients.getKey(); // Do not notify the person that resolved the issue if (!Objects.equals(author, login)) { for (NotificationChannel channel : channelsByRecipients.getValue()) { context.addUser(login, channel); } } } }
protected static EquivalenceClasses copy(EquivalenceClasses ec) { final EquivalenceClasses newEc = new EquivalenceClasses(); for (Entry<RexTableInputRef, Set<RexTableInputRef>> e : ec.nodeToEquivalenceClass.entrySet()) { newEc.nodeToEquivalenceClass.put(e.getKey(), Sets.newLinkedHashSet(e.getValue())); } for (Entry<Pair<RexTableInputRef, RexTableInputRef>, Collection<RexCall>> e : ec.equivalenceToOriginalNode.asMap().entrySet()) { newEc.equivalenceToOriginalNode.putAll(e.getKey(), e.getValue()); } return newEc; } }
@Override Iterator<Multiset.Entry<K>> entryIterator() { return new TransformedIterator<Map.Entry<K, Collection<V>>, Multiset.Entry<K>>( multimap.asMap().entrySet().iterator()) { @Override Multiset.Entry<K> transform(final Map.Entry<K, Collection<V>> backingEntry) { return new Multisets.AbstractEntry<K>() { @Override public K getElement() { return backingEntry.getKey(); } @Override public int getCount() { return backingEntry.getValue().size(); } }; } }; }
private static <K, V> ImmutableSetMultimap<K, V> copyOf( Multimap<? extends K, ? extends V> multimap, Comparator<? super V> valueComparator) { checkNotNull(multimap); // eager for GWT if (multimap.isEmpty() && valueComparator == null) { return of(); } if (multimap instanceof ImmutableSetMultimap) { @SuppressWarnings("unchecked") // safe since multimap is not writable ImmutableSetMultimap<K, V> kvMultimap = (ImmutableSetMultimap<K, V>) multimap; if (!kvMultimap.isPartialView()) { return kvMultimap; } } return fromMapEntries(multimap.asMap().entrySet(), valueComparator); }