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

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

Introduction

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

Prototype

public final void putAll(Map<? extends K, ? extends V> map) 

Source Link

Usage

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

/**
 * Given the set of all Soy directive implementations, a specific Soy directive type (subtype of
 * SoyPrintDirective) to look for, another Soy directive type to look for that is an equivalent
 * deprecated version of the specific Soy directive type, and an adapt function for adapting the
 * deprecated type to the specific type, finds the Soy directives that implement either type and
 * returns them in the form of a map from directive name to directive, where the directives with
 * the deprecated type have been adapted using the adapt function.
 *
 * @param <T> The specific Soy directive type to look for.
 * @param <D> The equivalent deprecated Soy directive type to also look for.
 * @param soyDirectivesSet The set of all Soy directives.
 * @param specificSoyDirectiveType The class of the specific Soy directive type to look for.
 * @param equivDeprecatedSoyDirectiveType The class of the equivalent deprecated Soy directive
 *     type to also look for.//from   w w  w .j  av  a  2 s.c  o m
 * @param adaptFn The adapt function that adapts the deprecated type to the specific type.
 * @return A map of the relevant specific Soy directives (name to directive).
 */
public static <T extends SoyPrintDirective, D extends SoyPrintDirective> ImmutableMap<String, T> buildSpecificSoyDirectivesMapWithAdaptation(
        Set<SoyPrintDirective> soyDirectivesSet, Class<T> specificSoyDirectiveType,
        Class<D> equivDeprecatedSoyDirectiveType, Function<D, T> adaptFn) {

    ImmutableMap<String, T> tMap = buildSpecificSoyDirectivesMap(soyDirectivesSet, specificSoyDirectiveType);
    ImmutableMap<String, D> dMap = buildSpecificSoyDirectivesMap(soyDirectivesSet,
            equivDeprecatedSoyDirectiveType);

    ImmutableMap.Builder<String, T> resultMapBuilder = ImmutableMap.builder();
    resultMapBuilder.putAll(tMap);
    for (String directiveName : dMap.keySet()) {
        if (tMap.containsKey(directiveName)) {
            if (tMap.get(directiveName).equals(dMap.get(directiveName))) {
                throw new IllegalStateException(String.format(
                        "Found print directive named '%s' that implements both %s and"
                                + " %s -- please remove the latter deprecated interface.",
                        directiveName, specificSoyDirectiveType.getSimpleName(),
                        equivDeprecatedSoyDirectiveType.getSimpleName()));
            } else {
                throw new IllegalStateException(String.format(
                        "Found two print directives with the same name '%s', one implementing %s and the"
                                + " other implementing %s",
                        directiveName, specificSoyDirectiveType.getSimpleName(),
                        equivDeprecatedSoyDirectiveType.getSimpleName()));
            }
        }
        resultMapBuilder.put(directiveName, adaptFn.apply(dMap.get(directiveName)));
    }
    return resultMapBuilder.build();
}

From source file:com.linecorp.armeria.server.thrift.ThriftDocString.java

/**
 * Parses DocStrings from input Thrift IDL JSON Resources.
 * @return a map with key is FQCN and value is document string.
 *//*from  www  .  ja v a  2s.  co  m*/
static Map<String, String> parseDocStrings(ClassLoader classLoader, Iterable<String> jsonPaths) {
    final ImmutableMap.Builder<String, String> docStrings = ImmutableMap.builder();
    for (String jsonPath : jsonPaths) {
        docStrings.putAll(getDocStringsFromJsonResource(classLoader, jsonPath));
    }
    return docStrings.build();
}

From source file:com.google.javascript.jscomp.newtypes.TypeParameters.java

static TypeParameters make(List<String> ordinaryTypeParams, Map<String, Node> ttlParams) {
    ImmutableMap.Builder<String, Node> builder = ImmutableMap.builder();
    for (String typeParam : ordinaryTypeParams) {
        builder.put(typeParam, IR.empty());
    }// w w  w . j av  a 2 s. c om
    builder.putAll(ttlParams);
    return new TypeParameters(builder.build());
}

From source file:com.facebook.buck.cxx.toolchain.CxxPlatformsProviderFactory.java

