@CanIgnoreReturnValue @Override 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; }
@CanIgnoreReturnValue @Override 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; }
private EqualityInference(Iterable<Set<Expression>> equalityGroups, Set<Expression> derivedExpressions) { ImmutableSetMultimap.Builder<Expression, Expression> setBuilder = ImmutableSetMultimap.builder(); for (Set<Expression> equalityGroup : equalityGroups) { if (!equalityGroup.isEmpty()) { setBuilder.putAll(CANONICAL_ORDERING.min(equalityGroup), equalityGroup); } } equalitySets = setBuilder.build(); ImmutableMap.Builder<Expression, Expression> mapBuilder = ImmutableMap.builder(); for (Map.Entry<Expression, Expression> entry : equalitySets.entries()) { Expression canonical = entry.getKey(); Expression expression = entry.getValue(); mapBuilder.put(expression, canonical); } canonicalMap = mapBuilder.build(); this.derivedExpressions = ImmutableSet.copyOf(derivedExpressions); }
/** * Stores an array of values with the same key in the built multimap. * * @throws NullPointerException if the key or any value is null. The * builder is left in an invalid state. */ @Override public Builder<K, V> putAll(K key, V... values) { return putAll(key, Arrays.asList(values)); }
/** * A builder for creating immutable {@code SetMultimap} instances, especially {@code public static * final} multimaps ("constant multimaps"). Example: * * <pre>{@code * static final Multimap<String, Integer> STRING_TO_INTEGER_MULTIMAP = * new ImmutableSetMultimap.Builder<String, Integer>() * .put("one", 1) * .putAll("several", 1, 2, 3) * .putAll("many", 1, 2, 3, 4, 5) * .build(); * }</pre> * * <p>Builder instances can be reused; it is safe to call {@link #build} multiple times to build * multiple multimaps in series. Each multimap contains the key-value mappings in the previously * created multimaps. * * @since 2.0 */ public static final class Builder<K, V> extends ImmutableMultimap.Builder<K, V> { /** * Creates a new builder. The returned builder is equivalent to the builder generated by {@link * ImmutableSetMultimap#builder}. */ public Builder() { super(); } @Override Collection<V> newMutableValueCollection() {
/** * This method creates a mapping from each index source lookup symbol (directly applied to the index) * to the corresponding probe key Input */ private SetMultimap<Symbol, Integer> mapIndexSourceLookupSymbolToProbeKeyInput(IndexJoinNode node, Map<Symbol, Integer> probeKeyLayout) { Set<Symbol> indexJoinSymbols = node.getCriteria().stream() .map(IndexJoinNode.EquiJoinClause::getIndex) .collect(toImmutableSet()); // Trace the index join symbols to the index source lookup symbols // Map: Index join symbol => Index source lookup symbol Map<Symbol, Symbol> indexKeyTrace = IndexJoinOptimizer.IndexKeyTracer.trace(node.getIndexSource(), indexJoinSymbols); // Map the index join symbols to the probe key Input Multimap<Symbol, Integer> indexToProbeKeyInput = HashMultimap.create(); for (IndexJoinNode.EquiJoinClause clause : node.getCriteria()) { indexToProbeKeyInput.put(clause.getIndex(), probeKeyLayout.get(clause.getProbe())); } // Create the mapping from index source look up symbol to probe key Input ImmutableSetMultimap.Builder<Symbol, Integer> builder = ImmutableSetMultimap.builder(); for (Map.Entry<Symbol, Symbol> entry : indexKeyTrace.entrySet()) { Symbol indexJoinSymbol = entry.getKey(); Symbol indexLookupSymbol = entry.getValue(); builder.putAll(indexLookupSymbol, indexToProbeKeyInput.get(indexJoinSymbol)); } return builder.build(); }
/** * Returns an immutable multimap containing the specified entries. The returned multimap iterates * over keys in the order they were first encountered in the input, and the values for each key * are iterated in the order they were encountered. If two values for the same key are {@linkplain * Object#equals equal}, the first value encountered is used. * * @throws NullPointerException if any key, value, or entry is null * @since 19.0 */ @Beta public static <K, V> ImmutableSetMultimap<K, V> copyOf( Iterable<? extends Entry<? extends K, ? extends V>> entries) { return new Builder<K, V>().putAll(entries).build(); }
@CanIgnoreReturnValue @Override public Builder<K, V> putAll(K key, V... values) { return putAll(key, Arrays.asList(values)); }
private static ImmutableSetMultimap<ImportTree, Symbol> getImportedSymbols( CompilationUnitTree compilationUnitTree, VisitorState state) { ImmutableSetMultimap.Builder<ImportTree, Symbol> builder = ImmutableSetMultimap.builder(); for (ImportTree importTree : compilationUnitTree.getImports()) { builder.putAll(importTree, getImportedSymbols(importTree, state)); } return builder.build(); }
public void testBuilderPutNullKey() { Multimap<String, Integer> toPut = LinkedListMultimap.create(); toPut.put("foo", null); ImmutableSetMultimap.Builder<String, Integer> builder = ImmutableSetMultimap.builder(); try { builder.put(null, 1); fail(); } catch (NullPointerException expected) { } try { builder.putAll(null, Arrays.asList(1, 2, 3)); fail(); } catch (NullPointerException expected) { } try { builder.putAll(null, 1, 2, 3); fail(); } catch (NullPointerException expected) { } try { builder.putAll(toPut); fail(); } catch (NullPointerException expected) { } }
public void testBuilderPutNullValue() { Multimap<String, Integer> toPut = LinkedListMultimap.create(); toPut.put(null, 1); ImmutableSetMultimap.Builder<String, Integer> builder = ImmutableSetMultimap.builder(); try { builder.put("foo", null); fail(); } catch (NullPointerException expected) { } try { builder.putAll("foo", Arrays.asList(1, null, 3)); fail(); } catch (NullPointerException expected) { } try { builder.putAll("foo", 4, null, 6); fail(); } catch (NullPointerException expected) { } try { builder.putAll(toPut); fail(); } catch (NullPointerException expected) { } }
public void testFlatteningToImmutableSetMultimap() { Collector<String, ?, ImmutableSetMultimap<Character, Character>> collector = ImmutableSetMultimap.flatteningToImmutableSetMultimap( str -> str.charAt(0), str -> str.substring(1).chars().mapToObj(c -> (char) c)); BiPredicate<Multimap<?, ?>, Multimap<?, ?>> equivalence = Equivalence.equals() .onResultOf((Multimap<?, ?> mm) -> ImmutableList.copyOf(mm.asMap().entrySet())) .and(Equivalence.equals()); ImmutableSetMultimap<Character, Character> empty = ImmutableSetMultimap.of(); ImmutableSetMultimap<Character, Character> filled = ImmutableSetMultimap.<Character, Character>builder() .putAll('b', Arrays.asList('a', 'n', 'a', 'n', 'a')) .putAll('a', Arrays.asList('p', 'p', 'l', 'e')) .putAll('c', Arrays.asList('a', 'r', 'r', 'o', 't')) .putAll('a', Arrays.asList('s', 'p', 'a', 'r', 'a', 'g', 'u', 's')) .putAll('c', Arrays.asList('h', 'e', 'r', 'r', 'y')) .build(); CollectorTester.of(collector, equivalence) .expectCollects(empty) .expectCollects(filled, "banana", "apple", "carrot", "asparagus", "cherry"); }
public void testBuilderPutAllMultimap() { Multimap<String, Integer> toPut = LinkedListMultimap.create(); toPut.put("foo", 1); toPut.put("bar", 4); toPut.put("foo", 2); toPut.put("foo", 3); Multimap<String, Integer> moreToPut = LinkedListMultimap.create(); moreToPut.put("foo", 6); moreToPut.put("bar", 5); moreToPut.put("foo", 7); ImmutableSetMultimap.Builder<String, Integer> builder = ImmutableSetMultimap.builder(); builder.putAll(toPut); builder.putAll(moreToPut); Multimap<String, Integer> multimap = builder.build(); assertEquals(ImmutableSet.of(1, 2, 3, 6, 7), multimap.get("foo")); assertEquals(ImmutableSet.of(4, 5), multimap.get("bar")); assertEquals(7, multimap.size()); }
/** * Returns an immutable multimap containing the specified entries. The returned multimap iterates * over keys in the order they were first encountered in the input, and the values for each key * are iterated in the order they were encountered. If two values for the same key are {@linkplain * Object#equals equal}, the first value encountered is used. * * @throws NullPointerException if any key, value, or entry is null * @since 19.0 */ @Beta public static <K, V> ImmutableSetMultimap<K, V> copyOf( Iterable<? extends Entry<? extends K, ? extends V>> entries) { return new Builder<K, V>().putAll(entries).build(); }
@CanIgnoreReturnValue @Override public Builder<K, V> putAll(K key, V... values) { return putAll(key, Arrays.asList(values)); }
public void testBuilderPutWithDuplicates() { ImmutableSetMultimap.Builder<String, Integer> builder = ImmutableSetMultimap.builder(); builder.putAll("foo", 1, 2, 3); builder.putAll("bar", 4, 5); builder.put("foo", 1); ImmutableSetMultimap<String, Integer> multimap = builder.build(); assertEquals(5, multimap.size()); }
public void testBuilderPutAllWithDuplicates() { ImmutableSetMultimap.Builder<String, Integer> builder = ImmutableSetMultimap.builder(); builder.putAll("foo", 1, 2, 3); builder.putAll("bar", 4, 5); builder.putAll("foo", 1, 6, 7); ImmutableSetMultimap<String, Integer> multimap = builder.build(); assertEquals(7, multimap.size()); }
public void testBuilderPutAllVarargs() { ImmutableSetMultimap.Builder<String, Integer> builder = ImmutableSetMultimap.builder(); builder.putAll("foo", 1, 2, 3); builder.putAll("bar", 4, 5); builder.putAll("foo", 6, 7); Multimap<String, Integer> multimap = builder.build(); assertEquals(ImmutableSet.of(1, 2, 3, 6, 7), multimap.get("foo")); assertEquals(ImmutableSet.of(4, 5), multimap.get("bar")); assertEquals(7, multimap.size()); }
public void testBuilderPutAllIterable() { ImmutableSetMultimap.Builder<String, Integer> builder = ImmutableSetMultimap.builder(); builder.putAll("foo", Arrays.asList(1, 2, 3)); builder.putAll("bar", Arrays.asList(4, 5)); builder.putAll("foo", Arrays.asList(6, 7)); Multimap<String, Integer> multimap = builder.build(); assertEquals(ImmutableSet.of(1, 2, 3, 6, 7), multimap.get("foo")); assertEquals(ImmutableSet.of(4, 5), multimap.get("bar")); assertEquals(7, multimap.size()); }
public void testBuilderPutAllMultimapWithDuplicates() { Multimap<String, Integer> toPut = LinkedListMultimap.create(); toPut.put("foo", 1); toPut.put("bar", 4); toPut.put("foo", 2); toPut.put("foo", 1); toPut.put("bar", 5); ImmutableSetMultimap.Builder<String, Integer> builder = ImmutableSetMultimap.builder(); builder.putAll(toPut); ImmutableSetMultimap<String, Integer> multimap = builder.build(); assertEquals(4, multimap.size()); }