Example usage for com.google.common.collect Multimaps index

List of usage examples for com.google.common.collect Multimaps index

Introduction

In this page you can find the example usage for com.google.common.collect Multimaps index.

Prototype

public static <K, V> ImmutableListMultimap<K, V> index(Iterator<V> values, Function<? super V, K> keyFunction) 

Source Link

Document

Creates an index ImmutableListMultimap that contains the results of applying a specified function to each item in an Iterator of values.

Usage

From source file:com.linkedin.pinot.core.query.aggregation.function.quantile.digest.QuantileDigest.java

public String toGraphviz() {
    StringBuilder builder = new StringBuilder();

    builder.append("digraph QuantileDigest {\n").append("\tgraph [ordering=\"out\"];");

    final List<Node> nodes = new ArrayList<>();
    postOrderTraversal(root, new Callback() {
        @Override//from   w w w . ja  v a  2  s . co  m
        public boolean process(Node node) {
            nodes.add(node);
            return true;
        }
    });

    Multimap<Integer, Node> nodesByLevel = Multimaps.index(nodes, new Function<Node, Integer>() {
        @Override
        public Integer apply(Node input) {
            return input.level;
        }
    });

    for (Map.Entry<Integer, Collection<Node>> entry : nodesByLevel.asMap().entrySet()) {
        builder.append("\tsubgraph level_" + entry.getKey() + " {\n").append("\t\trank = same;\n");

        for (Node node : entry.getValue()) {
            builder.append(
                    String.format("\t\t%s [label=\"[%s..%s]@%s\\n%s\", shape=rect, style=filled,color=%s];\n",
                            idFor(node), node.getLowerBound(), node.getUpperBound(), node.level,
                            node.weightedCount, node.weightedCount > 0 ? "salmon2" : "white"));
        }

        builder.append("\t}\n");
    }

    for (Node node : nodes) {
        if (node.left != null) {
            builder.append(format("\t%s -> %s;\n", idFor(node), idFor(node.left)));
        }
        if (node.right != null) {
            builder.append(format("\t%s -> %s;\n", idFor(node), idFor(node.right)));
        }
    }

    builder.append("}\n");

    return builder.toString();
}

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  ww.j  a  v a2  s.  c o  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:org.apache.omid.transaction.TTable.java

static ImmutableList<Collection<Cell>> groupCellsByColumnFilteringShadowCells(List<Cell> rawCells) {

    Predicate<Cell> shadowCellFilter = new Predicate<Cell>() {

        @Override/*from   ww  w. ja  va 2 s . co m*/
        public boolean apply(Cell cell) {
            return cell != null && !CellUtils.isShadowCell(cell);
        }

    };

    Function<Cell, ColumnWrapper> cellToColumnWrapper = new Function<Cell, ColumnWrapper>() {

        @Override
        public ColumnWrapper apply(Cell cell) {
            return new ColumnWrapper(CellUtil.cloneFamily(cell), CellUtil.cloneQualifier(cell));
        }

    };

    return Multimaps.index(Iterables.filter(rawCells, shadowCellFilter), cellToColumnWrapper).asMap().values()
            .asList();
}

From source file:com.facebook.swift.codec.metadata.AbstractThriftMetadataBuilder.java

protected final Iterable<ThriftFieldMetadata> buildFieldInjections() {
    Multimap<Optional<Short>, FieldMetadata> fieldsById = Multimaps.index(fields, getThriftFieldId());
    return Iterables.transform(fieldsById.asMap().values(),
            new Function<Collection<FieldMetadata>, ThriftFieldMetadata>() {
                @Override/* ww w  .ja v a  2 s . com*/
                public ThriftFieldMetadata apply(Collection<FieldMetadata> input) {
                    checkArgument(!input.isEmpty(), "input is empty");
                    return buildField(input);
                }
            });
}

From source file:eu.itesla_project.online.StateAnalyzer.java