private static CxxPlatformsProvider createProvider(BuckConfig config,
        ImmutableMap<Flavor, UnresolvedCxxPlatform> cxxSystemPlatforms) {
    Platform platform = Platform.detect();
    CxxBuckConfig cxxBuckConfig = new CxxBuckConfig(config);

    // Create a map of system platforms.
    ImmutableMap.Builder<Flavor, UnresolvedCxxPlatform> cxxSystemPlatformsBuilder = ImmutableMap.builder();

    cxxSystemPlatformsBuilder.putAll(cxxSystemPlatforms);

    CxxPlatform defaultHostCxxPlatform = DefaultCxxPlatforms.build(platform, cxxBuckConfig);
    cxxSystemPlatformsBuilder.put(defaultHostCxxPlatform.getFlavor(),
            new StaticUnresolvedCxxPlatform(defaultHostCxxPlatform));
    ImmutableMap<Flavor, UnresolvedCxxPlatform> cxxSystemPlatformsMap = cxxSystemPlatformsBuilder.build();

    cxxSystemPlatformsMap = appendHostPlatformIfNeeded(defaultHostCxxPlatform, cxxSystemPlatformsMap);

    Map<Flavor, UnresolvedCxxPlatform> cxxOverridePlatformsMap = updateCxxPlatformsWithOptionsFromBuckConfig(
            platform, config, cxxSystemPlatformsMap, defaultHostCxxPlatform);

    UnresolvedCxxPlatform hostCxxPlatform = getHostCxxPlatform(cxxBuckConfig, cxxOverridePlatformsMap);
    cxxOverridePlatformsMap.put(hostCxxPlatform.getFlavor(), hostCxxPlatform);

    ImmutableMap<Flavor, UnresolvedCxxPlatform> cxxPlatformsMap = ImmutableMap
            .<Flavor, UnresolvedCxxPlatform>builder().putAll(cxxOverridePlatformsMap).build();

    // Build up the final list of C/C++ platforms.
    FlavorDomain<UnresolvedCxxPlatform> cxxPlatforms = new FlavorDomain<>("C/C++ platform", cxxPlatformsMap);

    // Get the default target platform from config.
    UnresolvedCxxPlatform defaultCxxPlatform = CxxPlatforms.getConfigDefaultCxxPlatform(cxxBuckConfig,
            cxxPlatformsMap, hostCxxPlatform);

    return CxxPlatformsProvider.of(defaultCxxPlatform, cxxPlatforms);
}

From source file:org.lazulite.boot.autoconfigure.core.csv.StringArrayToObjectConverter.java

private static Map<Integer, ValueSetter> buildFieldMap(Class<?> targetClass) {
    HashMap setterMap = Maps.newHashMap();

    try {//from   w  w w .j a v a  2  s .  c  o  m
        Field[] var5;
        int var4 = (var5 = targetClass.getFields()).length;

        for (int var3 = 0; var3 < var4; ++var3) {
            Field e = var5[var3];
            CsvColumn column = (CsvColumn) e.getAnnotation(CsvColumn.class);
            if (column != null) {
                int index = column.value();
                if (setterMap.containsKey(Integer.valueOf(index))) {
                    throw new RuntimeException("Column [" + index + "] mapped to more one+ fields");
                }

                StringArrayToObjectConverter.ValueSetter valueSetter = new StringArrayToObjectConverter.ValueSetter(
                        e, column.trim());
                if (column.converter() != Object.class) {
                    valueSetter.setConverter((IStringConverter) column.converter().newInstance());
                } else {
                    Class type = e.getType();
                    if (type == String.class) {
                        valueSetter.setConverter(new IStringConverter() {
                            public String convert(String rawField) {
                                return SimpleStringConverters.toString(rawField);
                            }
                        });
                    } else if (type == Integer.class) {
                        valueSetter.setConverter(new IStringConverter() {
                            public Integer convert(String rawField) {
                                return SimpleStringConverters.toInteger(rawField);
                            }
                        });
                    } else if (type == Integer.TYPE) {
                        valueSetter.setConverter(new IStringConverter() {
                            public Integer convert(String rawField) {
                                return SimpleStringConverters.toInt(rawField);
                            }
                        });
                    } else if (type == Boolean.class) {
                        valueSetter.setConverter(new IStringConverter() {
                            public Boolean convert(String rawField) {
                                return SimpleStringConverters.toBoolean(rawField);
                            }
                        });
                    } else if (type == Boolean.TYPE) {
                        valueSetter.setConverter(new IStringConverter() {
                            public Boolean convert(String rawField) {
                                return SimpleStringConverters.toBool(rawField);
                            }
                        });
                    } else {
                        Method method = type.getMethod("fromString", new Class[] { String.class });
                        valueSetter.setConverter(new StringArrayToObjectConverter.FromStringConverter(method));
                    }
                }

                setterMap.put(Integer.valueOf(index), valueSetter);
            }
        }

        ImmutableMap.Builder var13 = new ImmutableMap.Builder();
        var13.putAll(setterMap);
        return var13.build();
    } catch (RuntimeException var11) {
        throw var11;
    } catch (Exception var12) {
        throw new RuntimeException(var12);
    }
}

From source file:com.opengamma.strata.examples.marketdata.timeseries.FixingSeriesCsvLoader.java

/**
 * Loads a set of historical fixing series into memory from CSV resources.
 * /*from ww  w . ja  va2  s.  co m*/
 * @param fixingSeriesResources  the fixing series CSV resources
 * @return the loaded fixing series, mapped by {@linkplain ObservableId observable ID}
 */
