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

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

Introduction

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

Prototype

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

Source Link

Document

Returns a new builder.

Usage

From source file:com.facebook.buck.util.MoreCollectors.java

/**
 * Returns a {@code Collector} that builds an {@code ImmutableListMultimap}, whose keys and values
 * are the result of applying mapping functions to the input elements.
 *
 * This {@code Collector} behaves similar to
 * {@code Collectors.collectingAndThen(Collectors.toMap(), ImmutableListMultimap::copyOf)} but
 * preserves iteration order and does not build an intermediate map.
 *
 * @param <T> the type of the input elements
 * @param <K> the output type of the key mapping function
 * @param <U> the output type of the value mapping function
 *
 * @param keyMapper a mapping function to produce keys
 * @param valueMapper a mapping function to produce values
 *
 * @return a {@code Collector} that builds an {@code ImmutableListMultimap}.
 *///from   ww w. j  a  v a2 s.  c om
public static <T, K, U> Collector<T, ?, ImmutableListMultimap<K, U>> toImmutableListMultimap(
        Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) {
    return Collector.<T, ImmutableListMultimap.Builder<K, U>, ImmutableListMultimap<K, U>>of(
            ImmutableListMultimap::builder,
            (builder, elem) -> builder.put(keyMapper.apply(elem), valueMapper.apply(elem)),
            (left, right) -> left.putAll(right.build()), ImmutableListMultimap.Builder::build);
}

From source file:org.ambraproject.rhino.rest.controller.RestController.java

protected static ImmutableListMultimap<String, String> getParameters(ServletRequest request) {
    Map<String, String[]> parameterMap = request.getParameterMap();
    if (parameterMap.isEmpty()) {
        return ImmutableListMultimap.of(); // avoid constructing a builder
    }//  w w  w  .  j  av  a 2 s  .c om
    ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder();
    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
        String key = entry.getKey();
        String[] values = entry.getValue();
        for (int i = 0; i < values.length; i++) {
            builder.put(key, values[i]);
        }
    }
    return builder.build();
}

From source file:com.facebook.presto.accumulo.index.IndexLookup.java

private static Multimap<AccumuloColumnConstraint, Range> getIndexedConstraintRanges(
        Collection<AccumuloColumnConstraint> constraints, AccumuloRowSerializer serializer)
        throws AccumuloSecurityException, AccumuloException {
    ImmutableListMultimap.Builder<AccumuloColumnConstraint, Range> builder = ImmutableListMultimap.builder();
    for (AccumuloColumnConstraint columnConstraint : constraints) {
        if (columnConstraint.isIndexed()) {
            for (Range range : getRangesFromDomain(columnConstraint.getDomain(), serializer)) {
                builder.put(columnConstraint, range);
            }//from  w  ww . java 2  s  .c o m
        } else {
            LOG.warn("Query containts constraint on non-indexed column %s. Is it worth indexing?",
                    columnConstraint.getName());
        }
    }
    return builder.build();
}

From source file:com.isotrol.impe3.nr.api.Node.java

public Multimap<String, String> getProperties() {
    if (properties == null) {
        if (propertiesMap != null) {
            ImmutableListMultimap.Builder<String, String> b = ImmutableListMultimap.builder();
            for (Entry<String, Collection<String>> entry : propertiesMap.entrySet()) {
                final Collection<String> values = entry.getValue();
                if (values != null) {
                    b.putAll(entry.getKey(), values);
                }/*from   w  ww  .j ava 2s.  co m*/
            }
            properties = b.build();
        } else {
            properties = ImmutableListMultimap.of();
        }
    }
    return properties;
}

From source file:com.google.api.server.spi.MethodHierarchyReader.java

/**
 * Returns a mapping of public service methods defined by the class and its super classes,
 * keyed by method name.  Bridge methods are ignored.  All valid method implementations are
 * included, ordered subclass to superclass.
 *///from w  ww.  jav a  2s.  c  o  m
public ListMultimap<String, EndpointMethod> getNameToEndpointOverridesMap() {
    readMethodHierarchyIfNecessary();
    ImmutableListMultimap.Builder<String, EndpointMethod> builder = ImmutableListMultimap.builder();
    for (List<EndpointMethod> overrides : asMap(endpointMethods).values()) {
        builder.putAll(getLeafMethod(overrides).getMethod().getName(), overrides);
    }
    return builder.build();
}

From source file:com.facebook.buck.maven.Publisher.java

/**
 * Checks for any packaged dependencies that exist between more than one of the targets that we
 * are trying to publish./*from  w w w.j ava  2 s . c o  m*/
 * @return A multimap of dependency build targets and the publishable build targets that have them
 * included in the final package that will be uploaded.
 */
