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.android.tools.idea.gradle.structure.configurables.android.dependencies.module.treeview.DependencyNodes.java

private static void addTransitive(@NotNull PsDependency dependency,
        @NotNull Multimap<PsDependency, PsDependency> allTransitive) {
    if (allTransitive.containsKey(dependency)) {
        return;//  w  ww.j  av  a2  s . c  o  m
    }

    if (dependency instanceof PsLibraryAndroidDependency) {
        PsLibraryAndroidDependency libraryDependency = (PsLibraryAndroidDependency) dependency;
        ImmutableCollection<PsDependency> transitives = libraryDependency.getTransitiveDependencies();
        allTransitive.putAll(dependency, transitives);

        for (PsDependency transitive : transitives) {
            addTransitive(transitive, allTransitive);
        }
    }
}

From source file:uk.ac.ebi.intact.dataexchange.cvutils.CvUtils.java

/**
 * Returns the id of the parent which is common to the children provided. If none is found, it returns null.
 *
 * @param children A CvTerms with a common parent
 * @return the common parent for the children
 *///from w  w  w .  j a v a 2  s.  c o m
public static CvDagObject findLowestCommonAncestor(CvDagObject... children) {
    if (children.length < 2) {
        throw new IllegalArgumentException("At least two children have to be provided to find a common parent");
    }

    Multimap<String, String> cvMap = LinkedHashMultimap.create();

    // get all the parents for each child
    for (CvDagObject child : children) {
        cvMap.put(child.getIdentifier(), child.getIdentifier());
        cvMap.putAll(child.getIdentifier(), findAllParentsForTerm(child));
    }

    // calculate the common parents by interesecting all the collections of parents for each child
    List<String> commonParents = null;

    for (Collection<String> parents : cvMap.asMap().values()) {
        List<String> parentsList = new LinkedList<String>(parents);
        if (commonParents == null) {
            commonParents = parentsList;
        } else {
            commonParents = ListUtils.intersection(commonParents, parentsList);
        }

    }

    // the first child of the commonParents collection is the lowest common ancestor
    if (!commonParents.isEmpty()) {
        String parentId = commonParents.iterator().next();
        return findParentById(children[0], parentId);
    }

    return null;
}

From source file:com.redprairie.moca.components.mocatest.ClusterTestComponent.java

/**
 * Gets a result set of node|roles which is each
 * node URL to a comma separated list of roles
 * @return//  w w w. j  a  v  a 2 s .  co  m
 */
public static MocaResults getNodesToRoles() {
    SimpleResults res = new SimpleResults();
    res.addColumn("node", MocaType.STRING);
    res.addColumn("roles", MocaType.STRING);

    ClusterRoleManager manager = ServerUtils.globalAttribute(ClusterRoleManager.class);
    MocaClusterAdministration admin = (MocaClusterAdministration) ServerUtils.globalContext()
            .getAttribute(MocaClusterAdministration.class.getName());
    if (manager != null && admin != null) {
        Multimap<Node, RoleDefinition> multiMap = manager.getClusterRoles();
        Map<Node, InstanceUrl> urls = admin.getKnownNodes();
        _logger.info("Cluster URLs: " + urls.entrySet());
        _logger.info("Node to Roles: " + multiMap.asMap().entrySet());

        Multimap<InstanceUrl, RoleDefinition> urlRoleMap = HashMultimap.create();

        for (Entry<Node, Collection<RoleDefinition>> entry : multiMap.asMap().entrySet()) {
            InstanceUrl url = urls.get(entry.getKey());
            // TODO: trying to handle that cluster URLs is wrong and doesn't contain the entry
            if (url == null) {
                url = new InstanceUrl(false, entry.getKey().toString(), 0);
            }
            urlRoleMap.putAll(url, entry.getValue());
        }

        for (Entry<Node, InstanceUrl> url : urls.entrySet()) {
            if (!urlRoleMap.containsKey(url.getValue())) {
                urlRoleMap.put(url.getValue(), EMPTY_ROLE);
            }
        }

        // Sort by the node url
        Map<InstanceUrl, Collection<RoleDefinition>> sortedMap = new TreeMap<InstanceUrl, Collection<RoleDefinition>>(
                new Comparator<InstanceUrl>() {

                    @Override
                    public int compare(InstanceUrl o1, InstanceUrl o2) {
                        return o1.toString().compareTo(o2.toString());
                    }
                });

        sortedMap.putAll(urlRoleMap.asMap());
        for (Map.Entry<InstanceUrl, Collection<RoleDefinition>> entry : sortedMap.entrySet()) {
            res.addRow();
            res.setStringValue("node", entry.getKey().toString());
            res.setStringValue("roles", Joiner.on(',').join(entry.getValue()));
        }
    } else {
        _logger.info("Cluster manager and/or admin not set");
    }

    return res;
}

