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

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

Introduction

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

Prototype

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

Source Link

Document

Returns a new builder.

Usage

From source file:com.facebook.presto.split.NativeSplitManager.java

@Override
public Iterable<Split> getPartitionSplits(TableHandle tableHandle, List<Partition> partitions) {
    Stopwatch splitTimer = new Stopwatch();
    splitTimer.start();//from   w ww  .j av a 2s  .co m

    checkNotNull(partitions, "partitions is null");
    if (partitions.isEmpty()) {
        return ImmutableList.of();
    }

    Map<String, Node> nodesById = uniqueIndex(nodeManager.getAllNodes().getActiveNodes(),
            Node.getIdentifierFunction());

    List<Split> splits = new ArrayList<>();

    Multimap<Long, Entry<Long, String>> partitionShardNodes = shardManager
            .getCommittedPartitionShardNodes(tableHandle);

    for (Partition partition : partitions) {
        checkArgument(partition instanceof NativePartition, "Partition must be a native partition");
        NativePartition nativePartition = (NativePartition) partition;

        ImmutableMultimap.Builder<Long, String> shardNodes = ImmutableMultimap.builder();
        for (Entry<Long, String> partitionShardNode : partitionShardNodes
                .get(nativePartition.getNativePartitionId())) {
            shardNodes.put(partitionShardNode.getKey(), partitionShardNode.getValue());
        }

        for (Map.Entry<Long, Collection<String>> entry : shardNodes.build().asMap().entrySet()) {
            List<HostAddress> addresses = getAddressesForNodes(nodesById, entry.getValue());
            checkState(addresses.size() > 0, "no host for shard %s found", entry.getKey());
            Split split = new NativeSplit(entry.getKey(), addresses);
            splits.add(split);
        }
    }

    log.debug("Split retrieval for %d partitions (%d splits): %dms", partitions.size(), splits.size(),
            splitTimer.elapsed(TimeUnit.MILLISECONDS));

    // the query engine assumes that splits are returned in a somewhat random fashion. The native split manager,
    // because it loads the data from a db table will return the splits somewhat ordered by node id so only a sub
    // set of nodes is fired up. Shuffle the splits to ensure random distribution.
    Collections.shuffle(splits);

    return ImmutableList.copyOf(splits);
}

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

/**
 * Returns a {@code Collector} that builds an {@code ImmutableMultimap}, 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(), ImmutableMultimap::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 ImmutableMultimap}.
 *//*w w w .  j  a  v  a  2s .  com*/
public static <T, K, U> Collector<T, ?, ImmutableMultimap<K, U>> toImmutableMultimap(
        Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) {
    return Collector.<T, ImmutableMultimap.Builder<K, U>, ImmutableMultimap<K, U>>of(
            ImmutableListMultimap::builder,
            (builder, elem) -> builder.put(keyMapper.apply(elem), valueMapper.apply(elem)),
            (left, right) -> left.putAll(right.build()), ImmutableMultimap.Builder::build);
}

From source file:org.jclouds.http.internal.JavaUrlHttpCommandExecutorService.java

@Override
protected HttpResponse invoke(HttpURLConnection connection) throws IOException, InterruptedException {
    HttpResponse.Builder<?> builder = HttpResponse.builder();
    InputStream in = null;/*from   ww  w  . ja v a  2 s .c o  m*/
    try {
        in = consumeOnClose(connection.getInputStream());
    } catch (IOException e) {
        in = bufferAndCloseStream(connection.getErrorStream());
    } catch (RuntimeException e) {
        closeQuietly(in);
        throw propagate(e);
    }

    int responseCode = connection.getResponseCode();
    if (responseCode == 204) {
        closeQuietly(in);
        in = null;
    }
    builder.statusCode(responseCode);
    builder.message(connection.getResponseMessage());

    Builder<String, String> headerBuilder = ImmutableMultimap.builder();
    for (Map.Entry<String, List<String>> entry : connection.getHeaderFields().entrySet()) {
        String header = entry.getKey();
        // HTTP message comes back as a header without a key
        if (header != null)
            headerBuilder.putAll(header, entry.getValue());
    }
    ImmutableMultimap<String, String> headers = headerBuilder.build();
    if (in != null) {
        Payload payload = newInputStreamPayload(in);
        contentMetadataCodec.fromHeaders(payload.getContentMetadata(), headers);
        builder.payload(payload);
    }
    builder.headers(filterOutContentHeaders(headers));
    return builder.build();
}

