Example usage for com.google.common.collect ImmutableSetMultimap builder

List of usage examples for com.google.common.collect ImmutableSetMultimap builder

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableSetMultimap builder.

Prototype

public static <K, V> Builder<K, V> builder() 

Source Link

Document

Returns a new Builder .

Usage

From source file:uk.ac.ebi.atlas.model.ExperimentDesign.java

private void addFactorOntologyTerms(ImmutableSetMultimap.Builder<String, String> builder) {
    for (Map.Entry<String, FactorSet> factorSetEntry : factorSetMap.entrySet()) {
        String runOrAssay = factorSetEntry.getKey();
        FactorSet factorSet = factorSetEntry.getValue();

        for (Factor factor : factorSet) {
            for (OntologyTerm valueOntologyTerm : factor.getValueOntologyTerms()) {
                builder.put(runOrAssay, valueOntologyTerm.id());
            }/*from w w w.j a  v a2 s .  com*/
        }
    }
}

From source file:zipkin.storage.cassandra.Indexer.java

@VisibleForTesting
static ImmutableSetMultimap<PartitionKeyToTraceId, Long> entriesThatIncreaseGap(
        ConcurrentMap<PartitionKeyToTraceId, Pair<Long>> sharedState,
        ImmutableSetMultimap<PartitionKeyToTraceId, Long> updates) {
    ImmutableSet.Builder<PartitionKeyToTraceId> toUpdate = ImmutableSet.builder();

    // Enter a loop that affects shared state when an update widens the time interval for a key.
    for (Map.Entry<PartitionKeyToTraceId, Long> input : updates.entries()) {
        PartitionKeyToTraceId key = input.getKey();
        long timestamp = input.getValue();
        for (;;) {
            Pair<Long> oldRange = sharedState.get(key);
            if (oldRange == null) {
                // Initial state is where this key has a single timestamp.
                oldRange = sharedState.putIfAbsent(key, Pair.create(timestamp, timestamp));

                // If there was no previous value, we need to update the index
                if (oldRange == null) {
                    toUpdate.add(key);/*  w ww. j  av a  2  s  .com*/
                    break;
                }
            }

            long first = timestamp < oldRange._1 ? timestamp : oldRange._1;
            long last = timestamp > oldRange._2 ? timestamp : oldRange._2;

            Pair<Long> newRange = Pair.create(first, last);
            if (oldRange.equals(newRange)) {
                break; // the current timestamp is contained
            } else if (sharedState.replace(key, oldRange, newRange)) {
                toUpdate.add(key); // The range was extended
                break;
            }
        }
    }

    // When the loop completes, we'll know one of our updates widened the interval of a trace, if
    // it is the first or last timestamp. By ignoring those between an existing interval, we can
    // end up with less Cassandra writes.
    Builder<PartitionKeyToTraceId, Long> result = ImmutableSetMultimap.builder();
    for (PartitionKeyToTraceId needsUpdate : toUpdate.build()) {
        Pair<Long> firstLast = sharedState.get(needsUpdate);
        if (updates.containsEntry(needsUpdate, firstLast._1))
            result.put(needsUpdate, firstLast._1);
        if (updates.containsEntry(needsUpdate, firstLast._2))
            result.put(needsUpdate, firstLast._2);
    }
    return result.build();
}

From source file:com.addthis.hydra.query.MeshFileRefCache.java

public void updateFileReferenceForTask(String job, int task, Iterable<FileReference> baseSet) {
    SetMultimap<Integer, FileReference> existing = fileReferenceCache.getIfPresent(job);
    if (existing != null) {
        ImmutableSetMultimap.Builder<Integer, FileReference> withReplacement = ImmutableSetMultimap
                .<Integer, FileReference>builder();
        for (Map.Entry<Integer, Collection<FileReference>> entry : existing.asMap().entrySet()) {
            if (entry.getKey() != task) {
                withReplacement.putAll(entry.getKey(), entry.getValue());
            }//from   w w w  . ja  v a  2s .  co  m
        }
        withReplacement.putAll(task, baseSet);
        fileReferenceCache.put(job, withReplacement.build());
    }
}

