private <T> SetMultimap<T, T> createMapping(List<T> keys, List<T> values) { checkArgument(keys.size() == values.size(), "Inputs must have the same size"); ImmutableSetMultimap.Builder<T, T> builder = ImmutableSetMultimap.builder(); for (int i = 0; i < keys.size(); i++) { builder.put(keys.get(i), values.get(i)); } return builder.build(); }
@GwtIncompatible // SerializableTester public void testSortedSerialization() { Multimap<String, Integer> multimap = new ImmutableSetMultimap.Builder<String, Integer>() .orderKeysBy(Ordering.natural().reverse()) .orderValuesBy(Ordering.usingToString()) .put("a", 2) .put("a", 10) .put("b", 1) .build(); multimap = SerializableTester.reserialize(multimap); assertThat(multimap.keySet()).containsExactly("b", "a").inOrder(); assertThat(multimap.get("a")).containsExactly(10, 2).inOrder(); assertEquals( Ordering.usingToString(), ((ImmutableSortedSet<Integer>) multimap.get("a")).comparator()); assertEquals( Ordering.usingToString(), ((ImmutableSortedSet<Integer>) multimap.get("z")).comparator()); }
public void testBuilder_withMutableEntry() { ImmutableSetMultimap.Builder<String, Integer> builder = new Builder<>(); final StringHolder holder = new StringHolder(); holder.string = "one"; Entry<String, Integer> entry = new AbstractMapEntry<String, Integer>() { @Override public String getKey() { return holder.string; } @Override public Integer getValue() { return 1; } }; builder.put(entry); holder.string = "two"; assertEquals(ImmutableSet.of(1), builder.build().get("one")); }
for (LibraryModels libraryModels : models) { for (Map.Entry<MethodRef, Integer> entry : libraryModels.failIfNullParameters().entries()) { failIfNullParametersBuilder.put(entry); explicitlyNullableParametersBuilder.put(entry); nonNullParametersBuilder.put(entry); failIfNullParameters = failIfNullParametersBuilder.build(); explicitlyNullableParameters = explicitlyNullableParametersBuilder.build(); nonNullParameters = nonNullParametersBuilder.build(); nullImpliesTrueParameters = nullImpliesTrueParametersBuilder.build(); nullableReturns = nullableReturnsBuilder.build(); nonNullReturns = nonNullReturnsBuilder.build();
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); }
@Override public SetMultimap<String, Measure> getRawMeasures(Component component) { T componentKey = componentToKey.apply(component); Map<MeasureKey, Measure> rawMeasures = measures.get(componentKey); if (rawMeasures == null) { return ImmutableSetMultimap.of(); } ImmutableSetMultimap.Builder<String, Measure> builder = ImmutableSetMultimap.builder(); for (Map.Entry<MeasureKey, Measure> entry : rawMeasures.entrySet()) { builder.put(entry.getKey().getMetricKey(), entry.getValue()); } return builder.build(); }
/** * 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(); }
/** * Given a list of actual elements and a list of expected elements, finds a many:many mapping * between actual and expected elements where a pair of elements maps if it satisfies the * correspondence. Returns this mapping as a multimap where the keys are indexes into the actual * list and the values are indexes into the expected list. Any exceptions are treated as if the * elements did not correspond, and the exception added to the store. */ private ImmutableSetMultimap<Integer, Integer> findCandidateMapping( List<? extends A> actual, List<? extends E> expected, Correspondence.ExceptionStore exceptions) { ImmutableSetMultimap.Builder<Integer, Integer> mapping = ImmutableSetMultimap.builder(); for (int actualIndex = 0; actualIndex < actual.size(); actualIndex++) { for (int expectedIndex = 0; expectedIndex < expected.size(); expectedIndex++) { if (correspondence.safeCompare( actual.get(actualIndex), expected.get(expectedIndex), exceptions)) { mapping.put(actualIndex, expectedIndex); } } } return mapping.build(); }
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()); }
ImmutableMultimap<State, Service> servicesByState() { ImmutableSetMultimap.Builder<State, Service> builder = ImmutableSetMultimap.builder(); monitor.enter(); try { for (Entry<State, Service> entry : servicesByState.entries()) { if (!(entry.getValue() instanceof NoOpService)) { builder.put(entry); } } } finally { monitor.leave(); } return builder.build(); }
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()); }
/** * {@inheritDoc} */ @Override public Multimap<String, NotificationChannel> findSubscribedRecipientsForDispatcher(NotificationDispatcher dispatcher, String projectKey, SubscriberPermissionsOnProject subscriberPermissionsOnProject) { requireNonNull(projectKey, "projectKey is mandatory"); String dispatcherKey = dispatcher.getKey(); Set<SubscriberAndChannel> subscriberAndChannels = Arrays.stream(notificationChannels) .flatMap(notificationChannel -> toSubscriberAndChannels(dispatcherKey, projectKey, notificationChannel)) .collect(Collectors.toSet()); if (subscriberAndChannels.isEmpty()) { return ImmutableMultimap.of(); } ImmutableSetMultimap.Builder<String, NotificationChannel> builder = ImmutableSetMultimap.builder(); try (DbSession dbSession = dbClient.openSession(false)) { Set<String> authorizedLogins = keepAuthorizedLogins(dbSession, projectKey, subscriberAndChannels, subscriberPermissionsOnProject); subscriberAndChannels.stream() .filter(subscriberAndChannel -> authorizedLogins.contains(subscriberAndChannel.getSubscriber().getLogin())) .forEach(subscriberAndChannel -> builder.put(subscriberAndChannel.getSubscriber().getLogin(), subscriberAndChannel.getChannel())); } return builder.build(); }
ImmutableSet.Builder<Node> shuttingDownNodesBuilder = ImmutableSet.builder(); ImmutableSet.Builder<Node> coordinatorsBuilder = ImmutableSet.builder(); ImmutableSetMultimap.Builder<ConnectorId, Node> byConnectorIdBuilder = ImmutableSetMultimap.builder(); connectorIds = connectorIds.toLowerCase(ENGLISH); for (String connectorId : CONNECTOR_ID_SPLITTER.split(connectorIds)) { byConnectorIdBuilder.put(new ConnectorId(connectorId), node); byConnectorIdBuilder.put(new ConnectorId(GlobalSystemConnector.NAME), node); break; case INACTIVE: activeNodesByConnectorId = byConnectorIdBuilder.build(); coordinators = coordinatorsBuilder.build();
/** * Returns an immutable multimap containing the given entries, in order. Repeated occurrences of * an entry (according to {@link Object#equals}) after the first are ignored. */ public static <K, V> ImmutableSetMultimap<K, V> of(K k1, V v1, K k2, V v2) { ImmutableSetMultimap.Builder<K, V> builder = ImmutableSetMultimap.builder(); builder.put(k1, v1); builder.put(k2, v2); return builder.build(); }
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()); }