Example usage for com.google.common.collect Multimap putAll

List of usage examples for com.google.common.collect Multimap putAll

Introduction

In this page you can find the example usage for com.google.common.collect Multimap putAll.

Prototype

boolean putAll(@Nullable K key, Iterable<? extends V> values);

Source Link

Document

Stores a key-value pair in this multimap for each of values , all using the same key, key .

Usage

From source file:com.facebook.buck.cli.FineGrainedJavaDependencySuggester.java

/**
 * Extracts the features from {@code src} and updates the collections accordingly.
 *//*ww w  .  j a  v  a  2  s  .c o  m*/
private void extractProvidedSymbolInfoFromSourceFile(SourcePath src, JavaFileParser javaFileParser,
        Multimap<String, String> providedSymbolToRequiredSymbols,
        Map<String, PathSourcePath> providedSymbolToSrc) {
    if (!(src instanceof PathSourcePath)) {
        return;
    }

    PathSourcePath path = (PathSourcePath) src;
    ProjectFilesystem filesystem = path.getFilesystem();
    Optional<String> contents = filesystem.readFileIfItExists(path.getRelativePath());
    if (!contents.isPresent()) {
        throw new RuntimeException(String.format("Could not read file '%s'", path.getRelativePath()));
    }

    JavaFileParser.JavaFileFeatures features = javaFileParser.extractFeaturesFromJavaCode(contents.get());
    // If there are multiple provided symbols, that is because there are inner classes. Choosing
    // the shortest name will effectively select the top-level type.
    String providedSymbol = Iterables.getFirst(features.providedSymbols, /* defaultValue */ null);
    if (providedSymbol == null) {
        console.getStdErr().printf("%s cowardly refuses to provide any types.\n", path.getRelativePath());
        return;
    }

    providedSymbolToSrc.put(providedSymbol, path);
    providedSymbolToRequiredSymbols.putAll(providedSymbol, features.requiredSymbols);
    providedSymbolToRequiredSymbols.putAll(providedSymbol, features.exportedSymbols);
}

From source file:com.palantir.atlasdb.keyvalue.partition.BasicPartitionMap.java

@Override
public Multimap<ConsistentRingRangeRequest, KeyValueService> getServicesForRangeRead(String tableName,
        RangeRequest range) {//from ww  w  .j a va2  s.c  o m
    if (range.isReverse()) {
        throw new UnsupportedOperationException();
    }
    Multimap<ConsistentRingRangeRequest, KeyValueService> result = LinkedHashMultimap.create();

    byte[] rangeStart = range.getStartInclusive();
    if (range.getStartInclusive().length == 0) {
        rangeStart = RangeRequests.getFirstRowName();
    }

    // Note that there is no wrapping around when traversing the circle with the key.
    // Ie. the range does not go over through "zero" of the ring.
    while (range.inRange(rangeStart)) {

        // Setup the consistent subrange
        byte[] rangeEnd = ring.higherKey(rangeStart);
        if (rangeEnd == null || !range.inRange(rangeEnd)) {
            rangeEnd = range.getEndExclusive();
        }

        ConsistentRingRangeRequest crrr = ConsistentRingRangeRequest
                .of(range.getBuilder().startRowInclusive(rangeStart).endRowExclusive(rangeEnd).build());

        Preconditions.checkState(!crrr.get().isEmptyRange());

        // We have now the "consistent" subrange which means that
        // every service having the (inclusive) start row will also
        // have all the other rows belonging to this range.
        // No other services will have any of these rows.
        result.putAll(crrr, getServicesHavingRow(rangeStart));

        // Proceed with next range
        rangeStart = ring.higherKey(rangeStart);
        // We are out of ranges to consider.
        if (rangeStart == null) {
            break;
        }
    }
    return result;
}

From source file:com.google.devtools.build.lib.analysis.config.BuildConfiguration.java

/**
 * Returns the config fragment options classes used by the given fragment types.
 *///w  w  w . j a  v a2  s  .com