From source file:com.facebook.buck.jvm.java.JavaSymbolFinder.java

/**
 * For all the possible BUCK files above each of the given source files, parse them to JSON to
 * find the targets that actually include these source files, and return a map of them. We do this
 * over a collection of source files, rather than a single file at a time, because instantiating
 * the BUCK file parser is expensive. (It spawns a Python subprocess.)
 *//*  w ww. j  a  va 2s .  c om*/
private ImmutableMultimap<Path, BuildTarget> getTargetsForSourceFiles(Collection<Path> sourceFilePaths)
        throws InterruptedException, IOException {
    Map<Path, List<Map<String, Object>>> parsedBuildFiles = Maps.newHashMap();
    ImmutableSetMultimap.Builder<Path, BuildTarget> sourceFileTargetsMultimap = ImmutableSetMultimap.builder();
    try (ProjectBuildFileParser parser = projectBuildFileParserFactory.createParser(marshaller, console,
            environment, buckEventBus, /* ignoreBuckAutodepsFiles */ false)) {
        for (Path sourceFile : sourceFilePaths) {
            for (Path buckFile : possibleBuckFilesForSourceFile(sourceFile)) {
                List<Map<String, Object>> rules;
                // Avoid parsing the same BUCK file twice.
                if (parsedBuildFiles.containsKey(buckFile)) {
                    rules = parsedBuildFiles.get(buckFile);
                } else {
                    rules = parser.getAll(buckFile);
                    parsedBuildFiles.put(buckFile, rules);
                }

                for (Map<String, Object> ruleMap : rules) {
                    String type = (String) ruleMap.get(BuckPyFunction.TYPE_PROPERTY_NAME);
                    if (javaRuleTypes.contains(type)) {
                        @SuppressWarnings("unchecked")
                        List<String> srcs = (List<String>) Preconditions.checkNotNull(ruleMap.get("srcs"));
                        if (isSourceFilePathInSrcsList(sourceFile, srcs, buckFile.getParent())) {
                            Path buckFileDir = buckFile.getParent();
                            String baseName = "//"
                                    + (buckFileDir != null ? MorePaths.pathWithUnixSeparators(buckFileDir)
                                            : "");
                            String shortName = (String) Preconditions.checkNotNull(ruleMap.get("name"));
                            sourceFileTargetsMultimap.put(sourceFile, BuildTarget
                                    .builder(projectFilesystem.getRootPath(), baseName, shortName).build());
                        }
                    }
                }
            }
        }
    } catch (BuildFileParseException e) {
        buckEventBus.post(ThrowableConsoleEvent.create(e, "Error while searching for targets."));
    }
    return sourceFileTargetsMultimap.build();
}

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

/**
 * Using missing symbol events from the build and the JavaSymbolFinder class, build a list of
 * missing dependencies for each broken target.
 *//* w  ww .  j a  v  a2s  . c  o  m*/
