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.core.build.engine.manifest.Manifest.java

/**
 * Create a multimap that's the result of apply the function to the input values, filtered by a
 * predicate.//from   w  ww .  jav a2s . co m
 *
 * <p>This is conceptually similar to {@code filterKeys(index(values, keyFunc), filter)}, but much
 * more efficient as it doesn't construct entries that will be filtered out.
 */
private static <K, V> ImmutableListMultimap<K, V> index(Iterable<V> values, Function<V, K> keyFunc,
        Predicate<K> keyFilter) {
    ImmutableListMultimap.Builder<K, V> builder = new ImmutableListMultimap.Builder<>();
    for (V value : values) {
        K key = keyFunc.apply(value);
        if (keyFilter.test(key)) {
            builder.put(key, value);
        }
    }
    return builder.build();
}

From source file:dynamicrefactoring.domain.xml.writer.JDOMXMLRefactoringWriterImp.java

/**
 * Crea un mapa que contiene como claves los nombres de las clasificaciones
 * y como valores las categorias a las que la refactorizacion pertenece en
 * esa clasificacion./*from   w w  w  .  jav a2  s  .  c  om*/
 * 
 * @return mapa que contiene como claves los nombres de las clasificaciones
 *         y como valores las categorias a las que la refactorizacion
 *         pertenece en esa clasificacion
 */
private ImmutableListMultimap<String, Category> createCategoriesMapByParent() {
    Builder<String, Category> builder = new ImmutableListMultimap.Builder<String, Category>();
    for (Category c : refactoringDefinition.getCategories()) {
        builder.put(c.getParent(), c);
    }
    ImmutableListMultimap<String, Category> categoriesMapByParent = builder.build();
    return categoriesMapByParent;
}

From source file:org.sonar.core.util.stream.Collectors.java

/**
 * Creates an {@link com.google.common.collect.ImmutableListMultimap} from the stream where the values are the values
 * in the stream and the keys are the result of the provided {@link Function keyFunction} applied to each value in the
 * stream.// w  w w  .  j av  a2  s  .c  om
 *
 * <p>
 * Neither {@link Function keyFunction} nor {@link Function valueFunction} can return {@code null}, otherwise a
 * {@link NullPointerException} will be thrown.
 * </p>
 *
 * @throws NullPointerException if {@code keyFunction} or {@code valueFunction} is {@code null}.
 * @throws NullPointerException if result of {@code keyFunction} or {@code valueFunction} is {@code null}.
 */
public static <K, E> Collector<E, ImmutableListMultimap.Builder<K, E>, ImmutableListMultimap<K, E>> index(
        Function<? super E, K> keyFunction) {
    return index(keyFunction, Function.<E>identity());
}

From source file:org.sonar.core.util.stream.Collectors.java

/**
 * Creates an {@link com.google.common.collect.ImmutableListMultimap} from the stream where the values are the result
 * of {@link Function valueFunction} applied to the values in the stream and the keys are the result of the provided
 * {@link Function keyFunction} applied to each value in the stream.
 *
 * <p>/*from w  ww  . j  a  va2s. c o  m*/
 * Neither {@link Function keyFunction} nor {@link Function valueFunction} can return {@code null}, otherwise a
 * {@link NullPointerException} will be thrown.
 * </p>
 *
 * @throws NullPointerException if {@code keyFunction} or {@code valueFunction} is {@code null}.
 * @throws NullPointerException if result of {@code keyFunction} or {@code valueFunction} is {@code null}.
 */
public static <K, E, V> Collector<E, ImmutableListMultimap.Builder<K, V>, ImmutableListMultimap<K, V>> index(
        Function<? super E, K> keyFunction, Function<? super E, V> valueFunction) {
    requireNonNull(keyFunction, "Key function can't be null");
    requireNonNull(valueFunction, "Value function can't be null");
    BiConsumer<ImmutableListMultimap.Builder<K, V>, E> accumulator = (map, element) -> {
        K key = requireNonNull(keyFunction.apply(element), "Key function can't return null");
        V value = requireNonNull(valueFunction.apply(element), "Value function can't return null");

        map.put(key, value);
    };
    BinaryOperator<ImmutableListMultimap.Builder<K, V>> merger = (m1, m2) -> {
        for (Map.Entry<K, V> entry : m2.build().entries()) {
            m1.put(entry.getKey(), entry.getValue());
        }
        return m1;
    };
    return Collector.of(ImmutableListMultimap::builder, accumulator, merger,
            ImmutableListMultimap.Builder::build);
}

From source file:io.prestosql.plugin.raptor.legacy.RaptorMetadata.java

@Override
public Map<SchemaTableName, List<ColumnMetadata>> listTableColumns(ConnectorSession session,
        SchemaTablePrefix prefix) {/*from  www  . j  a va2 s .c om*/
    requireNonNull(prefix, "prefix is null");

    ImmutableListMultimap.Builder<SchemaTableName, ColumnMetadata> columns = ImmutableListMultimap.builder();
    for (TableColumn tableColumn : dao.listTableColumns(prefix.getSchema().orElse(null),
            prefix.getTable().orElse(null))) {
        ColumnMetadata columnMetadata = new ColumnMetadata(tableColumn.getColumnName(),
                tableColumn.getDataType());
        columns.put(tableColumn.getTable(), columnMetadata);
    }
    return Multimaps.asMap(columns.build());
}

From source file:com.sun.tools.hat.internal.server.QueryHandler.java

