Example usage for com.google.common.collect Sets union

List of usage examples for com.google.common.collect Sets union

Introduction

In this page you can find the example usage for com.google.common.collect Sets union.

Prototype

public static <E> SetView<E> union(final Set<? extends E> set1, final Set<? extends E> set2) 

Source Link

Document

Returns an unmodifiable view of the union of two sets.

Usage

From source file:es.usc.citius.hipster.util.graph.HashBasedHipsterGraph.java

@Override
public Iterable<GraphEdge<V, E>> edgesOf(V vertex) {
    return Sets.union(Sets.newHashSet(graphTable.row(vertex).values()),
            Sets.newHashSet(graphTable.column(vertex).values()));
}

From source file:edu.mit.streamjit.impl.blob.DrainData.java

/**
 * Merge this DrainData with the given DrainData object, appending any
 * channel data to the data in this DrainData (just as adding to a List
 * appends at the end).//from   w  w w.  ja  va  2  s.c o  m
 * @param other the DrainData to merge with
 * @return a merged DrainData
 */
public DrainData merge(DrainData other) {
    ImmutableMap.Builder<Token, ImmutableList<Object>> dataBuilder = ImmutableMap.builder();
    for (Token t : Sets.union(data.keySet(), other.data.keySet())) {
        ImmutableList<Object> us = getData(t) != null ? getData(t) : ImmutableList.of();
        ImmutableList<Object> them = other.getData(t) != null ? other.getData(t) : ImmutableList.of();
        dataBuilder.put(t, ImmutableList.builder().addAll(us).addAll(them).build());
    }

    if (!Sets.intersection(state.rowKeySet(), other.state.rowKeySet()).isEmpty())
        throw new IllegalArgumentException("bad merge: one worker's state split across DrainData");
    return new DrainData(dataBuilder.build(), CollectionUtils.union(state, other.state));
}

From source file:com.continuuity.loom.layout.Solver.java

/**
 * Add services to a cluster, returning which nodes were affected by the change or null if there was no way to
 * add the services to the cluster.//w  ww  .  j  a  v a2s.  com
 *
 * @param cluster Cluster to add the services to.
 * @param clusterNodes Nodes in the cluster.
 * @param servicesToAdd Services to add to the cluster.
 * @return Nodes that need to have services added to them.
 * @throws Exception
 */
public Set<Node> addServicesToCluster(Cluster cluster, Set<Node> clusterNodes, Set<String> servicesToAdd)
        throws Exception {
    EntityStoreView entityStore = entityStoreService.getView(cluster.getAccount());
    Map<String, Service> serviceMap = getServiceMap(Sets.union(cluster.getServices(), servicesToAdd),
            entityStore);
    validateServiceCompatibilities(cluster.getClusterTemplate().getCompatibilities(), servicesToAdd);
    validateServiceDependencies(serviceMap);

    ClusterLayoutTracker tracker = updater.addServicesToCluster(cluster, clusterNodes, servicesToAdd);
    if (tracker == null) {
        return null;
    }

    Set<Node> changedNodes = Sets.newHashSet();
    for (ClusterLayoutChange change : tracker.getChanges()) {
        changedNodes.addAll(change.applyChange(cluster, clusterNodes, serviceMap));
    }
    return changedNodes;
}

From source file:sandbox.sfwatergit.utils.pt.gtfsUtils.merging.NetworkMerger.java