public ImmutableSetMultimap<BuildTarget, BuildTarget> getNeededDependencies(
        Collection<MissingSymbolEvent> missingSymbolEvents) throws InterruptedException, IOException {
    ImmutableSetMultimap.Builder<BuildTarget, String> targetsMissingSymbolsBuilder = ImmutableSetMultimap
            .builder();
    for (MissingSymbolEvent event : missingSymbolEvents) {
        if (event.getType() != MissingSymbolEvent.SymbolType.Java) {
            throw new UnsupportedOperationException("Only implemented for Java.");
        }
        targetsMissingSymbolsBuilder.put(event.getTarget(), event.getSymbol());
    }
    ImmutableSetMultimap<BuildTarget, String> targetsMissingSymbols = targetsMissingSymbolsBuilder.build();
    ImmutableSetMultimap<String, BuildTarget> symbolProviders = javaSymbolFinder
            .findTargetsForSymbols(ImmutableSet.copyOf(targetsMissingSymbols.values()));

    ImmutableSetMultimap.Builder<BuildTarget, BuildTarget> neededDeps = ImmutableSetMultimap.builder();

    for (BuildTarget target : targetsMissingSymbols.keySet()) {
        for (String symbol : targetsMissingSymbols.get(target)) {
            // TODO(oconnor663): Properly handle symbols that are defined in more than one place.
            // TODO(oconnor663): Properly handle target visibility.
            neededDeps.putAll(target, ImmutableSortedSet.copyOf(symbolProviders.get(symbol)));
        }
    }

    return neededDeps.build();
}

From source file:zipkin2.storage.cassandra.v1.Indexer.java

@VisibleForTesting
static ImmutableSetMultimap<PartitionKeyToTraceId, Long> entriesThatIncreaseGap(
        ConcurrentMap<PartitionKeyToTraceId, Pair> sharedState,
        ImmutableSetMultimap<PartitionKeyToTraceId, Long> updates) {
    ImmutableSet.Builder<PartitionKeyToTraceId> toUpdate = ImmutableSet.builder();

    // Enter a loop that affects shared state when an update widens the time interval for a key.
    for (Map.Entry<PartitionKeyToTraceId, Long> input : updates.entries()) {
        PartitionKeyToTraceId key = input.getKey();
        long timestamp = input.getValue();
        for (;;) {
            Pair oldRange = sharedState.get(key);
            if (oldRange == null) {
                // Initial state is where this key has a single timestamp.
                oldRange = sharedState.putIfAbsent(key, new Pair(timestamp, timestamp));

                // If there was no previous value, we need to update the index
                if (oldRange == null) {
                    toUpdate.add(key);//from  w  ww. j a  v  a 2 s  . co m
                    break;
                }
            }

            long first = timestamp < oldRange.left ? timestamp : oldRange.left;
            long last = timestamp > oldRange.right ? timestamp : oldRange.right;

            Pair newRange = new Pair(first, last);
            if (oldRange.equals(newRange)) {
                break; // the current timestamp is contained
            } else if (sharedState.replace(key, oldRange, newRange)) {
                toUpdate.add(key); // The range was extended
                break;
            }
        }
    }

    // When the loop completes, we'll know one of our updates widened the interval of a trace, if
    // it is the first or last timestamp. By ignoring those between an existing interval, we can
    // end up with less Cassandra writes.
    Builder<PartitionKeyToTraceId, Long> result = ImmutableSetMultimap.builder();
    for (PartitionKeyToTraceId needsUpdate : toUpdate.build()) {
        Pair firstLast = sharedState.get(needsUpdate);
        if (updates.containsEntry(needsUpdate, firstLast.left))
            result.put(needsUpdate, firstLast.left);
        if (updates.containsEntry(needsUpdate, firstLast.right))
            result.put(needsUpdate, firstLast.right);
    }
    return result.build();
}

From source file:org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule.java

/**
 * Return measures that were added by the step (using {@link #add(Component, Metric, Measure)}).
 * It does not contain the one added in the test by {@link #addRawMeasure(int, String, Measure)}
 *///from www . jav  a2s.c o m
public SetMultimap<String, Measure> getAddedRawMeasures(Component component) {
    checkAndInitProvidersState();

    ImmutableSetMultimap.Builder<String, Measure> builder = ImmutableSetMultimap.builder();
    for (Map.Entry<InternalKey, Measure> entry : from(
            filterKeys(rawMeasures, hasComponentRef(component)).entrySet()).filter(isAddedMeasure)) {
        builder.put(entry.getKey().getMetricKey(), entry.getValue());
    }
    return builder.build();
}

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;
        }/*w  w w .j  a  va2s.  c  om*/
        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();
}

