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:org.zanata.model.tm.TMXMetadataHelper.java

/**
 * Gets all the entity's metadata in a single Map.
 *
 * @param tu//w w w  .  j  a va  2s .c o m
 * @return
 */
@Nonnull
public static ImmutableMap<String, String> getAttributes(TransMemory fromTm) {
    ImmutableMap.Builder<String, String> m = ImmutableMap.builder();
    m.putAll(getSharedMetadata(fromTm));
    String srclang = fromTm.getSourceLanguage();
    if (srclang != null) {
        m.put(SRC_LANG, srclang);
    }
    return m.build();
}

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

@Nonnull
private static ImmutableMap<String, String> getSharedMetadata(HasTMMetadata fromEntity) {
    ImmutableMap.Builder<String, String> m = ImmutableMap.builder();
    m.putAll(getGenericMetadata(fromEntity));
    Date creationDate = fromEntity.getCreationDate();
    if (creationDate != null) {
        m.put(CREATION_DATE, toString(creationDate));
    }/*  w  w  w . j  a va2 s  .co m*/
    Date lastChanged = fromEntity.getLastChanged();
    if (lastChanged != null) {
        m.put(CHANGE_DATE, toString(lastChanged));
    }
    return m.build();
}

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

/**
 * Gets all the entity's metadata in a single Map.
 *
 * @param fromTu/*www  .j a  va  2s.c o  m*/
 * @return
 */
@Nonnull
public static ImmutableMap<String, String> getAttributes(TransMemoryUnit fromTu) {
    ImmutableMap.Builder<String, String> m = ImmutableMap.builder();
    m.putAll(getSharedMetadata(fromTu));
    String tuid = fromTu.getTransUnitId();
    if (tuid != null) {
        m.put(TUID, tuid);
    }
    String srclang = fromTu.getSourceLanguage();
    if (srclang != null) {
        m.put(SRC_LANG, srclang);
    }
    return m.build();
}

From source file:org.chaston.oakfunds.storage.RecordType.java

private static ImmutableMap<String, JdbcTypeHandler> buildTypeHandlers(
        ImmutableMap<String, AttributeType> attributes, @Nullable RecordType<?> parentType) {
    ImmutableMap.Builder<String, JdbcTypeHandler> jdbcTypeHandlers = ImmutableMap.builder();
    if (parentType != null) {
        jdbcTypeHandlers.putAll(parentType.jdbcTypeHandlers);
    }//from   w w w .j  a  v a 2s .c  om
    for (AttributeType attributeType : attributes.values()) {
        jdbcTypeHandlers.put(attributeType.getName(), createJdbcTypeHandler(attributeType));
    }
    return jdbcTypeHandlers.build();
}

From source file:com.spectralogic.ds3autogen.converters.ResponseTypeConverter.java

/**
 * Creates new types for generating encapsulating models. This occurs when a response
 * handler has a return type with a component type. This is done to ensure that all
 * response types can be properly parsed within the generated code.
 * @param requests A Ds3Request/*from www .  j  a  v  a 2 s. com*/
 * @param types A list of Ds3Types
 * @return The list of Ds3Types with the additionally generated types to handle the
 * response payloads that return arrays
 */
protected static ImmutableMap<String, Ds3Type> createEncapsulatingModelResponseTypes(
        final ImmutableList<Ds3Request> requests, final ImmutableMap<String, Ds3Type> types) {
    if (isEmpty(requests)) {
        LOG.debug("No requests to generate encapsulating models for");
        return types;
    }
    final ImmutableSet<EncapsulatingTypeNames> encapsulatingTypes = getResponseComponentTypesFromRequests(
            requests);
    if (isEmpty(encapsulatingTypes)) {
        LOG.debug("Requests do not contain any responses with component types");
        return types;
    }
    final ImmutableMap.Builder<String, Ds3Type> builder = ImmutableMap.builder();
    if (hasContent(types)) {
        builder.putAll(types);
    }
    for (final EncapsulatingTypeNames encapsulatingType : encapsulatingTypes) {
        final Ds3Type ds3Type = toDs3Type(encapsulatingType, types);
        builder.put(ds3Type.getName(), ds3Type);
    }
    return builder.build();
}

From source file:org.caleydo.data.importer.tcga.model.ClinicalMapping.java

public static Map<String, String> getAliasMap(Collection<String> toInclude) {
    ImmutableMap.Builder<String, String> b = ImmutableMap.builder();
    if (toInclude == null || toInclude.isEmpty()) {
        for (ClinicalMapping m : types) {
            b.putAll(m.getAliasLookup());
        }/* ww  w .j av  a  2s.  com*/
    } else {
        for (String include : toInclude) {
            ClinicalMapping r = byName(include);
            if (r == null)
                continue;
            b.putAll(r.getAliasLookup());
        }
    }
    return b.build();
}

From source file:me.yanaga.guava.stream.MoreCollectors.java