private ImmutableListMultimap<UnflavoredBuildTarget, UnflavoredBuildTarget> checkForDuplicatePackagedDeps(
        ImmutableSet<MavenPublishable> publishables) {
    // First build the multimap of the builtin dependencies and the publishable targets that use
    // them.
    Multimap<UnflavoredBuildTarget, UnflavoredBuildTarget> builtinDeps = HashMultimap.create();
    for (MavenPublishable publishable : publishables) {
        for (BuildRule buildRule : publishable.getPackagedDependencies()) {
            builtinDeps.put(buildRule.getBuildTarget().getUnflavoredBuildTarget(),
                    publishable.getBuildTarget().getUnflavoredBuildTarget());
        }
    }
    // Now, check for any duplicate uses, and if found, return them.
    ImmutableListMultimap.Builder<UnflavoredBuildTarget, UnflavoredBuildTarget> builder = ImmutableListMultimap
            .builder();
    for (UnflavoredBuildTarget buildTarget : builtinDeps.keySet()) {
        Collection<UnflavoredBuildTarget> publishablesUsingBuildTarget = builtinDeps.get(buildTarget);
        if (publishablesUsingBuildTarget.size() > 1) {
            builder.putAll(buildTarget, publishablesUsingBuildTarget);
        }
    }
    return builder.build();
}

From source file:com.google.template.soy.soytree.TemplateRegistry.java

/**
 * Constructor./*from  w  w  w. j av a 2s  .  c o m*/
 * @param soyTree The Soy tree from which to build a template registry.
 */
public TemplateRegistry(SoyFileSetNode soyTree, ErrorReporter errorReporter) {

    // ------ Iterate through all templates to collect data. ------
    ImmutableList.Builder<TemplateNode> allTemplatesBuilder = ImmutableList.builder();
    Map<String, TemplateBasicNode> basicTemplates = new LinkedHashMap<>();
    ImmutableSetMultimap.Builder<String, DelTemplateKey> delTemplateBuilder = ImmutableSetMultimap.builder();
    Map<DelTemplateKey, Map<Priority, Map<String, TemplateDelegateNode>>> tempDelTemplatesMap = new LinkedHashMap<>();

    for (SoyFileNode soyFile : soyTree.getChildren()) {
        for (TemplateNode template : soyFile.getChildren()) {
            allTemplatesBuilder.add(template);
            if (template instanceof TemplateBasicNode) {
                // Case 1: Basic template.
                TemplateBasicNode prev = basicTemplates.put(template.getTemplateName(),
                        (TemplateBasicNode) template);
                if (prev != null) {
                    errorReporter.report(template.getSourceLocation(), DUPLICATE_BASIC_TEMPLATES,
                            template.getTemplateName(), prev.getSourceLocation());
                }
            } else {
                // Case 2: Delegate template.
                TemplateDelegateNode delTemplate = (TemplateDelegateNode) template;
                DelTemplateKey delTemplateKey = delTemplate.getDelTemplateKey();

                // Add to tempDelTemplateNameToKeysMap.
                String delTemplateName = delTemplate.getDelTemplateName();
                delTemplateBuilder.put(delTemplateName, delTemplateKey);

                // Add to tempDelTemplatesMap.
                Priority delPriority = delTemplate.getDelPriority();
                String delPackageName = delTemplate.getDelPackageName();

                Map<Priority, Map<String, TemplateDelegateNode>> tempDivisions = tempDelTemplatesMap
                        .get(delTemplateKey);
                if (tempDivisions == null) {
                    tempDivisions = new EnumMap<>(Priority.class);
                    tempDelTemplatesMap.put(delTemplateKey, tempDivisions);
                }

                Map<String, TemplateDelegateNode> tempDivision = tempDivisions.get(delPriority);
                if (tempDivision == null) {
                    tempDivision = new LinkedHashMap<>();
                    tempDivisions.put(delPriority, tempDivision);
                }

                if (tempDivision.containsKey(delPackageName)) {
                    TemplateDelegateNode prevTemplate = tempDivision.get(delPackageName);
                    if (delPackageName == null) {
                        errorReporter.report(delTemplate.getSourceLocation(),
                                DUPLICATE_DEFAULT_DELEGATE_TEMPLATES, delTemplateName,
                                prevTemplate.getSourceLocation());
                    } else {
                        errorReporter.report(delTemplate.getSourceLocation(),
                                DUPLICATE_DELEGATE_TEMPLATES_IN_DELPACKAGE, delTemplateName, delPackageName,
                                prevTemplate.getSourceLocation());
                    }
                }
                tempDivision.put(delPackageName, delTemplate);
            }
        }
    }

    // ------ Build the final data structures. ------

    basicTemplatesMap = ImmutableMap.copyOf(basicTemplates);

    ImmutableListMultimap.Builder<DelTemplateKey, DelegateTemplateDivision> delTemplatesMapBuilder = ImmutableListMultimap
            .builder();

    for (DelTemplateKey delTemplateKey : tempDelTemplatesMap.keySet()) {
        Map<Priority, Map<String, TemplateDelegateNode>> tempDivisions = tempDelTemplatesMap
                .get(delTemplateKey);

        ImmutableList.Builder<DelegateTemplateDivision> divisionsBuilder = ImmutableList.builder();

        // Note: List should be in decreasing priority order.
        Map<String, TemplateDelegateNode> highPriorityTemplates = tempDivisions.get(Priority.HIGH_PRIORITY);
        if (highPriorityTemplates != null) {
            divisionsBuilder.add(new DelegateTemplateDivision(highPriorityTemplates));
        }
        Map<String, TemplateDelegateNode> standardPriorityTemplates = tempDivisions.get(Priority.STANDARD);
        if (standardPriorityTemplates != null) {
            divisionsBuilder.add(new DelegateTemplateDivision(standardPriorityTemplates));
        }

        delTemplatesMapBuilder.putAll(delTemplateKey, divisionsBuilder.build());
    }

    delTemplatesMap = delTemplatesMapBuilder.build();
    delTemplateNameToKeysMap = delTemplateBuilder.build();
    allTemplates = allTemplatesBuilder.build();
}