public static Network mergeNetworks(final Network networkA, final Network networkB) {
    final Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig());
    scenario.getConfig().transit().setUseTransit(true);
    final Network mergedNetwork = scenario.getNetwork();
    final NetworkFactory factory = mergedNetwork.getFactory();

    log.info("Merging networks...");
    // Nodes/*from  www  .  j  a  va  2s  .co m*/
    for (Node node : networkA.getNodes().values()) {
        Node newNode = factory.createNode(Id.create(node.getId().toString(), Node.class), node.getCoord());
        mergedNetwork.addNode(newNode);
    }

    networkB.getNodes().values().stream().filter(node -> !networkA.getNodes().containsKey(node.getId()))
            .forEach(node -> {
                Node newNode = factory.createNode(Id.create(node.getId().toString(), Node.class),
                        node.getCoord());
                mergedNetwork.addNode(newNode);
            });

    // Links
    double capacityFactor = mergedNetwork.getCapacityPeriod() / networkA.getCapacityPeriod();
    for (Link link : networkA.getLinks().values()) {
        Id<Node> fromNodeId = Id.create(link.getFromNode().getId().toString(), Node.class);
        Id<Node> toNodeId = Id.create(link.getToNode().getId().toString(), Node.class);
        Link newLink = factory.createLink(Id.create(link.getId().toString(), Link.class),
                mergedNetwork.getNodes().get(fromNodeId), mergedNetwork.getNodes().get(toNodeId));
        newLink.setAllowedModes(link.getAllowedModes());
        newLink.setCapacity(link.getCapacity() * capacityFactor);
        newLink.setFreespeed(link.getFreespeed());
        newLink.setLength(link.getLength());
        newLink.setNumberOfLanes(link.getNumberOfLanes());
        mergedNetwork.addLink(newLink);
    }

    capacityFactor = mergedNetwork.getCapacityPeriod() / networkB.getCapacityPeriod();
    for (Link link : networkB.getLinks().values()) {
        final Set<String> allowedModes = link.getAllowedModes();
        if (mergedNetwork.getLinks().containsKey(link.getId())) {
            final Link existingLink = mergedNetwork.getLinks().get(link.getId());
            final Set<String> existingModes = existingLink.getAllowedModes();
            if (!existingModes.equals(allowedModes)) {
                existingLink.setAllowedModes(Sets.union(existingModes, allowedModes));
            }
        } else {
            Id<Node> fromNodeId = Id.create(link.getFromNode().getId().toString(), Node.class);
            Id<Node> toNodeId = Id.create(link.getToNode().getId().toString(), Node.class);
            Link newLink = factory.createLink(Id.create(link.getId().toString(), Link.class),
                    mergedNetwork.getNodes().get(fromNodeId), mergedNetwork.getNodes().get(toNodeId));
            newLink.setAllowedModes(allowedModes);
            newLink.setCapacity(link.getCapacity() * capacityFactor);
            newLink.setFreespeed(link.getFreespeed());
            newLink.setLength(link.getLength());
            newLink.setNumberOfLanes(link.getNumberOfLanes());
            mergedNetwork.addLink(newLink);
        }
    }

    log.info(" Merging Stats:");
    log.info("  Number of links network A: " + networkA.getLinks().size());
    log.info("  Number of nodes network A: " + networkA.getNodes().size());
    log.info("  Number of links network B: " + networkB.getLinks().size());
    log.info("  Number of nodes network B: " + networkB.getNodes().size());
    log.info("  Sum of links: " + (networkA.getLinks().size() + networkB.getLinks().size()));
    log.info("  Sum of nodes: " + (networkA.getNodes().size() + networkB.getNodes().size()));
    log.info("  Number of links merged: " + mergedNetwork.getLinks().size());
    log.info("  Number of nodes merged: " + mergedNetwork.getNodes().size());

    log.info("Merging networks... done.");
    return mergedNetwork;
}

From source file:org.opendaylight.nic.compiler.Transform.java

private Set<Action> merge(Set<Action> a1, Set<Action> a2) throws IntentCompilerException {
    Set<Action> composebleActions = new LinkedHashSet<>();
    Set<Action> observableActions = new LinkedHashSet<>();
    Set<Action> exclusiveActions = new LinkedHashSet<>();
    for (Action action : Sets.union(a1, a2)) {
        switch (action.getType()) {
        case COMPOSABLE:
            composebleActions.add(action);
            break;
        case OBSERVER:
            observableActions.add(action);
            break;
        case EXCLUSIVE:
            exclusiveActions.add(action);
            break;
        default:// www.  j  a  v a 2 s.c o  m
            return null;
        }
    }
    if (!exclusiveActions.isEmpty()) {
        if (exclusiveActions.size() == 1) {
            return Sets.union(exclusiveActions, observableActions);
        } else {
            return null;
        }
    }
    return Sets.union(composebleActions, observableActions);
}