From source file:com.facebook.buck.rules.DefaultJavaLibraryRule.java

protected DefaultJavaLibraryRule(BuildRuleParams buildRuleParams, Set<String> srcs, Set<String> resources,
        @Nullable String proguardConfig, AnnotationProcessingParams annotationProcessingParams,
        boolean exportDeps, String sourceLevel, String targetLevel) {
    super(buildRuleParams);
    this.srcs = ImmutableSortedSet.copyOf(srcs);
    this.resources = ImmutableSortedSet.copyOf(resources);
    this.annotationProcessingParams = Preconditions.checkNotNull(annotationProcessingParams);
    this.proguardConfig = proguardConfig;
    this.sourceLevel = sourceLevel;
    this.targetLevel = targetLevel;
    this.exportDeps = exportDeps;

    if (!srcs.isEmpty() || !resources.isEmpty()) {
        File file = new File(getOutputJarPath(getBuildTarget()));
        this.outputJar = Optional.of(file);
    } else {//w  ww  . j a v a 2  s. c o m
        this.outputJar = Optional.absent();
    }

    // Note that both srcs and resources are sorted so that the list order is consistent even if
    // the iteration order of the sets passed to the constructor changes. See
    // AbstractBuildRule.getInputsToCompareToOutput() for details.
    inputsToConsiderForCachingPurposes = ImmutableList.<String>builder().addAll(this.srcs)
            .addAll(this.resources).build();

    outputClasspathEntriesSupplier = Suppliers.memoize(new Supplier<ImmutableSet<String>>() {
        @Override
        public ImmutableSet<String> get() {
            ImmutableSet<String> outputClasspathEntries;

            // If this java_library exports its dependencies then just return the transitive
            // dependencies.
            if (DefaultJavaLibraryRule.this.exportDeps) {
                outputClasspathEntries = ImmutableSet.copyOf(getTransitiveClasspathEntries().values());
            } else if (outputJar.isPresent()) {
                outputClasspathEntries = ImmutableSet.of(getOutput().getPath());
            } else {
                outputClasspathEntries = ImmutableSet.of();
            }

            return outputClasspathEntries;
        }
    });

    transitiveClasspathEntriesSupplier = Suppliers
            .memoize(new Supplier<ImmutableSetMultimap<BuildRule, String>>() {
                @Override
                public ImmutableSetMultimap<BuildRule, String> get() {
                    final ImmutableSetMultimap.Builder<BuildRule, String> classpathEntries = ImmutableSetMultimap
                            .builder();
                    ImmutableSetMultimap<BuildRule, String> classpathEntriesForDeps = Classpaths
                            .getClasspathEntries(getDeps());

                    classpathEntries.putAll(classpathEntriesForDeps);

                    if (DefaultJavaLibraryRule.this.exportDeps) {
                        classpathEntries.putAll(DefaultJavaLibraryRule.this, classpathEntriesForDeps.values());
                    }

                    // Only add ourselves to the classpath if there's a jar to be built.
                    if (outputJar.isPresent()) {
                        classpathEntries.putAll(DefaultJavaLibraryRule.this, getOutput().getPath());
                    }

                    return classpathEntries.build();
                }
            });

    declaredClasspathEntriesSupplier = Suppliers
            .memoize(new Supplier<ImmutableSetMultimap<BuildRule, String>>() {
                @Override
                public ImmutableSetMultimap<BuildRule, String> get() {
                    final ImmutableSetMultimap.Builder<BuildRule, String> classpathEntries = ImmutableSetMultimap
                            .builder();

                    Iterable<JavaLibraryRule> javaLibraryDeps = Iterables.filter(
                            Sets.union(getDeps(), ImmutableSet.of(DefaultJavaLibraryRule.this)),
                            JavaLibraryRule.class);

                    for (JavaLibraryRule rule : javaLibraryDeps) {
                        classpathEntries.putAll(rule, rule.getOutputClasspathEntries());
                    }
                    return classpathEntries.build();
                }
            });
}