public static Set<Class<? extends FragmentOptions>> getOptionsClasses(
        Iterable<Class<? extends Fragment>> fragmentClasses, RuleClassProvider ruleClassProvider) {

    Multimap<Class<? extends BuildConfiguration.Fragment>, Class<? extends FragmentOptions>> fragmentToRequiredOptions = ArrayListMultimap
            .create();
    for (ConfigurationFragmentFactory fragmentLoader : ((ConfiguredRuleClassProvider) ruleClassProvider)
            .getConfigurationFragments()) {
        fragmentToRequiredOptions.putAll(fragmentLoader.creates(), fragmentLoader.requiredOptions());
    }
    Set<Class<? extends FragmentOptions>> options = new HashSet<>();
    for (Class<? extends BuildConfiguration.Fragment> fragmentClass : fragmentClasses) {
        options.addAll(fragmentToRequiredOptions.get(fragmentClass));
    }
    return options;
}

From source file:com.google.devtools.build.lib.query2.SkyQueryEnvironment.java

/**
 * Returns FileValue keys for which there may be relevant (from the perspective of {@link
 * #getRBuildFiles}) FileValues in the graph corresponding to the given {@code pathFragments},
 * which are assumed to be file paths.//  w ww. j  a  v  a 2 s .c om
 *
 * <p>To do this, we emulate the {@link ContainingPackageLookupFunction} logic: for each given
 * file path, we look for the nearest ancestor directory (starting with its parent directory), if
 * any, that has a package. The {@link PackageLookupValue} for this package tells us the package
 * root that we should use for the {@link RootedPath} for the {@link FileValue} key.
 *
 * <p>Note that there may not be nodes in the graph corresponding to the returned SkyKeys.
 */
Collection<SkyKey> getSkyKeysForFileFragments(Iterable<PathFragment> pathFragments)
        throws InterruptedException {
    Set<SkyKey> result = new HashSet<>();
    Multimap<PathFragment, PathFragment> currentToOriginal = ArrayListMultimap.create();
    for (PathFragment pathFragment : pathFragments) {
        currentToOriginal.put(pathFragment, pathFragment);
    }
    while (!currentToOriginal.isEmpty()) {
        Multimap<SkyKey, PathFragment> packageLookupKeysToOriginal = ArrayListMultimap.create();
        Multimap<SkyKey, PathFragment> packageLookupKeysToCurrent = ArrayListMultimap.create();
        for (Entry<PathFragment, PathFragment> entry : currentToOriginal.entries()) {
            PathFragment current = entry.getKey();
            PathFragment original = entry.getValue();
            for (SkyKey packageLookupKey : getPkgLookupKeysForFile(original, current)) {
                packageLookupKeysToOriginal.put(packageLookupKey, original);
                packageLookupKeysToCurrent.put(packageLookupKey, current);
            }
        }
        Map<SkyKey, SkyValue> lookupValues = graph.getSuccessfulValues(packageLookupKeysToOriginal.keySet());
        for (Map.Entry<SkyKey, SkyValue> entry : lookupValues.entrySet()) {
            SkyKey packageLookupKey = entry.getKey();
            PackageLookupValue packageLookupValue = (PackageLookupValue) entry.getValue();
            if (packageLookupValue.packageExists()) {
                Collection<PathFragment> originalFiles = packageLookupKeysToOriginal.get(packageLookupKey);
                Preconditions.checkState(!originalFiles.isEmpty(), entry);
                for (PathFragment fileName : originalFiles) {
                    result.add(FileValue.key(RootedPath.toRootedPath(packageLookupValue.getRoot(), fileName)));
                }
                for (PathFragment current : packageLookupKeysToCurrent.get(packageLookupKey)) {
                    currentToOriginal.removeAll(current);
                }
            }
        }
        Multimap<PathFragment, PathFragment> newCurrentToOriginal = ArrayListMultimap.create();
        for (PathFragment pathFragment : currentToOriginal.keySet()) {
            PathFragment parent = pathFragment.getParentDirectory();
            if (parent != null) {
                newCurrentToOriginal.putAll(parent, currentToOriginal.get(pathFragment));
            }
        }
        currentToOriginal = newCurrentToOriginal;
    }
    return result;
}

From source file:me.lukasczyk.busybox_preprocessor.core.Algorithm.java