From source file:io.prestosql.execution.MockRemoteTaskFactory.java

public MockRemoteTask createTableScanTask(TaskId taskId, Node newNode, List<Split> splits,
        PartitionedSplitCountTracker partitionedSplitCountTracker) {
    Symbol symbol = new Symbol("column");
    PlanNodeId sourceId = new PlanNodeId("sourceId");
    PlanFragment testFragment = new PlanFragment(new PlanFragmentId("test"),
            new TableScanNode(sourceId, new TableHandle(new ConnectorId("test"), new TestingTableHandle()),
                    ImmutableList.of(symbol), ImmutableMap.of(symbol, new TestingColumnHandle("column"))),
            ImmutableMap.of(symbol, VARCHAR), SOURCE_DISTRIBUTION, ImmutableList.of(sourceId),
            new PartitioningScheme(Partitioning.create(SINGLE_DISTRIBUTION, ImmutableList.of()),
                    ImmutableList.of(symbol)),
            ungroupedExecution(), StatsAndCosts.empty(), Optional.empty());

    ImmutableMultimap.Builder<PlanNodeId, Split> initialSplits = ImmutableMultimap.builder();
    for (Split sourceSplit : splits) {
        initialSplits.put(sourceId, sourceSplit);
    }//from   w  w  w.  j a  v  a2s  .  c  om
    return createRemoteTask(TEST_SESSION, taskId, newNode, testFragment, initialSplits.build(),
            OptionalInt.empty(), createInitialEmptyOutputBuffers(BROADCAST), partitionedSplitCountTracker,
            true);
}

From source file:org.apache.beam.runners.flink.translation.functions.FlinkBatchSideInputHandlerFactory.java

private <T, W extends BoundedWindow> SideInputHandler<T, W> forIterableSideInput(
        List<WindowedValue<T>> broadcastVariable, Coder<T> elementCoder, Coder<W> windowCoder) {
    ImmutableMultimap.Builder<Object, T> windowToValuesBuilder = ImmutableMultimap.builder();
    for (WindowedValue<T> windowedValue : broadcastVariable) {
        for (BoundedWindow boundedWindow : windowedValue.getWindows()) {
            @SuppressWarnings("unchecked")
            W window = (W) boundedWindow;
            windowToValuesBuilder.put(windowCoder.structuralValue(window), windowedValue.getValue());
        }/*from   w w w  .j a  v  a  2s.  c o  m*/
    }
    ImmutableMultimap<Object, T> windowToValues = windowToValuesBuilder.build();

    return new SideInputHandler<T, W>() {
        @Override
        public Iterable<T> get(byte[] key, W window) {
            return windowToValues.get(windowCoder.structuralValue(window));
        }

        @Override
        public Coder<T> resultCoder() {
            return elementCoder;
        }
    };
}

From source file:com.facebook.buck.android.APKModuleGraph.java

/**
 * Group the classes in the input jars into a multimap based on the APKModule they belong to
 *
 * @param apkModuleToJarPathMap the mapping of APKModules to the path for the jar files
 * @param translatorFunction function used to translate obfuscated names
 * @param filesystem filesystem representation for resolving paths
 * @return The mapping of APKModules to the class names they contain
 * @throws IOException//from  w  w w .  ja  v a2 s  .c  o  m
 */
public static ImmutableMultimap<APKModule, String> getAPKModuleToClassesMap(
        final ImmutableMultimap<APKModule, Path> apkModuleToJarPathMap,
        final Function<String, String> translatorFunction, final ProjectFilesystem filesystem)
        throws IOException {
    final ImmutableMultimap.Builder<APKModule, String> builder = ImmutableMultimap.builder();
    if (!apkModuleToJarPathMap.isEmpty()) {
        for (final APKModule dexStore : apkModuleToJarPathMap.keySet()) {
            for (Path jarFilePath : apkModuleToJarPathMap.get(dexStore)) {
                ClasspathTraverser classpathTraverser = new DefaultClasspathTraverser();
                classpathTraverser.traverse(new ClasspathTraversal(ImmutableSet.of(jarFilePath), filesystem) {
                    @Override
                    public void visit(FileLike entry) {
                        if (!entry.getRelativePath().endsWith(".class")) {
                            // ignore everything but class files in the jar.
                            return;
                        }

                        builder.put(dexStore, translatorFunction.apply(entry.getRelativePath()));
                    }
                });
            }
        }
    }
    return builder.build();
}