private static <T, K, V, M extends ImmutableMap<K, V>> Collector<T, ?, M> toImmutableMap(
        Supplier<ImmutableMap.Builder<K, V>> supplier, Function<? super T, ? extends K> keyMapper,
        Function<? super T, ? extends V> valueMapper, Collector.Characteristics... characteristics) {
    return Collector.of(supplier, new BiConsumer<ImmutableMap.Builder<K, V>, T>() {
        @Override/*from   w  w w.  j av a2  s  .c o m*/
        public void accept(ImmutableMap.Builder<K, V> objectObjectBuilder, T t) {
            objectObjectBuilder.put(keyMapper.apply(t), valueMapper.apply(t));
        }
    }, new BinaryOperator<ImmutableMap.Builder<K, V>>() {
        @Override
        public ImmutableMap.Builder<K, V> apply(ImmutableMap.Builder<K, V> kuBuilder,
                ImmutableMap.Builder<K, V> kuBuilder2) {
            return kuBuilder.putAll(kuBuilder2.build());
        }
    }, new Function<ImmutableMap.Builder<K, V>, M>() {
        @SuppressWarnings("unchecked")
        @Override
        public M apply(ImmutableMap.Builder<K, V> kuBuilder) {
            return (M) kuBuilder.build();
        }
    }, characteristics);
}

From source file:org.apache.gobblin.lineage.LineageInfo.java