/**
 * Returns a multi map of needed files per file
 *
 * @return Multi map [file needs files]//from  www . ja  va2s.  c om
 * @throws InterruptedException In case of interruption by shutdown notifier
 */
private Multimap<String, String> getNeededFilesPerFile() throws InterruptedException {
    final Map<String, String> exportedFunctions = getExportedFunctions();
    final Multimap<String, String> importedFunctions = getImportedFunctions();
    final Multimap<String, String> neededFilesPerFile = LinkedListMultimap.create();

    for (XMLInterfaceParser parser : interfaceParsers) {
        if (parser.getFileName().contains("lib")) {
            continue;
        }
        shutdownNotifier.shutdownIfNecessary();
        final Deque<String> waitList = new LinkedList<>();
        waitList.addAll(importedFunctions.get(parser.getFileName()));
        final List<String> visited = new LinkedList<>();
        final Set<String> fileNames = new LinkedHashSet<>();

        while (!waitList.isEmpty()) {
            final String function = waitList.poll();

            if (visited.contains(function)) {
                continue;
            }

            visited.add(function);

            if (!exportedFunctions.containsKey(function)) {
                continue;
            }

            final String fileName = exportedFunctions.get(function);

            if (!importedFunctions.containsKey(fileName)) {
                continue;
            }

            shutdownNotifier.shutdownIfNecessary();

            fileNames.add(fileName);
            waitList.addAll(importedFunctions.get(fileName));
        }
        if (fileNames.size() > 0) {
            neededFilesPerFile.putAll(parser.getFileName(), fileNames);
        } else {
            neededFilesPerFile.put(parser.getFileName(), "");
        }
    }

    return neededFilesPerFile;
}

From source file:org.jclouds.rest.internal.RestAnnotationProcessor.java

private Multimap<String, Object> getQueryParamKeyValues(Invocation invocation) {
    Multimap<String, Object> queryParamValues = LinkedHashMultimap.create();
    for (Parameter param : parametersWithAnnotation(invocation.getInvokable(), QueryParam.class)) {
        QueryParam queryParam = param.getAnnotation(QueryParam.class);
        String paramKey = queryParam.value();
        Optional<?> paramValue = getParamValue(invocation, param.getAnnotation(ParamParser.class),
                param.hashCode(), paramKey);
        if (paramValue.isPresent())
            if (paramValue.get() instanceof Iterable) {
                @SuppressWarnings("unchecked")
                Iterable<String> iterableStrings = transform(Iterable.class.cast(paramValue.get()),
                        toStringFunction());
                queryParamValues.putAll(paramKey, iterableStrings);
            } else {
                queryParamValues.put(paramKey, paramValue.get().toString());
            }/*from w w  w .j a v a2 s . com*/
    }
    return queryParamValues;
}

From source file:to.lean.tools.gmail.importer.gmail.Mailbox.java