From source file:com.googlecode.blaisemath.graph.GraphUtils.java

/**
 * Generates adjacency map from a subgraph.
 * @param <V> graph node type/*from   ww w . j av a  2s  .co m*/
 * @param graph the graph
 * @param nodes subset of nodes
 * @return adjacency map restricted to the given subset
 */
public static <V> Multimap<V, V> adjacencies(Graph<V> graph, Set<V> nodes) {
    Multimap<V, V> res = LinkedHashMultimap.create();
    for (V v : nodes) {
        res.putAll(v, Sets.intersection(graph.neighbors(v), nodes));
    }
    return res;
}

From source file:pt.org.aguiaj.core.AguiaJActivator.java

private static Multimap<String, Class<?>> readClasses(List<IPath> workingDirs) {
    Multimap<String, Class<?>> ret = ArrayListMultimap.create();
    for (IPath path : workingDirs) {
        Map<String, Set<Class<?>>> classes = ReflectionUtils.readClassFiles(path);

        if (ret.isEmpty()) {
            for (String key : classes.keySet()) {
                ret.putAll(key, classes.get(key));
            }/*from   w w  w .ja  v  a2  s . c  o  m*/
        }
    }
    return ret;
}

From source file:org.crypto.sse.Partition.java

public static Multimap<Integer, String> partitioning(Multimap<String, String> lookup) {

    // Partitions Creation
    Set<String> keys = lookup.keySet();

    int partitionId = 0;
    Multimap<Integer, String> partitions = ArrayListMultimap.create();
    int counter2 = 0;

    for (String key : keys) {
        Set<Integer> keys2 = partitions.keySet();
        List<String> inter = (List<String>) lookup.get(key);
        List<String> interTMP = new ArrayList<String>(inter);

        Printer.debugln("Step number: " + counter2++ + "Number of keywords " + keys.size());

        Set<String> set = new HashSet<String>(interTMP);
        Multimap<Integer, String> partitionsTMP = ArrayListMultimap.create();

        for (Integer key2 : keys2) {

            if (!set.isEmpty()) {
                Set<String> tmp = new HashSet<String>(partitions.get(key2));

                Set<String> intersection = Sets.intersection(tmp, set);

                Set<String> difference;

                if (intersection.isEmpty()) {
                    difference = tmp;//w  w  w.  j  a  v a  2s.c om
                } else {
                    difference = Sets.difference(tmp, intersection);
                    set = Sets.difference(set, intersection);

                }

                if (!difference.isEmpty()) {
                    partitionId = partitionId + 1;
                    partitionsTMP.putAll(partitionId, difference);
                }

                if (!intersection.isEmpty()) {
                    partitionId = partitionId + 1;
                    partitionsTMP.putAll(partitionId, intersection);
                }

            } else {
                partitionId = partitionId + 1;
                partitionsTMP.putAll(partitionId, new HashSet<String>(partitions.get(key2)));
            }

        }

        interTMP = new ArrayList<String>(set);

        if (!interTMP.isEmpty()) {

            partitionId = partitionId + 1;
            partitionsTMP.putAll(partitionId, interTMP);

        }

        partitions = ArrayListMultimap.create(partitionsTMP);
        partitionsTMP.clear();
        interTMP.clear();

    }

    Printer.debugln("Partitions size " + partitions.keySet().size());
    Printer.debugln("\n");

    return partitions;
}

From source file:com.sam.moca.cluster.manager.AbstractClusterRoleManager.java

protected static <K, V> Multimap<K, V> convertMapToMultimap(Map<K, ? extends Iterable<V>> map) {
    Multimap<K, V> multimap = HashMultimap.create();
    for (Entry<K, ? extends Iterable<V>> entry : map.entrySet()) {
        multimap.putAll(entry.getKey(), entry.getValue());
    }/*  w  w w.  j  av  a 2 s .c  o m*/
    return multimap;
}

From source file:org.deephacks.confit.model.Bean.java

