Example usage for com.google.common.collect ImmutableMap.Builder put

List of usage examples for com.google.common.collect ImmutableMap.Builder put

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableMap.Builder put.

Prototype

public final V put(K k, V v) 

Source Link

Usage

From source file:edu.buaa.satla.analysis.util.ImmutableMapMerger.java

/**
 * Join two maps taking the key into account, use {@code func} on duplicate values.
 * @param a input map//from   w ww .  j  a v  a2 s.  co m
 * @param b input map
 * @param func function to merge two values from different maps.
 * @return map containing the union of keys in {@code a} and {@code b}, which
 * contains the value from {@code a} if it contains only in {@code a}, value
 * from {@code b} if it is contained only in {@code b} and {@code func}
 * applied on value from {@code a} and a value from {@code b} otherwise.
 */
public static <K, V> ImmutableMap<K, V> merge(ImmutableMap<K, V> a, ImmutableMap<K, V> b,
        MergeFunc<K, V> func) {
    Set<K> allKeys = new HashSet<>();

    allKeys.addAll(a.keySet());
    allKeys.addAll(b.keySet());

    ImmutableMap.Builder<K, V> builder = ImmutableMap.builder();

    for (K key : allKeys) {
        if (a.containsKey(key) && !b.containsKey(key)) {
            builder.put(key, a.get(key));
        } else if (!a.containsKey(key) && b.containsKey(key)) {
            builder.put(key, b.get(key));
        } else {
            builder.put(key, func.apply(key, a.get(key), b.get(key)));
        }
    }
    return builder.build();
}

From source file:com.facebook.buck.rules.CellPathResolverSerializer.java

@SuppressWarnings("unchecked")
public static CellPathResolver deserialize(Map<String, Object> data) {
    String type = (String) data.get(TYPE);
    Preconditions.checkNotNull(type, "Expected to have value for key %s", TYPE);
    Preconditions.checkArgument(type.equals(TYPE_DEFAULT),
            "Only CellPathResolver with type '%s' are supported, but found '%s' type", TYPE_DEFAULT, type);

    String rootPathAsString = (String) data.get(ROOT_PATH);
    Preconditions.checkNotNull(rootPathAsString, "Expected to have value for key %s", ROOT_PATH);
    Path rootPath = Paths.get(rootPathAsString);

    Map<String, String> cellPathsAsStrings = (Map<String, String>) data.get(CELL_PATHS);
    Preconditions.checkNotNull(cellPathsAsStrings, "Expected to have value for key %s", CELL_PATHS);

    ImmutableMap.Builder<String, Path> cellPaths = ImmutableMap.builder();
    for (Map.Entry<String, String> entry : cellPathsAsStrings.entrySet()) {
        cellPaths.put(entry.getKey(), Paths.get(entry.getValue()));
    }/*from  w  w  w  .j a v a 2  s .com*/

    return new DefaultCellPathResolver(rootPath, cellPaths.build());
}

From source file:com.google.idea.blaze.base.model.primitives.Kind.java

private static ImmutableMap<String, Kind> makeStringToKindMap() {
    ImmutableMap.Builder<String, Kind> result = ImmutableMap.builder();
    for (Kind kind : Kind.values()) {
        result.put(kind.toString(), kind);
    }//from w  ww.  j  a v a  2s . c om
    return result.build();
}

From source file:com.spectralogic.ds3autogen.docspec.DocSpecConverter.java

/**
 * Converts a list of RequestDescriptors into a request documentation map
 * @return Map of normalized request names (key) and descriptions (value)
 *///  www. j  a v  a 2s. c  o  m
protected static ImmutableMap<String, String> toRequestDocs(final List<RequestDescriptor> requestDescriptors,
        final NameMapper nameMapper) {
    if (isEmpty(requestDescriptors)) {
        return ImmutableMap.of();
    }
    final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
    for (final RequestDescriptor rd : requestDescriptors) {
        builder.put(normalizeRequestName(rd, nameMapper), rd.getDescription());
    }
    return builder.build();
}

From source file:com.googlecode.wmbutil.util.ElementUtil.java

public static ImmutableMap<String, Object> asImmutableMap(MbElement parent) throws MbException {
    MbElement child = parent.getFirstChild();

    ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
    while (child != null) {
        if (child.getType() == MbElement.TYPE_NAME_VALUE) {
            builder.put(child.getName(), child.getValue());
        }/*from   ww w .  j  ava2 s .  c  o  m*/
        child = child.getNextSibling();
    }
    return builder.build();
}

From source file:com.spectralogic.ds3contractcomparator.print.simpleprinter.Ds3EnumConstantDiffSimplePrinter.java

/**
 * Converts an {@link ImmutableList} of {@link Ds3Property} into an {@link ImmutableMap} of
 * property names and {@link Ds3Property}
 *//*from   w w  w  . ja  v a  2  s  .  c  o m*/
private static ImmutableMap<String, Ds3Property> toPropertyMap(final ImmutableList<Ds3Property> properties) {
    if (isEmpty(properties)) {
        return ImmutableMap.of();
    }
    final ImmutableMap.Builder<String, Ds3Property> builder = ImmutableMap.builder();
    properties.forEach(property -> builder.put(property.getName(), property));
    return builder.build();
}

From source file:io.prestosql.connector.system.SystemColumnHandle.java

public static Map<String, ColumnHandle> toSystemColumnHandles(ConnectorId connectorId,
        ConnectorTableMetadata tableMetadata) {
    ImmutableMap.Builder<String, ColumnHandle> columnHandles = ImmutableMap.builder();
    for (ColumnMetadata columnMetadata : tableMetadata.getColumns()) {
        columnHandles.put(columnMetadata.getName(),
                new SystemColumnHandle(connectorId, columnMetadata.getName()));
    }/*from w ww. j a v  a 2 s. c  om*/

    return columnHandles.build();
}

From source file:com.google.template.soy.shared.internal.InternalPlugins.java

public static ImmutableMap<String, SoyPrintDirective> fromDirectives(
        Iterable<? extends SoyPrintDirective> directives) {
    ImmutableMap.Builder<String, SoyPrintDirective> builder = ImmutableMap.builder();
    for (SoyPrintDirective directive : directives) {
        builder.put(directive.getName(), directive);
    }//from ww  w.jav a 2  s . co m
    return builder.build();
}

From source file:org.codice.ddf.catalog.ui.security.ShareableMetacardSharingPolicyPlugin.java

private static Map<String, Set<String>> getShareablePermissions() {
    ImmutableMap.Builder<String, Set<String>> builder = new ImmutableMap.Builder<>();
    Constants.SHAREABLE_TAGS.forEach(t -> builder.put(t, ImmutableSet.of(t)));
    return builder.build();
}

From source file:org.apache.gobblin.metrics.Tag.java

/**
 * Converts a {@link List} of {@link Tag}s to a {@link Map} of key, value pairs.
 *
 * @param tags a {@link List} of {@link Tag}s that should be converted to key, value pairs
 *
 * @return a {@link Map} of key, value pairs
 *///from   w w  w .j  a  v a  2  s.co  m
public static <T> Map<? extends String, T> toMap(List<Tag<T>> tags) {
    ImmutableMap.Builder<String, T> tagsMapBuilder = ImmutableMap.builder();
    for (Tag<T> tag : tags) {
        tagsMapBuilder.put(tag.getKey(), tag.getValue());
    }
    return tagsMapBuilder.build();
}