From source file:io.prestosql.plugin.accumulo.index.IndexLookup.java

private static Multimap<AccumuloColumnConstraint, Range> getIndexedConstraintRanges(
        Collection<AccumuloColumnConstraint> constraints, AccumuloRowSerializer serializer) {
    ImmutableListMultimap.Builder<AccumuloColumnConstraint, Range> builder = ImmutableListMultimap.builder();
    for (AccumuloColumnConstraint columnConstraint : constraints) {
        if (columnConstraint.isIndexed()) {
            for (Range range : getRangesFromDomain(columnConstraint.getDomain(), serializer)) {
                builder.put(columnConstraint, range);
            }//w  w  w. ja  v a2  s  . c  o m
        } else {
            LOG.warn("Query contains constraint on non-indexed column %s. Is it worth indexing?",
                    columnConstraint.getName());
        }
    }
    return builder.build();
}

From source file:de.metas.ui.web.picking.pickingslot.PickingHURowsRepository.java

private ListMultimap<PickingSlotId, PickedHUEditorRow> retrievePickedHUsIndexedByPickingSlotId(
        @NonNull final List<PickingCandidate> pickingCandidates) {
    final Map<HuId, PickedHUEditorRow> huId2huRow = new HashMap<>();

    final ImmutableListMultimap.Builder<PickingSlotId, PickedHUEditorRow> builder = ImmutableListMultimap
            .builder();/* www.ja  v a  2 s  .c o m*/
    for (final PickingCandidate pickingCandidate : pickingCandidates) {
        if (pickingCandidate.isRejectedToPick()) {
            continue;
        }

        final HuId huId = pickingCandidate.getPickFromHuId();
        if (huId == null) {
            logger.warn("Skip {} because huId is null", huId);
            continue;
        }
        if (huId2huRow.containsKey(huId)) {
            continue;
        }

        final PickingSlotId pickingSlotId = pickingCandidate.getPickingSlotId();
        if (pickingSlotId == null) {
            logger.warn("Skip picking candidate because it has no picking slot set: {}."
                    + "\n Usually that happening because it was picked with some other picking terminal.",
                    pickingCandidate);
            continue;
        }

        final HUEditorRow huEditorRow = huEditorRepo.retrieveForHUId(huId);
        final boolean pickingCandidateProcessed = isPickingCandidateProcessed(pickingCandidate);
        final PickedHUEditorRow row = new PickedHUEditorRow(huEditorRow, pickingCandidateProcessed);

        huId2huRow.put(huId, row);
        builder.put(pickingSlotId, row);
    }

    return builder.build();
}

From source file:com.google.api.server.spi.MethodHierarchyReader.java

/**
 * Recursively builds a map from method_names to methods.  Method types will be resolved as much
 * as possible using {@code serviceType}.
 *
 * @param serviceType is the class object being inspected for service methods
 *///from   www  . j  a  va  2 s  .  c  o  m
private void buildServiceMethods(
        ImmutableListMultimap.Builder<EndpointMethod.ResolvedSignature, EndpointMethod> builder,
        TypeToken<?> serviceType) {
    for (TypeToken<?> typeToken : serviceType.getTypes().classes()) {
        Class<?> serviceClass = typeToken.getRawType();
        if (Object.class.equals(serviceClass)) {
            return;
        }
        for (Method method : serviceClass.getDeclaredMethods()) {
            if (!isServiceMethod(method)) {
                continue;
            }

            EndpointMethod currentMethod = EndpointMethod.create(endpointClass, method, typeToken);
            builder.put(currentMethod.getResolvedMethodSignature(), currentMethod);
        }
    }
}