From source file:io.leishvl.core.xml.GbSeqXmlBinder.java

/**
 * Infers the possible countries of the species from which the DNA sequence was obtained and 
 * returns a map of Java {@link Locale} where the key of the map is the GenBank field that was
 * used to infer the country. The country is inferred from the annotations of the GenBank file 
 * format, using the fields in the following order:
 * <ol>//www.  ja v a  2s  . c  o m
 * <li>If a country entry exists in the features of the file, then this is returned to 
 * the caller and no other check is performed;</li>
 * <li>Definition field;</li>
 * <li>Title field; or</li>
 * <li>Check PubMed title and abstract fields.</li>
 * </ol>
 * @param sequence - sequence to be analyzed
 * @return a map of Java {@link Locale} inferred from the input sequence, where the key of the map
 *         is the GenBank field used to infer the country.
 */
public static ImmutableMultimap<String, Locale> inferCountry(final GBSeq sequence) {
    checkArgument(sequence != null, "Uninitialized or invalid sequence");
    final ImmutableMultimap.Builder<String, Locale> builder = new ImmutableMultimap.Builder<>();
    // infer from features
    final String countryFeature = countryFeature(sequence);
    Locale locale = isNotBlank(countryFeature) ? countryFeatureToLocale(countryFeature) : null;
    if (locale != null) {
        builder.put("features", locale);
    } else {
        // infer from definition
        // TODO

        // infer from title
        // TODO

        // infer from PubMed title and abstract fields
        // TODO
    }
    return builder.build();
}

From source file:org.jclouds.azurecompute.arm.compute.AzureComputeService.java

@Override
protected void cleanUpIncidentalResourcesOfDeadNodes(Set<? extends NodeMetadata> deadNodes) {
    ImmutableMultimap.Builder<String, String> regionGroups = ImmutableMultimap.builder();
    ImmutableSet.Builder<String> resourceGroups = ImmutableSet.builder();

    for (NodeMetadata deadNode : deadNodes) {
        String resourceGroupName = ResourceGroupAndName.fromSlashEncoded(deadNode.getId()).resourceGroup();
        resourceGroups.add(resourceGroupName);

        if (deadNode.getGroup() != null) {
            regionGroups.put(resourceGroupName, deadNode.getGroup());
        }//from w  w w  .j a  va2  s  .  c  o m

        try {
            cleanupResources.cleanupNode(deadNode.getId());
        } catch (Exception ex) {
            logger.warn(ex, "Error cleaning up resources for node %s", deadNode);
        }
    }

    for (Entry<String, String> regionGroup : regionGroups.build().entries()) {
        cleanupResources.cleanupSecurityGroupIfOrphaned(regionGroup.getKey(), regionGroup.getValue());
    }

    for (String resourceGroup : resourceGroups.build()) {
        cleanupResources.deleteResourceGroupIfEmpty(resourceGroup);
    }
}

From source file:co.cask.cdap.internal.app.runtime.service.http.AbstractHttpServiceResponder.java

/**
 * Creates a {@link Multimap} from an {@link Iterable} of {@link Map.Entry}.
 *///from   ww  w.ja v a2  s.c  om
protected final <K, V> Multimap<K, V> createMultimap(Iterable<? extends Map.Entry<K, V>> entries) {
    ImmutableMultimap.Builder<K, V> builder = ImmutableMultimap.builder();
    for (Map.Entry<K, V> entry : entries) {
        builder.put(entry);
    }
    return builder.build();
}

From source file:com.outerspacecat.icalendar.Component.java

/**
 * Returns the sub-components of this component excluding those whose name is
 * specified in {@code names}.//from   w ww . j  ava 2 s  .c om
 * 
 * @param names the names of the sub-components to exclude. Must be non
 *        {@code null}.
 * @return the sub-components of this component excluding those whose name is
 *         specified in {@code names}. Never {@code null}, contains zero or
 *         more entries.
 */
public ImmutableMultimap<String, Component> getComponentsExcept(final Set<String> names) {
    Preconditions.checkNotNull(names, "names required");

    ImmutableMultimap.Builder<String, Component> ret = ImmutableMultimap.builder();

    for (Component comp : getComponents().values()) {
        if (!names.contains(comp.getName()))
            ret.put(comp.getName(), comp);
    }

    return ret.build();
}