Multimap<LocalMessage, Message> mapMessageIds(Iterable<LocalMessage> localMessages) {
    Multimap<LocalMessage, Message> results = MultimapBuilder.hashKeys().linkedListValues().build();

    Gmail gmail = gmailService.getServiceWithRetries();
    BatchRequest batch = gmail.batch();// w w w . j a  va2  s  .c  om

    try {
        for (LocalMessage localMessage : localMessages) {
            gmail.users().messages().list(user.getEmailAddress())
                    .setQ("rfc822msgid:" + localMessage.getMessageId()).setFields("messages(id)")
                    .queue(batch, new JsonBatchCallback<ListMessagesResponse>() {
                        @Override
                        public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders)
                                throws IOException {
                            System.err.println("Could not get message: " + localMessage.getMessageId());
                            System.err.println("  because: " + e);
                        }

                        @Override
                        public void onSuccess(ListMessagesResponse response, HttpHeaders responseHeaders)
                                throws IOException {
                            if (!response.isEmpty()) {
                                results.putAll(localMessage, response.getMessages());
                                System.err.println("For " + localMessage.getMessageId() + " got:");
                                response.getMessages().stream().forEach(
                                        message -> System.err.println("  message id: " + message.getId()));
                            }
                        }
                    });
        }
        if (batch.size() > 0) {
            batch.execute();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    return results;
}

From source file:com.trebogeer.jcql.JCQLMain.java

/**
 * main routine generating java code/*from   w w  w. j a  va2s .c  om*/
 *
 * @throws IOException thrown if unable to write generated code to files
 */
public void exec() throws IOException {
    String keyspace = cfg.keysapce;
    Cluster.Builder b = Cluster.builder().addContactPoint(cfg.dbHost).withPort(Integer.valueOf(cfg.dbPort));
    if (cfg.userName != null && !"".equals(cfg.userName.trim()) && cfg.password != null
            && !"".equals(cfg.password.trim())) {
        b = b.withAuthProvider(new PlainTextAuthProvider(cfg.userName, cfg.password));
    } else {
        logger.info("No auth will be used. Either credentials are not provided or are incorrect.");
    }

    try (Cluster c = b.build(); Session s = c.connect(keyspace)) {

        Multimap<String, Pair<String, DataType>> beans = HashMultimap.create();
        Multimap<String, Pair<String, ColumnMetadata>> tables = HashMultimap.create();
        ArrayListMultimap<String, String> partitionKeys = ArrayListMultimap.create();

        Collection<UserType> types = s.getCluster().getMetadata().getKeyspace(keyspace).getUserTypes();

        for (UserType t : types) {
            String name = t.getTypeName();
            Set<Pair<String, DataType>> fields = new HashSet<Pair<String, DataType>>();
            for (String field : t.getFieldNames()) {
                DataType dt = t.getFieldType(field);
                fields.add(Pair.with(field, dt));
            }
            beans.putAll(name, fields);
        }
        Collection<TableMetadata> tbls = s.getCluster().getMetadata().getKeyspace(keyspace).getTables();
        for (TableMetadata t : tbls) {
            String name = t.getName();
            for (ColumnMetadata clmdt : t.getPartitionKey()) {
                partitionKeys.put(name, clmdt.getName());
            }
            partitionKeys.trimToSize();
            Set<Pair<String, ColumnMetadata>> fields = new HashSet<>();
            for (ColumnMetadata field : t.getColumns()) {
                fields.add(Pair.with(field.getName(), field));
            }
            tables.putAll(name, fields);
        }

        generateModelCode(beans, tables, partitionKeys);
        generateAccessCode(s);

        if ("y".equalsIgnoreCase(cfg.toString)) {
            toStringMethods();
        }

        if ("y".equalsIgnoreCase(cfg.printInfo)) {
            info();
        }
        if ("y".equalsIgnoreCase(cfg.debug)) {
            model.build(new SingleStreamCodeWriter(System.out));
        } else {
            File source = new File(cfg.generatedSourceDir);
            if (source.exists() || source.mkdirs()) {
                model.build(new File(cfg.generatedSourceDir));
            }
        }
    }

}

From source file:it.sayservice.platform.smartplanner.cache.CacheManager.java

private CalendarBuildResult buildCalendarFile(String router, String agencyId, String routeId,
        Multimap<String, String> daysMap, List<String> written, Multimap<CacheEntryStatus, String> result,
        String dir) throws Exception {
    CalendarBuildResult buildResult = new CalendarBuildResult();
    Set<String> empty = new TreeSet<String>();
    List<String> calendar = manager.getRouteCalendarEntries(router, routeId);

    Multimap<List<String>, String> reversedDaysMap = ArrayListMultimap.create();
    Multimap<String, String> compactReversedDaysMap = ArrayListMultimap.create();
    for (String day : daysMap.keySet()) {
        List<String> sl = new ArrayList<String>(daysMap.get(day));
        Collections.sort(sl);/*  w  w  w.  ja va  2s . c  o  m*/
        String dayKey = getEqString(sl, agencyId);
        reversedDaysMap.put(sl, day);
    }

    SortedSet<String> calendarData = new TreeSet<String>();
    for (List<String> key : reversedDaysMap.keySet()) {
        List<String> shortKey = new ArrayList<String>(key);
        for (Iterator<String> iterator = shortKey.iterator(); iterator.hasNext();) {
            String next = iterator.next().replace(agencyId + "_", "");
            if (!calendar.contains(next)) {
                iterator.remove();
            }
        }

        String sk = getEqString(shortKey, agencyId);
        compactReversedDaysMap.putAll(sk, reversedDaysMap.get(key));

        for (String day : compactReversedDaysMap.get(sk)) {
            if (!shortKey.isEmpty()) {
                calendarData.add("\"" + day + "\":\"" + sk + "\"");
            } else {
                calendarData.add("\"" + day + "\":\"" + null + "\"");
                empty.add(sk);
            }
        }
    }

    StringBuilder sb = new StringBuilder();
    sb.append("{\n");
    for (String line : calendarData) {
        sb.append(line + ",\n");
    }
    sb.replace(sb.length() - 2, sb.length() - 1, "");
    sb.append("};");
    String c = sb.toString();

    String cn = "calendar_" + routeId + ".js";
    String fn = dir + "/" + cn;

    boolean toIndex = !compare(fn, c);
    written.add(cn);
    result.put(CacheEntryStatus.WRITTEN, "calendar_" + routeId);
    if (toIndex) {
        result.put(CacheEntryStatus.ADDED, "calendar_" + routeId);
    }

    FileWriter fw = new FileWriter(fn);
    fw.write(c);
    fw.close();

    buildResult.setCalendar(compactReversedDaysMap);
    buildResult.setEmpty(empty);

    return buildResult;
}

From source file:org.apache.sentry.provider.file.SimplePolicyEngine.java

private ImmutableSetMultimap<String, String> parsePermissions(@Nullable String database,
        Ini.Section rolesSection, Ini.Section groupsSection) {
    ImmutableSetMultimap.Builder<String, String> resultBuilder = ImmutableSetMultimap.builder();
    Multimap<String, String> roleNameToPrivilegeMap = HashMultimap.create();
    List<? extends RoleValidator> validators = Lists.newArrayList(new ServersAllIsInvalid(),
            new DatabaseMustMatch(), new DatabaseRequiredInRole(), new ServerNameMustMatch(serverName));
    for (Map.Entry<String, String> entry : rolesSection.entrySet()) {
        String roleName = Strings.nullToEmpty(entry.getKey()).trim();
        String roleValue = Strings.nullToEmpty(entry.getValue()).trim();
        boolean invalidConfiguration = false;
        if (roleName.isEmpty()) {
            LOGGER.warn("Empty role name encountered in {}", resourcePath);
            invalidConfiguration = true;
        }//from   ww  w.j  ava  2 s.c  o m
        if (roleValue.isEmpty()) {
            LOGGER.warn("Empty role value encountered in {}", resourcePath);
            invalidConfiguration = true;
        }
        if (roleNameToPrivilegeMap.containsKey(roleName)) {
            LOGGER.warn("Role {} defined twice in {}", roleName, resourcePath);
        }
        Set<String> roles = PermissionUtils.toPermissionStrings(roleValue);
        if (!invalidConfiguration && roles != null) {
            for (String role : roles) {
                for (RoleValidator validator : validators) {
                    validator.validate(database, role.trim());
                }
            }
            roleNameToPrivilegeMap.putAll(roleName, roles);
        }
    }
    Splitter roleSplitter = ROLE_SPLITTER.omitEmptyStrings().trimResults();
    for (Map.Entry<String, String> entry : groupsSection.entrySet()) {
        String groupName = Strings.nullToEmpty(entry.getKey()).trim();
        String groupPrivileges = Strings.nullToEmpty(entry.getValue()).trim();
        Collection<String> resolvedGroupPrivileges = Sets.newHashSet();
        for (String roleName : roleSplitter.split(groupPrivileges)) {
            if (roleNameToPrivilegeMap.containsKey(roleName)) {
                resolvedGroupPrivileges.addAll(roleNameToPrivilegeMap.get(roleName));
            } else {
                LOGGER.warn("Role {} for group {} does not exist in privileges section in {}",
                        new Object[] { roleName, groupName, resourcePath });
            }
        }
        resultBuilder.putAll(groupName, resolvedGroupPrivileges);
    }
    return resultBuilder.build();
}