List of usage examples for com.google.common.collect Multimaps asMap
@Beta public static <K, V> Map<K, Collection<V>> asMap(Multimap<K, V> multimap)
From source file:com.google.javascript.refactoring.ApplySuggestedFixes.java
/** * Applies the provided set of suggested fixes to the provided code and returns the new code. * The {@code filenameToCodeMap} must contain all the files that the provided fixes apply to. * The fixes can be provided in any order, but they may not have any overlapping modifications * for the same file./*from ww w . ja v a 2s .c om*/ * This function will return new code only for the files that have been modified. */ public static Map<String, String> applySuggestedFixesToCode(Iterable<SuggestedFix> fixes, Map<String, String> filenameToCodeMap) { ImmutableSetMultimap.Builder<String, CodeReplacement> builder = ImmutableSetMultimap.builder(); for (SuggestedFix fix : fixes) { builder.putAll(fix.getReplacements()); } SetMultimap<String, CodeReplacement> replacementsMap = builder.build(); ImmutableMap.Builder<String, String> newCodeMap = ImmutableMap.builder(); for (Map.Entry<String, Set<CodeReplacement>> entry : Multimaps.asMap(replacementsMap).entrySet()) { String filename = entry.getKey(); if (!filenameToCodeMap.containsKey(filename)) { throw new IllegalArgumentException("filenameToCodeMap missing code for file: " + filename); } Set<CodeReplacement> replacements = entry.getValue(); String newCode = applyCodeReplacements(replacements, filenameToCodeMap.get(filename)); newCodeMap.put(filename, newCode); } return newCodeMap.build(); }
From source file:dagger.internal.codegen.IncompatiblyScopedBindingsValidator.java
@Override public void visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter) { ImmutableSetMultimap.Builder<ComponentNode, dagger.model.Binding> incompatibleBindings = ImmutableSetMultimap .builder();/*from ww w.j av a 2s. c o m*/ for (dagger.model.Binding binding : bindingGraph.bindings()) { binding.scope().filter(scope -> !scope.isReusable()).ifPresent(scope -> { ComponentNode componentNode = bindingGraph.componentNode(binding.componentPath()).get(); if (!componentNode.scopes().contains(scope)) { // @Inject bindings in module binding graphs will appear at the properly scoped // ancestor component, so ignore them here. if (binding.kind().equals(INJECTION) && bindingGraph.isModuleBindingGraph()) { return; } incompatibleBindings.put(componentNode, binding); } }); } Multimaps.asMap(incompatibleBindings.build()).forEach( (componentNode, bindings) -> report(componentNode, bindings, bindingGraph, diagnosticReporter)); }
From source file:com.google.errorprone.BugPatternIndexWriter.java
void dump(Collection<BugPatternInstance> patterns, Writer w, Target target, Set<String> enabledChecks) throws IOException { // (Default, Severity) -> [Pattern...] SortedSetMultimap<IndexEntry, MiniDescription> sorted = TreeMultimap.create( Comparator.comparing(IndexEntry::onByDefault).reversed().thenComparing(IndexEntry::severity), Comparator.comparing(MiniDescription::name)); for (BugPatternInstance pattern : patterns) { sorted.put(IndexEntry.create(enabledChecks.contains(pattern.name), pattern.severity), MiniDescription.create(pattern)); }/*from w ww . j a va 2 s . c o m*/ Map<String, Object> templateData = new HashMap<>(); List<Map<String, Object>> bugpatternData = Multimaps.asMap(sorted).entrySet().stream() .map(e -> ImmutableMap.of("category", e.getKey().asCategoryHeader(), "checks", e.getValue())) .collect(Collectors.toCollection(ArrayList::new)); templateData.put("bugpatterns", bugpatternData); if (target == Target.EXTERNAL) { Map<String, String> frontmatterData = ImmutableMap.<String, String>builder() .put("title", "Bug Patterns").put("layout", "master").build(); DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yaml = new Yaml(options); Writer yamlWriter = new StringWriter(); yamlWriter.write("---\n"); yaml.dump(frontmatterData, yamlWriter); yamlWriter.write("---\n"); templateData.put("frontmatter", yamlWriter.toString()); MustacheFactory mf = new DefaultMustacheFactory(); Mustache mustache = mf.compile("com/google/errorprone/resources/bugpatterns_external.mustache"); mustache.execute(w, templateData); } else { MustacheFactory mf = new DefaultMustacheFactory(); Mustache mustache = mf.compile("com/google/errorprone/resources/bugpatterns_internal.mustache"); mustache.execute(w, templateData); } }
From source file:org.glowroot.ui.MultiErrorIntervalMerger.java
private static GroupedMultiErrorInterval mergeWithOverlapping(String syntheticMonitorId, MultiErrorInterval multiErrorInterval, List<GroupedMultiErrorInterval> overlapping) { long minFrom = multiErrorInterval.from(); long maxTo = multiErrorInterval.to(); ListMultimap<String, ErrorInterval> errorIntervals = ArrayListMultimap.create(); errorIntervals.putAll(syntheticMonitorId, multiErrorInterval.errorIntervals()); for (GroupedMultiErrorInterval groupedMultiErrorInterval : overlapping) { minFrom = Math.min(minFrom, groupedMultiErrorInterval.from()); maxTo = Math.max(maxTo, groupedMultiErrorInterval.to()); for (Map.Entry<String, List<ErrorInterval>> entry : groupedMultiErrorInterval.errorIntervals() .entrySet()) {// ww w. j a va 2s. co m errorIntervals.putAll(entry.getKey(), entry.getValue()); } } return ImmutableGroupedMultiErrorInterval.builder().from(minFrom).to(maxTo) .putAllErrorIntervals(Multimaps.asMap(errorIntervals)).build(); }
From source file:dagger.internal.codegen.MapMultibindingValidation.java
private void checkForDuplicateMapKeys(BindingNode multiboundMapBindingNode, ImmutableSet<ContributionBinding> contributions, DiagnosticReporter diagnosticReporter) { ImmutableSetMultimap<Object, ContributionBinding> contributionsByMapKey = ImmutableSetMultimap .copyOf(Multimaps.index(contributions, ContributionBinding::mapKey)); for (Set<ContributionBinding> contributionsForOneMapKey : Multimaps.asMap(contributionsByMapKey).values()) { if (contributionsForOneMapKey.size() > 1) { diagnosticReporter.reportBinding(ERROR, multiboundMapBindingNode, duplicateMapKeyErrorMessage( contributionsForOneMapKey, multiboundMapBindingNode.binding().key())); }/* ww w. ja v a2 s .co m*/ } }
From source file:de.bund.bfr.knime.pmmlite.views.fittedparameterview.FittedParameterViewReader.java
public FittedParameterViewReader(PmmPortObject input) throws UnitException { ListMultimap<String, PrimaryModel> modelsById = ArrayListMultimap.create(); for (PrimaryModel model : input.getData(PrimaryModel.class)) { modelsById.put(model.getFormula().getId(), model); }/*from w w w . j av a 2 s . c o m*/ ids = new ArrayList<>(); colorCounts = new ArrayList<>(); plotables = new LinkedHashMap<>(); legend = new LinkedHashMap<>(); stringValues = new LinkedHashMap<>(); stringValues.put(PmmUtils.PARAMETER, new ArrayList<>()); stringValues.put(PmmUtils.MODEL, new ArrayList<>()); visibleColumns = new LinkedHashSet<>(Arrays.asList(PmmUtils.PARAMETER, PmmUtils.MODEL)); conditionValues = ViewUtils.initConditionsValues(PmmUtils.getData(modelsById.values())); for (List<PrimaryModel> currentModels : Multimaps.asMap(modelsById).values()) { PrimaryModelFormula formula = currentModels.get(0).getFormula(); Map<String, Plotable> currentPlotables = Plotables.readFittedParameters(currentModels); for (Parameter param : formula.getParams()) { plotables.put(createId(formula, param), currentPlotables.get(param.getName())); } } Map<String, PmmUnit> units = ViewUtils.getMostCommonUnits(plotables.values()); for (List<PrimaryModel> currentModels : Multimaps.asMap(modelsById).values()) { PrimaryModelFormula formula = currentModels.get(0).getFormula(); Map<String, ConditionValue> condRanges = new LinkedHashMap<>(); List<Map<String, Condition>> conditionsByName = new ArrayList<>(); for (PrimaryModel model : currentModels) { conditionsByName.add(PmmUtils.getByName(model.getData().getConditions())); } for (String cond : conditionValues.keySet()) { double min = Double.POSITIVE_INFINITY; double max = Double.NEGATIVE_INFINITY; for (Map<String, Condition> byName : conditionsByName) { Condition condition = byName.get(cond); if (condition != null) { double value = PmmUtils.convertTo(MathUtils.nullToNan(condition.getValue()), condition.getUnit(), units.get(cond)); if (Double.isFinite(value)) { min = Math.min(min, value); max = Math.max(max, value); } } } condRanges.put(cond, new ConditionValue(!Double.isInfinite(min) ? min : null, !Double.isInfinite(max) ? max : null)); } for (Parameter param : formula.getParams()) { String id = createId(formula, param); ids.add(id); legend.put(id, param.getName() + " (" + formula.getName() + ")"); colorCounts.add(plotables.get(id).getNumberOfCombinations()); stringValues.get(PmmUtils.PARAMETER).add(param.getName()); stringValues.get(PmmUtils.MODEL).add(formula.getName()); for (Map.Entry<String, List<ConditionValue>> entry : conditionValues.entrySet()) { entry.getValue().add(condRanges.get(entry.getKey())); } } } conditionValues = PmmUtils.addUnitToKey(conditionValues, units); }
From source file:dagger.internal.codegen.DuplicateBindingsValidation.java
@Override public void visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter) { Multimaps.asMap(indexEdgesBySourceAndRequest(bindingGraph)).forEach((sourceAndRequest, dependencyEdges) -> { if (dependencyEdges.size() > 1) { reportDuplicateBindings(sourceAndRequest.request(), dependencyEdges, bindingGraph, diagnosticReporter); }/*from ww w . j a va 2 s.c om*/ }); }
From source file:org.ambraproject.wombat.model.SearchFilterFactory.java
/** * The main create method of the factory. Creates a @code{SearchFilter} in three steps: * * 1. Retrieve the specified @code{SearchFilterType} using the @code{SearchFilterTypeMap} * 2. Parse the results from a faceted search into individual @code{SearchFilterItem}s * 3. Combine all SearchFilterItems into a @code{SearchFilter} object. * * Note that the @code{ArticleSearchQuery.Builder} query must be set as a faceted search * by calling setFacet()/* www .j a va 2s. c o m*/ * * @param results faceted search results returned from Solr * @param filterTypeMapKey key for the filter type * @param params URL parameters applicable to the search * @return @code{SearchFilter} object made up of one or many @code{SearchFilterItems} that contain * the faceted search results. The SearchFilterItems also house a /search URL that represents * how the filter would be applied or removed from a search. */ public SearchFilter createSearchFilter(Map<?, ?> results, String filterTypeMapKey, Multimap<String, String> params) { SearchFilterType filterType = filterTypeMap.getSearchFilterByKey(filterTypeMapKey); List<SearchFilterItem> searchFilterResult = new ArrayList<>(); for (Map.Entry<?, ?> entry : results.entrySet()) { Double numberOfHits = (Double) entry.getValue(); //displayName is often represented by the filter value String displayName = entry.getKey().toString(); ListMultimap<String, String> changedParams = applyFilterToParams(displayName, params, filterType); SearchFilterItem filterItem = new SearchFilterItem(displayName, numberOfHits.floatValue(), filterType.getParameterName(), filterType.getFilterValue(displayName), Multimaps.asMap(changedParams)); searchFilterResult.add(filterItem); } return new SearchFilter(searchFilterResult, filterTypeMapKey); }
From source file:com.neiljbrown.brighttalk.channels.reportingapi.client.common.GetSubscribersWebcastActivityRequestParamsBuilder.java
/** * @return A {@code Map<String, List<String>>} representation of thee request parameter names and their values. *///from w ww . j a v a2 s .c o m public Map<String, List<String>> asMap() { // Multimap's asMap() methods convert return Multimaps.asMap(this.params); }
From source file:com.madvay.tools.android.perf.common.Table.java
public Table<AggregateRow> groupAndAggregate(final String groupByColumn, final String weightColumn, final AggregationType aggregationType) { final RowAdapter<T> adapter = getAdapter(); final boolean groupNumeric = adapter.types .get(adapter.columns.indexOf(groupByColumn)) == RowAdapter.CoerceType.NUMERIC; boolean weightNumeric = adapter.types .get(adapter.columns.indexOf(weightColumn)) == RowAdapter.CoerceType.NUMERIC; ImmutableListMultimap<Object, T> indexed = Multimaps.index(rows, new Function<T, Object>() { @Override//w ww . j a v a 2 s . co m public Object apply(T input) { return adapter.get(input, groupByColumn).toString(); } }); Map<Object, Long> map = Maps.transformEntries(Multimaps.asMap(indexed), new Maps.EntryTransformer<Object, List<T>, Long>() { @Override public Long transformEntry(Object key, List<T> value) { switch (aggregationType) { case COUNT: return (long) value.size(); case SUM: { long l = 0; for (T t : value) { l += Long.parseLong(adapter.get(t, weightColumn).toString()); } return l; } case UNIQUE: { Set<String> set = new HashSet<>(); for (T t : value) { set.add(adapter.get(t, weightColumn).toString()); } return (long) set.size(); } } throw new IllegalArgumentException(); } }); List<AggregateRow> newRows = Lists.newArrayList( Iterables.transform(map.entrySet(), new Function<Map.Entry<Object, Long>, AggregateRow>() { @Override public AggregateRow apply(Map.Entry<Object, Long> input) { return new AggregateRow(input.getValue(), input.getKey()); } })); Table<AggregateRow> ret = new Table<AggregateRow>(newRows) { final RowAdapter<AggregateRow> adap = new AggregateRow.Adapter(groupNumeric); @Override public RowAdapter<AggregateRow> getAdapter() { return adap; } }; return ret; }