private void putResultsIntoContext(Integer stateId, ImpactAnalysisResult simulationResult,
        ForecastAnalysisResults results) {
    Objects.requireNonNull(stateId, "state id is null");
    Objects.requireNonNull(simulationResult, "simulation result is null");
    Objects.requireNonNull(results, "forecast analysis result is null");
    List<SecurityIndex> securityIndexesList = new ArrayList<SecurityIndex>();
    if (parameters.getSecurityIndexes() == null)
        securityIndexesList = simulationResult.getSecurityIndexes();
    else {//  ww  w  .j a  va  2s  .  com
        securityIndexesList = simulationResult.getSecurityIndexes().stream()
                .filter(x -> parameters.getSecurityIndexes().contains(x.getId().getSecurityIndexType()))
                .collect(Collectors.toList());
        if (securityIndexesList.isEmpty()) {
            logger.info("Empty filter security indexes -> using all the indexes");
            securityIndexesList = simulationResult.getSecurityIndexes();
        }
    }
    //Multimap<String, SecurityIndex> securityIndexes = Multimaps.index(simulationResult.getSecurityIndexes(), new Function<SecurityIndex, String>() {
    Multimap<String, SecurityIndex> securityIndexes = Multimaps.index(securityIndexesList,
            new Function<SecurityIndex, String>() {
                @Override
                public String apply(SecurityIndex index) {
                    return index.getId().getContingencyId();
                }
            });
    synchronized (results) {
        for (Map.Entry<String, Collection<SecurityIndex>> entry : securityIndexes.asMap().entrySet()) {
            boolean isSafe = OnlineUtils.isSafe(entry.getValue());
            if (!isSafe) {
                logger.info("{}: unsafe for contingency {} afer time domain simulation", stateId,
                        entry.getKey());
                results.addUnsafeStateWithIndexes(entry.getKey(), stateId, new ArrayList<>(entry.getValue()));
            } else {
                logger.info("{}: safe for contingency {} afer time domain simulation", stateId, entry.getKey());
                if (parameters.validation()) // if validation add anyway to results
                    results.addUnsafeStateWithIndexes(entry.getKey(), stateId,
                            new ArrayList<>(entry.getValue()));
            }
        }
    }
}

From source file:com.b2international.snowowl.snomed.reasoner.server.normalform.RelationshipNormalFormGenerator.java

private Iterable<Group> toGroups(final boolean preserveNumbers,
        final Collection<StatementFragment> nonIsARelationshipFragments) {

    final Map<Integer, Collection<StatementFragment>> relationshipsByGroupId = Multimaps
            .index(nonIsARelationshipFragments, new Function<StatementFragment, Integer>() {
                @Override/* w w  w .j  a v  a  2s . co  m*/
                public Integer apply(final StatementFragment input) {
                    return input.getGroup();
                }
            }).asMap();

    final Collection<Collection<Group>> groups = Maps.transformEntries(relationshipsByGroupId,
            new EntryTransformer<Integer, Collection<StatementFragment>, Collection<Group>>() {
                @Override
                public Collection<Group> transformEntry(final Integer key,
                        final Collection<StatementFragment> values) {
                    final Iterable<UnionGroup> unionGroups = toUnionGroups(preserveNumbers, values);
                    final Iterable<UnionGroup> disjointUnionGroups = getDisjointComparables(unionGroups);

                    if (key == 0) {
                        // Relationships in group 0 form separate groups
                        return ImmutableList.copyOf(toZeroGroups(preserveNumbers, disjointUnionGroups));
                    } else {
                        // Other group numbers produce a single group from all fragments
                        return ImmutableList.of(toNonZeroGroup(preserveNumbers, key, disjointUnionGroups));
                    }
                }
            }).values();

    return Iterables.concat(groups);
}

From source file:com.b2international.snowowl.snomed.exporter.server.dsv.SnomedSimpleTypeRefSetDSVExporter.java

private SnomedRelationships mapToSourceConcepts(SnomedRelationships relationships, SnomedConcepts concepts) {
    ImmutableListMultimap<String, SnomedRelationship> relationshipsBySourceId = Multimaps
            .index(relationships.getItems(), (relationship) -> relationship.getSourceId());
    concepts.forEach(concept -> {//w ww.j  a v  a 2 s .co  m
        List<SnomedRelationship> collection = relationshipsBySourceId.get(concept.getId());
        concept.setRelationships(
                new SnomedRelationships(collection, null, null, collection.size(), collection.size()));
    });
    return relationships;
}

From source file:com.b2international.snowowl.snomed.exporter.server.dsv.SnomedSimpleTypeRefSetDSVExporter.java

private SnomedDescriptions mapToConcepts(SnomedConcepts concepts, SnomedDescriptions resultDescriptions) {
    ImmutableListMultimap<String, SnomedDescription> descriptionsByOwnerConceptId = Multimaps
            .index(resultDescriptions.getItems(), description -> description.getConceptId());

    concepts.forEach(concept -> {//from  w w  w. j a va 2 s . c o  m
        List<SnomedDescription> descriptions = descriptionsByOwnerConceptId.get(concept.getId());
        concept.setDescriptions(
                new SnomedDescriptions(descriptions, null, null, descriptions.size(), descriptions.size()));
    });

    return resultDescriptions;
}

From source file:com.b2international.snowowl.snomed.reasoner.server.normalform.RelationshipNormalFormGenerator.java