From source file:io.prestosql.metadata.DiscoveryNodeManager.java

private synchronized void refreshNodesInternal() {
    // This is currently a blacklist.
    // TODO: make it a whitelist (a failure-detecting service selector) and maybe build in support for injecting this in airlift
    Set<ServiceDescriptor> services = serviceSelector.selectAllServices().stream()
            .filter(service -> !failureDetector.getFailed().contains(service)).collect(toImmutableSet());

    ImmutableSet.Builder<Node> activeNodesBuilder = ImmutableSet.builder();
    ImmutableSet.Builder<Node> inactiveNodesBuilder = ImmutableSet.builder();
    ImmutableSet.Builder<Node> shuttingDownNodesBuilder = ImmutableSet.builder();
    ImmutableSet.Builder<Node> coordinatorsBuilder = ImmutableSet.builder();
    ImmutableSetMultimap.Builder<ConnectorId, Node> byConnectorIdBuilder = ImmutableSetMultimap.builder();

    for (ServiceDescriptor service : services) {
        URI uri = getHttpUri(service, httpsRequired);
        NodeVersion nodeVersion = getNodeVersion(service);
        boolean coordinator = isCoordinator(service);
        if (uri != null && nodeVersion != null) {
            PrestoNode node = new PrestoNode(service.getNodeId(), uri, nodeVersion, coordinator);
            NodeState nodeState = getNodeState(node);

            switch (nodeState) {
            case ACTIVE:
                activeNodesBuilder.add(node);
                if (coordinator) {
                    coordinatorsBuilder.add(node);
                }//from   w ww .java2  s.  c  o m

                // record available active nodes organized by connector id
                String connectorIds = service.getProperties().get("connectorIds");
                if (connectorIds != null) {
                    connectorIds = connectorIds.toLowerCase(ENGLISH);
                    for (String connectorId : CONNECTOR_ID_SPLITTER.split(connectorIds)) {
                        byConnectorIdBuilder.put(new ConnectorId(connectorId), node);
                    }
                }

                // always add system connector
                byConnectorIdBuilder.put(new ConnectorId(GlobalSystemConnector.NAME), node);
                break;
            case INACTIVE:
                inactiveNodesBuilder.add(node);
                break;
            case SHUTTING_DOWN:
                shuttingDownNodesBuilder.add(node);
                break;
            default:
                log.error("Unknown state %s for node %s", nodeState, node);
            }
        }
    }

    if (allNodes != null) {
        // log node that are no longer active (but not shutting down)
        SetView<Node> missingNodes = difference(allNodes.getActiveNodes(),
                Sets.union(activeNodesBuilder.build(), shuttingDownNodesBuilder.build()));
        for (Node missingNode : missingNodes) {
            log.info("Previously active node is missing: %s (last seen at %s)", missingNode.getNodeIdentifier(),
                    missingNode.getHostAndPort());
        }
    }

    // nodes by connector id changes anytime a node adds or removes a connector (note: this is not part of the listener system)
    activeNodesByConnectorId = byConnectorIdBuilder.build();

    AllNodes allNodes = new AllNodes(activeNodesBuilder.build(), inactiveNodesBuilder.build(),
            shuttingDownNodesBuilder.build(), coordinatorsBuilder.build());
    // only update if all nodes actually changed (note: this does not include the connectors registered with the nodes)
    if (!allNodes.equals(this.allNodes)) {
        // assign allNodes to a local variable for use in the callback below
        this.allNodes = allNodes;
        coordinators = coordinatorsBuilder.build();

        // notify listeners
        List<Consumer<AllNodes>> listeners = ImmutableList.copyOf(this.listeners);
        nodeStateEventExecutor.submit(() -> listeners.forEach(listener -> listener.accept(allNodes)));
    }
}