/**
 * Returns a link to <code>/<var>path</var>/<var>pathInfo</var></code>
 * that can be used to construct a referrer chain. See also the related
 * {@link #printBreadcrumbs} function.//from w w  w. java  2 s . c  om
 *
 * <p>For queries that support referrer chains, there is a primary
 * class that the query works on, followed by a referrer chain that
 * further filters instances. The primary class is specified as
 * {@code clazz}.
 *
 * <p>The referrer chain is always written with parameter name
 * {@code referrer}, so the query handler should use that name to get
 * the referrer chain. Additionally, if the primary class is omitted,
 * then the referrer chain is irrelevant and will not be printed.
 *
 * @param path the static portion of the link target (should only
 *             contain trusted text)
 * @param pathInfo the non-static portion of the link target (will be
 *                 URL-encoded); ignored if {@code name} is omitted
 * @param label the link text to use
 * @param name the parameter name for referring to the primary class;
 *             if omitted, place the class reference in {@code pathInfo}
 * @param clazz the primary class in use
 * @param referrers the referrer chain in use
 * @param tail an optional element to append to the referrer chain
 * @param params any further parameters to be prepended
 * @return an HTML {@code <a>} tag formatted as described
 */
protected static String formatLink(String path, String pathInfo, String label, String name, JavaClass clazz,
        Collection<JavaClass> referrers, JavaClass tail, Multimap<String, String> params) {
    ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder();
    if (params != null) {
        builder.putAll(params);
    }
    if (clazz != null) {
        if (name != null) {
            builder.put(name, clazz.getIdString());
        } else {
            pathInfo = clazz.getIdString();
        }
        if (referrers != null) {
            builder.putAll("referrer", Collections2.transform(referrers, JavaClass::getIdString));
        }
        if (tail != null) {
            builder.put("referrer", tail.getIdString());
        }
    }
    return formatLink(path, pathInfo, label, builder.build());
}

From source file:com.proofpoint.http.client.MediaType.java

/**
 * <em>Replaces</em> all parameters with the given attribute with a single parameter with the
 * given value. If multiple parameters with the same attributes are necessary use
 * {@link #withParameters}. Prefer {@link #withCharset} for setting the {@code charset} parameter
 * when using a {@link Charset} object./*ww  w  .  jav  a  2s .co m*/
 *
 * @throws IllegalArgumentException if either {@code attribute} or {@code value} is invalid
 */
public MediaType withParameter(String attribute, String value) {
    checkNotNull(attribute);
    checkNotNull(value);
    String normalizedAttribute = normalizeToken(attribute);
    ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder();
    for (Entry<String, String> entry : parameters.entries()) {
        String key = entry.getKey();
        if (!normalizedAttribute.equals(key)) {
            builder.put(key, entry.getValue());
        }
    }
    builder.put(normalizedAttribute, normalizeParameterValue(normalizedAttribute, value));
    MediaType mediaType = new MediaType(type, subtype, builder.build());
    // Return one of the constants if the media type is a known type.
    return Objects.firstNonNull(KNOWN_TYPES.get(mediaType), mediaType);
}

From source file:com.google.gerrit.server.notedb.ChangeNotes.java

public ImmutableListMultimap<PatchSet.Id, PatchSetApproval> getApprovals() {
    if (approvals == null) {
        ImmutableListMultimap.Builder<PatchSet.Id, PatchSetApproval> b = ImmutableListMultimap.builder();
        for (Map.Entry<PatchSet.Id, PatchSetApproval> e : state.approvals()) {
            b.put(e.getKey(), new PatchSetApproval(e.getValue()));
        }// www  .  j ava2s  .co  m
        approvals = b.build();
    }
    return approvals;
}

From source file:org.gradle.util.CollectionUtils.java

public static <K, V> ImmutableListMultimap<K, V> groupBy(Iterable<? extends V> iterable,
        Transformer<? extends K, V> grouper) {
    ImmutableListMultimap.Builder<K, V> builder = ImmutableListMultimap.builder();

    for (V element : iterable) {
        K key = grouper.transform(element);
        builder.put(key, element);//from   w  ww  . j  a va 2s .c o  m
    }

    return builder.build();
}

From source file:com.opengamma.strata.collect.Guavate.java

/**
 * Collector used at the end of a stream to build an immutable multimap.
 * <p>/*from w  ww.j  a  v  a 2 s . c  o m*/
 * A collector is used to gather data at the end of a stream operation.
 * This method returns a collector allowing streams to be gathered into
 * an {@link ImmutableListMultimap}.
 * <p>
 * This returns a multimap by converting each stream element to a key and value.
 * Stream elements may be converted to the same key, with the values forming a multimap list.
 *
 * @param <T> the type of the stream elements
 * @param <K> the type of the keys in the result multimap
 * @param <V> the type of the values in the result multimap
 * @param keyExtractor  function to produce keys from stream elements
 * @param valueExtractor  function to produce values from stream elements
 * @return the immutable multimap collector
 */
public static <T, K, V> Collector<T, ?, ImmutableListMultimap<K, V>> toImmutableListMultimap(
        Function<? super T, ? extends K> keyExtractor, Function<? super T, ? extends V> valueExtractor) {

    return Collector.of(ImmutableListMultimap.Builder<K, V>::new,
            (builder, val) -> builder.put(keyExtractor.apply(val), valueExtractor.apply(val)),
            (l, r) -> l.putAll(r.build()), ImmutableListMultimap.Builder<K, V>::build,
            Collector.Characteristics.UNORDERED);
}