/**
 * Retrieve all lineage information from different {@link State}s.
 * This requires the job id and dataset urn to be present in the state, under job.id and dataset.urn.
 * A global union operation is applied to combine all <K, V> pairs from the input {@link State}s. If multiple {@link State}s
 * share the same K, but have conflicting V, a {@link LineageException} is thrown.
 *
 * {@link Level} can control if a dataset level or branch level information should be used. When {@link Level#All} is
 * specified, all levels of information will be returned; otherwise only specified level of information will be returned.
 *
 * For instance, assume we have below input states:
 *    State[0]: gobblin.lineage.K1          ---> V1
 *              gobblin.lineage.K2          ---> V2
 *              gobblin.lineage.branch.1.K4 ---> V4
 *    State[1]: gobblin.lineage.K2          ---> V2
 *              gobblin.lineage.K3          ---> V3
 *              gobblin.lineage.branch.1.K4 ---> V4
 *              gobblin.lineage.branch.1.K5 ---> V5
 *              gobblin.lineage.branch.2.K6 ---> V6
 *
 *  (1) With {@link Level#DATASET} level, the output would be:
 *      LinieageInfo[0]:  K1 ---> V1/*from  w w w .  java2s .  c  o m*/
 *                        K2 ---> V2
 *                        K3 ---> V3
 *  (2) With {@link Level#All} level, the output would be: (because there are two branches, so there are two LineageInfo)
 *      LineageInfo[0]:   K1 ---> V1
 *                        K2 ---> V2
 *                        K3 ---> V3
 *                        K4 ---> V4
 *                        K5 ---> V5
 *
 *      LineageInfo[1]:   K1 ---> V1
 *                        K2 ---> V2
 *                        K3 ---> V3
 *                        K6 ---> V6
 *
 *   (3) With {@link Level#BRANCH} level, the output would be: (only branch level information was returned)
 *      LineageInfo[0]:   K4 ---> V4
 *                        K5 ---> V5
 *      LineageInfo[1]:   K6 ---> V6
 *
 * @param states All states which belong to the same dataset and share the same jobId.
 * @param level {@link Level#DATASET}  only load dataset level lineage attributes
 *              {@link Level#BRANCH}   only load branch level lineage attributes
 *              {@link Level#All}      load all lineage attributes
 * @return A collection of {@link LineageInfo}s per branch. When level is {@link Level#DATASET}, this list has only single element.
 *
 * @throws LineageException.LineageConflictAttributeException if two states have same key but not the same value.
 */
public static Collection<LineageInfo> load(Collection<? extends State> states, Level level)
        throws LineageException {
    Preconditions.checkArgument(states != null && !states.isEmpty());
    Map<String, String> datasetMetaData = new HashMap<>();
    Map<String, Map<String, String>> branchAggregate = new HashMap<>();

    State anyOne = states.iterator().next();
    String jobId = anyOne.getProp(ConfigurationKeys.JOB_ID_KEY, "");
    String urn = anyOne.getProp(ConfigurationKeys.DATASET_URN_KEY, ConfigurationKeys.DEFAULT_DATASET_URN);

    for (State state : states) {
        for (Map.Entry<Object, Object> entry : state.getProperties().entrySet()) {
            if (entry.getKey() instanceof String && ((String) entry.getKey()).startsWith(LINEAGE_NAME_SPACE)) {

                String lineageKey = ((String) entry.getKey());
                String lineageValue = (String) entry.getValue();

                if (lineageKey.startsWith(BRANCH_PREFIX)) {
                    String branchPrefixStrip = lineageKey.substring(BRANCH_PREFIX.length());
                    String branchId = branchPrefixStrip.substring(0, branchPrefixStrip.indexOf("."));
                    String key = branchPrefixStrip.substring(branchPrefixStrip.indexOf(".") + 1);

                    if (level == Level.BRANCH || level == Level.All) {
                        if (!branchAggregate.containsKey(branchId)) {
                            branchAggregate.put(branchId, new HashMap<>());
                        }
                        Map<String, String> branchMetaData = branchAggregate.get(branchId);
                        String prev = branchMetaData.put(key, lineageValue);
                        if (prev != null && !prev.equals(lineageValue)) {
                            throw new LineageException.LineageConflictAttributeException(lineageKey, prev,
                                    lineageValue);
                        }
                    }
                } else if (lineageKey.startsWith(DATASET_PREFIX)) {
                    if (level == Level.DATASET || level == Level.All) {
                        String prev = datasetMetaData.put(lineageKey.substring(DATASET_PREFIX.length()),
                                lineageValue);
                        if (prev != null && !prev.equals(lineageValue)) {
                            throw new LineageException.LineageConflictAttributeException(lineageKey, prev,
                                    lineageValue);
                        }
                    }
                }
            }
        }
    }

    Collection<LineageInfo> collection = Sets.newHashSet();

    if (level == Level.DATASET) {
        ImmutableMap<String, String> metaData = ImmutableMap.<String, String>builder().putAll(datasetMetaData)
                .build();
        collection.add(new LineageInfo(urn, jobId, metaData));
        return collection;
    } else if (level == Level.BRANCH || level == Level.All) {
        if (branchAggregate.isEmpty()) {
            if (level == Level.All) {
                collection.add(new LineageInfo(urn, jobId,
                        ImmutableMap.<String, String>builder().putAll(datasetMetaData).build()));
            }
            return collection;
        }
        for (Map.Entry<String, Map<String, String>> branchMetaDataEntry : branchAggregate.entrySet()) {
            String branchId = branchMetaDataEntry.getKey();
            Map<String, String> branchMetaData = branchMetaDataEntry.getValue();
            ImmutableMap.Builder<String, String> metaDataBuilder = ImmutableMap.builder();
            if (level == Level.All) {
                metaDataBuilder.putAll(datasetMetaData);
            }
            metaDataBuilder.putAll(branchMetaData).put(BRANCH_ID_METADATA_KEY, branchId);
            collection.add(new LineageInfo(urn, jobId, metaDataBuilder.build()));
        }

        return collection;
    } else {
        throw new LineageException.LineageUnsupportedLevelException(level);
    }
}

From source file:org.apache.aurora.scheduler.configuration.executor.ExecutorModule.java

private static ExecutorSettings makeExecutorSettings() {
    try {/*from w  ww. j  ava2  s .  c om*/

        ImmutableMap.Builder<String, ExecutorConfig> configsBuilder = ImmutableMap.builder();

        configsBuilder.put(apiConstants.AURORA_EXECUTOR_NAME, makeThermosExecutorConfig());

        if (CUSTOM_EXECUTOR_CONFIG.hasAppliedValue()) {
            configsBuilder.putAll(ExecutorSettingsLoader.read(
                    Files.newBufferedReader(CUSTOM_EXECUTOR_CONFIG.get().toPath(), StandardCharsets.UTF_8)));
        }

        return new ExecutorSettings(configsBuilder.build(), POPULATE_DISCOVERY_INFO.get());

    } catch (ExecutorSettingsLoader.ExecutorConfigException | IOException e) {
        throw new IllegalArgumentException("Failed to read executor settings: " + e, e);
    }
}

From source file:com.google.api.codegen.config.ApiConfig.java

private static ImmutableMap<String, ResourceNameConfig> createResourceNameConfigs(DiagCollector diagCollector,
        ConfigProto configProto, ProtoFile file) {
    ImmutableMap<String, SingleResourceNameConfig> singleResourceNameConfigs = createSingleResourceNameConfigs(
            diagCollector, configProto, file);
    ImmutableMap<String, FixedResourceNameConfig> fixedResourceNameConfigs = createFixedResourceNameConfigs(
            diagCollector, configProto.getFixedResourceNameValuesList(), file);
    ImmutableMap<String, ResourceNameOneofConfig> resourceNameOneofConfigs = createResourceNameOneofConfigs(
            diagCollector, configProto.getCollectionOneofsList(), singleResourceNameConfigs,
            fixedResourceNameConfigs, file);

    ImmutableMap.Builder<String, ResourceNameConfig> resourceCollectionMap = ImmutableMap.builder();
    resourceCollectionMap.putAll(singleResourceNameConfigs);
    resourceCollectionMap.putAll(resourceNameOneofConfigs);
    resourceCollectionMap.putAll(fixedResourceNameConfigs);
    return resourceCollectionMap.build();
}