private Iterable<UnionGroup> toUnionGroups(final boolean preserveNumbers,
        final Collection<StatementFragment> values) {
    final Map<Integer, Collection<StatementFragment>> relationshipsByUnionGroupId = Multimaps
            .index(values, new Function<StatementFragment, Integer>() {
                @Override//  w  w  w .  java  2s  .c o  m
                public Integer apply(final StatementFragment input) {
                    return input.getUnionGroup();
                }
            }).asMap();

    final Collection<Collection<UnionGroup>> unionGroups = Maps.transformEntries(relationshipsByUnionGroupId,
            new EntryTransformer<Integer, Collection<StatementFragment>, Collection<UnionGroup>>() {
                @Override
                public Collection<UnionGroup> transformEntry(final Integer key,
                        final Collection<StatementFragment> values) {
                    if (key == 0) {
                        // Relationships in union group 0 form separate union groups
                        return ImmutableList.copyOf(toZeroUnionGroups(values));
                    } else {
                        // Other group numbers produce a single union group from all fragments
                        return ImmutableList.of(toNonZeroUnionGroup(preserveNumbers, key, values));
                    }
                }
            }).values();

    return Iterables.concat(unionGroups);
}

From source file:org.apache.hadoop.hive.metastore.utils.MetaStoreServerUtils.java

/**
 * Coalesce list of partitions belonging to a table into a more compact PartitionSpec
 * representation.//from w  ww.ja va 2 s.c om
 *
 * @param table Table thrift object
 * @param partitions List of partition objects
 * @return collection PartitionSpec objects which is a compressed representation of original
 * partition list.
 */
public static List<PartitionSpec> getPartitionspecsGroupedByStorageDescriptor(Table table,
        Collection<Partition> partitions) {
    final String tablePath = table.getSd().getLocation();

    ImmutableListMultimap<StorageDescriptorKey, Partition> partitionsWithinTableDirectory = Multimaps
            .index(partitions, input -> {
                // if sd is not in the list of projected fields, all the partitions
                // can be just grouped in PartitionSpec object
                if (input.getSd() == null) {
                    return StorageDescriptorKey.UNSET_KEY;
                }
                // if the partition is within table, use the tableSDKey to group it with other partitions
                // within the table directory
                if (input.getSd().getLocation() != null && input.getSd().getLocation().startsWith(tablePath)) {
                    return new StorageDescriptorKey(tablePath, input.getSd());
                }
                // if partitions are located outside table location we treat them as non-standard
                // and do not perform any grouping
                // if the location is not set partitions are grouped according to the rest of the SD fields
                return new StorageDescriptorKey(input.getSd());
            });

    List<PartitionSpec> partSpecs = new ArrayList<>();

    // Classify partitions based on shared SD properties.
    Map<StorageDescriptorKey, List<PartitionWithoutSD>> sdToPartList = new HashMap<>();
    // we don't expect partitions to exist outside directory in most cases
    List<Partition> partitionsOutsideTableDir = new ArrayList<>(0);
    for (StorageDescriptorKey key : partitionsWithinTableDirectory.keySet()) {
        boolean isUnsetKey = key.equals(StorageDescriptorKey.UNSET_KEY);
        // group the partitions together when
        // case I : sd is not set because it was not in the requested fields
        // case II : when sd.location is not set because it was not in the requested fields
        // case III : when sd.location is set and it is located within table directory
        if (isUnsetKey || key.baseLocation == null || key.baseLocation.equals(tablePath)) {
            for (Partition partition : partitionsWithinTableDirectory.get(key)) {

                PartitionWithoutSD partitionWithoutSD = new PartitionWithoutSD();
                partitionWithoutSD.setValues(partition.getValues());
                partitionWithoutSD.setCreateTime(partition.getCreateTime());
                partitionWithoutSD.setLastAccessTime(partition.getLastAccessTime());
                partitionWithoutSD.setRelativePath((isUnsetKey || !partition.getSd().isSetLocation()) ? null
                        : partition.getSd().getLocation().substring(tablePath.length()));
                partitionWithoutSD.setParameters(partition.getParameters());

                if (!sdToPartList.containsKey(key)) {
                    sdToPartList.put(key, new ArrayList<>());
                }
                sdToPartList.get(key).add(partitionWithoutSD);
            }
        } else {
            // Lump all partitions outside the tablePath into one PartSpec.
            // if non-standard partitions need not be deDuped create PartitionListComposingSpec
            // this will be used mostly for keeping backwards compatibility with  some HMS APIs which use
            // PartitionListComposingSpec for non-standard partitions located outside table
            partitionsOutsideTableDir.addAll(partitionsWithinTableDirectory.get(key));
        }
    }
    // create sharedSDPartSpec for all the groupings
    for (Map.Entry<StorageDescriptorKey, List<PartitionWithoutSD>> entry : sdToPartList.entrySet()) {
        partSpecs.add(getSharedSDPartSpec(table, entry.getKey(), entry.getValue()));
    }
    if (!partitionsOutsideTableDir.isEmpty()) {
        PartitionSpec partListSpec = new PartitionSpec();
        partListSpec.setDbName(table.getDbName());
        partListSpec.setTableName(table.getTableName());
        partListSpec.setPartitionList(new PartitionListComposingSpec(partitionsOutsideTableDir));
        partSpecs.add(partListSpec);
    }
    return partSpecs;
}