From source file:prm4j.indexing.model.JoinArgs.java

protected static List<Set<Parameter<?>>> getDisableSets(ParametricPropertyModel ppm, BaseEvent baseEvent,
        final Set<Parameter<?>> enableSet) {
    final Set<Parameter<?>> combination = Sets.union(baseEvent.getParameters(), enableSet);
    return toListOfParameterSetsAscending(
            Sets.filter(toParameterSets(ppm.getParametricProperty().getSpec().getBaseEvents()),
                    new Predicate<Set<Parameter<?>>>() {
                        @Override
                        public boolean apply(Set<Parameter<?>> baseEventParameterSet) {
                            return combination.containsAll(baseEventParameterSet)
                                    && !enableSet.containsAll(baseEventParameterSet);
                        }/*from   w ww .  j av  a2s  . co m*/
                    }));
}

From source file:uk.ac.ed.inf.ace.FileEvaluator.java

@Override
public void output(File file) throws Exception {

    try (PrintStream printStream = new PrintStream(file)) {
        SetView<Object> allPredictedLabels = Sets.union(labels, confusionMatrix.getPredictedLabels());

        for (Object predictedLabel : allPredictedLabels) {
            printStream.print(",");
            printStream.print(predictedLabel);
        }// www  . j  av  a2 s .  c  o  m

        printStream.println();

        for (Object actualLabel : Sets.union(labels, confusionMatrix.getActualLabels())) {
            printStream.print(actualLabel);

            for (Object predictedLabel : allPredictedLabels) {
                Integer value = confusionMatrix.get(actualLabel, predictedLabel);

                if (value == null) {
                    value = 0;
                }

                printStream.print(",");
                printStream.print(value);
            }

            printStream.println();
        }
    }
}

From source file:org.sosy_lab.cpachecker.cfa.ast.c.FileLocationCollectingVisitor.java

@Override
public Set<FileLocation> visit(CExpressionAssignmentStatement pS) throws RuntimeException {
    return Sets.union(Collections.singleton(pS.getFileLocation()),
            Sets.union(pS.getLeftHandSide().accept(this), pS.getRightHandSide().accept(this)));
}

From source file:grakn.core.graql.reasoner.unifier.UnifierImpl.java

@Override
public Unifier merge(Unifier d) {
    return new UnifierImpl(Sets.union(this.mappings(), d.mappings()));
}

From source file:org.gradle.internal.component.model.ComponentAttributeMatcher.java

private void doMatch() {
    Set<Attribute<Object>> requestedAttributes = Cast.uncheckedCast(consumerAttributesContainer.keySet());
    for (Map.Entry<HasAttributes, MatchDetails> entry : matchDetails.entrySet()) {
        HasAttributes key = entry.getKey();
        MatchDetails details = entry.getValue();
        AttributeContainer producerAttributesContainer = key.getAttributes();
        Set<Attribute<Object>> dependencyAttributes = Cast.uncheckedCast(producerAttributesContainer.keySet());
        Set<Attribute<Object>> filter = attributesToConsider != null
                ? Cast.<Set<Attribute<Object>>>uncheckedCast(attributesToConsider.keySet())
                : null;//from   w  w w .  jav  a 2 s  .  c  om
        Set<Attribute<Object>> allAttributes = filter != null ? filter
                : Sets.union(requestedAttributes, dependencyAttributes);
        for (Attribute<Object> attribute : allAttributes) {
            AttributeValue<Object> consumerValue = attributeValue(attribute, consumerAttributeSchema,
                    consumerAttributesContainer);
            AttributeValue<Object> producerValue = attributeValue(attribute, producerAttributeSchema,
                    producerAttributesContainer);
            details.update(attribute, consumerAttributeSchema, producerAttributeSchema, consumerValue,
                    producerValue);
        }
    }
}