public static Bean read(BeanId beanId, byte[] data) {
    if (ids == null) {
        ids = UniqueIds.lookup();/*from   www  .ja  v a 2  s.co m*/
    }
    Preconditions.checkNotNull(beanId);
    Preconditions.checkNotNull(data);
    Schema schema = beanId.getSchema();
    Preconditions.checkNotNull(schema);

    Bean bean = Bean.create(beanId);
    ValueReader reader = new ValueReader(data);

    Multimap<String, String> properties = ArrayListMultimap.create();
    Multimap<String, BeanId> references = ArrayListMultimap.create();

    int[] propertyIds = reader.getIds();
    for (int id : propertyIds) {
        String propertyName = ids.getSchemaName(id);
        Object value = reader.getValue(id);
        if (schema.isProperty(propertyName)) {
            if (Collection.class.isAssignableFrom(value.getClass())) {
                properties.putAll(propertyName, conversion.convert((Collection) value, String.class));
            } else {
                properties.put(propertyName, conversion.convert(value, String.class));
            }
        } else if (schema.isReference(propertyName)) {
            String schemaName = schema.getReferenceSchemaName(propertyName);
            if (Collection.class.isAssignableFrom(value.getClass())) {
                for (String instanceId : (Collection<String>) value) {
                    references.put(propertyName, BeanId.create(instanceId, schemaName));
                }
            } else {
                String instanceId = (String) value;
                references.put(propertyName, BeanId.create(instanceId, schemaName));
            }
        } else {
            throw new IllegalArgumentException("Unrecognized property " + propertyName);
        }
    }
    for (String propertyName : properties.keySet()) {
        bean.addProperty(propertyName, properties.get(propertyName));
    }
    for (String propertyName : references.keySet()) {
        bean.addReference(propertyName, references.get(propertyName));
    }
    bean.set(schema);
    return bean;
}

From source file:com.b2international.snowowl.snomed.core.ecl.SnomedEclRefinementEvaluator.java

static Function<Collection<Property>, Collection<Property>> filterByCardinality(
        final boolean grouped, final Range<Long> groupCardinality, final Range<Long> cardinality,
        final Function<Property, Object> idProvider) {
    return matchingProperties -> {
        final Multimap<Object, Property> propertiesByMatchingIds = Multimaps.index(matchingProperties,
                idProvider);/*from   w w w.  j  a va 2  s  . co  m*/
        final Collection<Property> properties = newHashSet();

        final Range<Long> allowedRelationshipCardinality;
        if (grouped) {
            final long minRelationships = groupCardinality.lowerEndpoint() == 0 ? cardinality.lowerEndpoint()
                    : groupCardinality.lowerEndpoint() * cardinality.lowerEndpoint();
            final long maxRelationships;
            if (groupCardinality.hasUpperBound() && cardinality.hasUpperBound()) {
                if (groupCardinality.upperEndpoint() == Long.MAX_VALUE
                        || cardinality.upperEndpoint() == Long.MAX_VALUE) {
                    maxRelationships = Long.MAX_VALUE;
                } else {
                    maxRelationships = groupCardinality.upperEndpoint() * cardinality.upperEndpoint();
                }
            } else {
                // group and relationship cardinalities are unbounded
                maxRelationships = Long.MAX_VALUE;
            }
            allowedRelationshipCardinality = Range.closed(minRelationships, maxRelationships);
        } else {
            allowedRelationshipCardinality = cardinality;
        }

        for (Object matchingConceptId : propertiesByMatchingIds.keySet()) {
            final Collection<Property> propertiesOfConcept = propertiesByMatchingIds.get(matchingConceptId);
            if (allowedRelationshipCardinality.contains((long) propertiesOfConcept.size())) {
                if (grouped) {
                    final Multimap<Integer, Property> indexedByGroup = FluentIterable.from(propertiesOfConcept)
                            .index(Property::getGroup);
                    // if groups should be considered as well, then check group numbers in the matching sets
                    // check that the concept has at least the right amount of groups
                    final Multimap<Integer, Property> validGroups = ArrayListMultimap.create();

                    for (Integer group : indexedByGroup.keySet()) {
                        final Collection<Property> groupedRelationships = indexedByGroup.get(group);
                        if (cardinality.contains((long) groupedRelationships.size())) {
                            validGroups.putAll(group, groupedRelationships);
                        }
                    }

                    if (groupCardinality.contains((long) validGroups.keySet().size())) {
                        properties.addAll(validGroups.values());
                    }
                } else {
                    properties.addAll(propertiesOfConcept);
                }
            }
        }
        return properties;
    };
}

From source file:edu.umn.msi.tropix.client.directory.impl.LocalPersonSupplierImpl.java

public Multimap<String, Person> get() {
    Iterable<Person> persons = Lists.transform(Arrays.asList(userService.getUsers()), USER_TO_PERSON_FUNCTION);
    final Multimap<String, Person> personMap = HashMultimap.create();
    personMap.putAll(institution, persons);
    return personMap;
}