public static Map<ObservableId, LocalDateDoubleTimeSeries> loadFixingSeries(
        Collection<ResourceLocator> fixingSeriesResources) {
    ImmutableMap.Builder<ObservableId, LocalDateDoubleTimeSeries> builder = ImmutableMap.builder();
    for (ResourceLocator timeSeriesResource : fixingSeriesResources) {
        // builder ensures keys can only be seen once
        builder.putAll(loadFixingSeries(timeSeriesResource));
    }
    return builder.build();
}

From source file:org.apache.beam.runners.dataflow.util.CloudObjects.java

private static Map<String, CloudObjectTranslator<? extends Coder>> populateCloudObjectTranslators() {
    ImmutableMap.Builder<String, CloudObjectTranslator<? extends Coder>> builder = ImmutableMap.builder();
    for (CoderCloudObjectTranslatorRegistrar coderRegistrar : ServiceLoader
            .load(CoderCloudObjectTranslatorRegistrar.class)) {
        builder.putAll(coderRegistrar.classNamesToTranslators());
    }// w  w  w  .  ja v  a 2  s . c o m
    return builder.build();
}

From source file:org.apache.beam.runners.dataflow.util.CloudObjects.java

private static Map<Class<? extends Coder>, CloudObjectTranslator<? extends Coder>> populateCoderTranslators() {
    ImmutableMap.Builder<Class<? extends Coder>, CloudObjectTranslator<? extends Coder>> builder = ImmutableMap
            .builder();//  ww  w .j  ava2 s  .c  o m
    for (CoderCloudObjectTranslatorRegistrar coderRegistrar : ServiceLoader
            .load(CoderCloudObjectTranslatorRegistrar.class)) {
        builder.putAll(coderRegistrar.classesToTranslators());
    }
    return builder.build();
}

From source file:io.airlift.drift.transport.netty.codec.HeaderTransport.java

/**
 * Decodes the ByteBuf into a HeaderFrame transferring the reference ownership.
 * @param buffer buffer to be decoded; reference count ownership is transferred to this method
 * @return the decoded frame; caller is responsible for releasing this object
 *///from  www  .  ja  va  2 s.  com
public static ThriftFrame decodeFrame(ByteBuf buffer) {
    ByteBuf messageHeader = null;
    try {
        // frame header
        short magic = buffer.readShort();
        verify(magic == HEADER_MAGIC, "Invalid header magic");
        short flags = buffer.readShort();
        boolean outOfOrderResponse;
        switch (flags) {
        case FLAGS_NONE:
            outOfOrderResponse = false;
            break;
        case FLAG_SUPPORT_OUT_OF_ORDER:
            outOfOrderResponse = true;
            break;
        default:
            throw new IllegalArgumentException("Unsupported header flags: " + flags);
        }
        int frameSequenceId = buffer.readInt();
        int headerSize = buffer.readShort() << 2;
        messageHeader = buffer.readBytes(headerSize);

        // encoding info
        byte protocolId = messageHeader.readByte();
        Protocol protocol = Protocol.getProtocolByHeaderTransportId(protocolId);
        byte numberOfTransforms = messageHeader.readByte();
        if (numberOfTransforms > 0) {
            // currently there are only two transforms, a cryptographic extension which is deprecated, and gzip which is too expensive
            throw new IllegalArgumentException("Unsupported transform");
        }

        // headers
        // todo what about duplicate headers?
        ImmutableMap.Builder<String, String> allHeaders = ImmutableMap.builder();
        allHeaders.putAll(decodeHeaders(NORMAL_HEADERS, messageHeader));
        allHeaders.putAll(decodeHeaders(PERSISTENT_HEADERS, messageHeader));

        // message
        ByteBuf message = buffer.readBytes(buffer.readableBytes());

        // header frame wraps message byte buffer, so message should not be release yet
        return new ThriftFrame(frameSequenceId, message, allHeaders.build(), HEADER, protocol,
                outOfOrderResponse);
    } finally {
        // message header in an independent buffer and must be released
        if (messageHeader != null) {
            messageHeader.release();
        }

        // input buffer has been consumed and transformed into a HeaderFrame, so release it
        buffer.release();
    }
}

From source file:org.zanata.model.tm.TMXMetadataHelper.java

/**
 * Gets all the entity's metadata in a single Map.
 *
 * @param tu/*from w w  w.  j a  v a  2  s .  c  o m*/
 * @return
 */
@Nonnull
public static ImmutableMap<String, String> getAttributes(TransMemoryUnitVariant fromTuv) {
    ImmutableMap.Builder<String, String> m = ImmutableMap.builder();
    m.putAll(getSharedMetadata(fromTuv));
    String lang = fromTuv.getLanguage();
    assert lang != null;
    m.put(XML_LANG, lang);
    return m.build();
}