Example usage for com.google.common.collect Maps transformValues

List of usage examples for com.google.common.collect Maps transformValues

Introduction

In this page you can find the example usage for com.google.common.collect Maps transformValues.

Prototype

@GwtIncompatible("NavigableMap")
public static <K, V1, V2> NavigableMap<K, V2> transformValues(NavigableMap<K, V1> fromMap,
        Function<? super V1, V2> function) 

Source Link

Document

Returns a view of a navigable map where each value is transformed by a function.

Usage

From source file:brooklyn.entity.mesos.task.marathon.MarathonTaskImpl.java

private Map<String, Object> getMarathonFlags(Entity entity) {
    MutableMap.Builder<String, Object> builder = MutableMap.builder();
    ConfigBag provisioningProperties = ConfigBag
            .newInstance(entity.config().get(SoftwareProcess.PROVISIONING_PROPERTIES));

    // CPU// w  w w.  ja v a  2  s.  c  o  m
    Double cpus = entity.config().get(MarathonTask.CPU_RESOURCES);
    if (cpus == null)
        cpus = config().get(MarathonTask.CPU_RESOURCES);
    if (cpus == null) {
        Integer minCores = entity.config().get(JcloudsLocationConfig.MIN_CORES);
        if (minCores == null) {
            minCores = provisioningProperties.get(JcloudsLocationConfig.MIN_CORES);
        }
        if (minCores == null) {
            TemplateBuilder template = provisioningProperties.get(JcloudsLocationConfig.TEMPLATE_BUILDER);
            if (template != null) {
                minCores = 0;
                for (Processor cpu : template.build().getHardware().getProcessors()) {
                    minCores = minCores + (int) cpu.getCores();
                }
            }
        }
        if (minCores != null) {
            cpus = 1.0d * minCores;
        }
    }
    if (cpus == null)
        cpus = 0.25d;
    builder.put("cpus", cpus);

    // Memory
    Integer memory = entity.config().get(MarathonTask.MEMORY_RESOURCES);
    if (memory == null)
        memory = config().get(MarathonTask.MEMORY_RESOURCES);
    if (memory == null) {
        Integer minRam = parseMbSizeString(entity.config().get(JcloudsLocationConfig.MIN_RAM));
        if (minRam == null) {
            minRam = parseMbSizeString(provisioningProperties.get(JcloudsLocationConfig.MIN_RAM));
        }
        if (minRam == null) {
            TemplateBuilder template = provisioningProperties.get(JcloudsLocationConfig.TEMPLATE_BUILDER);
            if (template != null) {
                minRam = template.build().getHardware().getRam();
            }
        }
        if (minRam != null) {
            memory = minRam;
        }
    }
    if (memory == null)
        memory = 256;
    builder.put("memory", memory);

    // Inbound ports
    Set<Integer> entityOpenPorts = MutableSet.copyOf(DockerUtils.getContainerPorts(entity));
    entityOpenPorts.addAll(DockerUtils.getOpenPorts(entity));
    if (!config().get(DockerContainer.DOCKER_USE_SSH)) {
        entityOpenPorts.remove(22);
    }
    builder.put("openPorts", Ints.toArray(entityOpenPorts));
    sensors().set(DockerAttributes.DOCKER_CONTAINER_OPEN_PORTS, ImmutableList.copyOf(entityOpenPorts));
    entity.sensors().set(DockerAttributes.DOCKER_CONTAINER_OPEN_PORTS, ImmutableList.copyOf(entityOpenPorts));

    // Direct port mappings
    // Note that the Marathon map is reversed, from container to host, with 0 indicating any host port
    Map<Integer, Integer> bindings = MutableMap.of();
    Map<Integer, Integer> marathonBindings = MutableMap.of();
    for (Integer port : entityOpenPorts) {
        marathonBindings.put(port, 0);
    }
    Map<Integer, Integer> entityBindings = entity.config().get(DockerAttributes.DOCKER_PORT_BINDINGS);
    if (entityBindings != null) {
        for (Integer host : entityBindings.keySet()) {
            bindings.put(entityBindings.get(host), host);
            marathonBindings.put(host, entityBindings.get(host));
        }
    }
    if (bindings.isEmpty()) {
        List<PortAttributeSensorAndConfigKey> entityPortConfig = entity.config()
                .get(DockerAttributes.DOCKER_DIRECT_PORT_CONFIG);
        if (entityPortConfig != null) {
            for (PortAttributeSensorAndConfigKey key : entityPortConfig) {
                PortRange range = entity.config().get(key);
                if (range != null && !range.isEmpty()) {
                    Integer port = range.iterator().next();
                    if (port != null) {
                        bindings.put(port, port);
                        marathonBindings.put(port, port);
                    }
                }
            }
        }
        List<Integer> entityPorts = entity.config().get(DockerAttributes.DOCKER_DIRECT_PORTS);
        if (entityPorts != null) {
            for (Integer port : entityPorts) {
                bindings.put(port, port);
                marathonBindings.put(port, port);
            }
        }
    }
    sensors().set(DockerAttributes.DOCKER_CONTAINER_PORT_BINDINGS, bindings);
    entity.sensors().set(DockerAttributes.DOCKER_CONTAINER_PORT_BINDINGS, bindings);
    builder.put("portBindings", Lists.newArrayList(marathonBindings.entrySet()));

    // Environment variables and Docker links
    Map<String, Object> environment = MutableMap.copyOf(config().get(DOCKER_CONTAINER_ENVIRONMENT));
    environment.putAll(MutableMap.copyOf(entity.config().get(DOCKER_CONTAINER_ENVIRONMENT)));
    List<Entity> links = entity.config().get(DockerAttributes.DOCKER_LINKS);
    if (links != null && links.size() > 0) {
        LOG.debug("Found links: {}", links);
        Map<String, String> extraHosts = MutableMap.of();
        for (Entity linked : links) {
            Map<String, Object> linkVars = DockerUtils.generateLinks(getRunningEntity(), linked);
            environment.putAll(linkVars);
            Optional<String> alias = DockerUtils.getUniqueContainerName(linked);
            if (alias.isPresent()) {
                String targetAddress = DockerUtils.getTargetAddress(getRunningEntity(), linked);
                extraHosts.put(alias.get(), targetAddress);
            }
        }
        builder.put("extraHosts", Lists.newArrayList(extraHosts.entrySet()));
    }
    sensors().set(DockerContainer.DOCKER_CONTAINER_ENVIRONMENT, ImmutableMap.copyOf(environment));
    entity.sensors().set(DockerContainer.DOCKER_CONTAINER_ENVIRONMENT, ImmutableMap.copyOf(environment));
    builder.put("environment",
            Lists.newArrayList(Maps.transformValues(environment, Functions.toStringFunction()).entrySet()));

    // Volumes
    Map<String, String> volumes = MutableMap.of();
    Map<String, String> mapping = entity.config().get(DockerHost.DOCKER_HOST_VOLUME_MAPPING);
    if (mapping != null) {
        for (String source : mapping.keySet()) {
            volumes.put(source, mapping.get(source));
        }
    }
    sensors().set(DockerAttributes.DOCKER_VOLUME_MAPPING, volumes);
    entity.sensors().set(DockerAttributes.DOCKER_VOLUME_MAPPING, volumes);
    builder.put("volumes", Lists.newArrayList(volumes.entrySet()));

    // URIs to copy
    List<String> uris = MutableList.copyOf(config().get(TASK_URI_LIST));
    uris.addAll(MutableList.copyOf(entity.config().get(TASK_URI_LIST)));
    sensors().set(TASK_URI_LIST, uris);
    entity.sensors().set(TASK_URI_LIST, uris);
    builder.put("uris", uris);

    // Docker config
    Optional<String> imageName = Optional.fromNullable(config().get(DOCKER_IMAGE_NAME));
    if (imageName.isPresent()) {
        // Docker image
        builder.put("imageName", imageName.get());
        builder.put("imageVersion", config().get(DOCKER_IMAGE_TAG));

        // Docker command or args
        String command = config().get(COMMAND);
        builder.putIfNotNull("command", command);
        List<String> args = MutableList.copyOf(config().get(ARGS));
        builder.putIfNotNull("args", args);
    } else {
        // OS name for image
        OsFamily os = entity.config().get(JcloudsLocationConfig.OS_FAMILY);
        if (os == null) {
            os = provisioningProperties.get(JcloudsLocationConfig.OS_FAMILY);
        }
        if (os == null) {
            TemplateBuilder template = provisioningProperties.get(JcloudsLocationConfig.TEMPLATE_BUILDER);
            if (template != null) {
                os = template.build().getImage().getOperatingSystem().getFamily();
            }
        }
        if (os == null) {
            os = OsFamily.UBUNTU;
        }
        imageName = Optional.of(Strings.toLowerCase(os.value()));
        builder.put("imageName", "clockercentral/" + imageName.get());

        // OS version specified in regex config
        String version = entity.config().get(JcloudsLocationConfig.OS_VERSION_REGEX);
        if (version == null) {
            version = provisioningProperties.get(JcloudsLocationConfig.OS_VERSION_REGEX);
        }
        if (version == null) {
            TemplateBuilder template = provisioningProperties.get(JcloudsLocationConfig.TEMPLATE_BUILDER);
            if (template != null) {
                version = template.build().getImage().getOperatingSystem().getVersion();
            }
        }
        if (version == null) {
            version = "latest";
        }
        builder.put("imageVersion", version);

        // Empty args
        builder.put("args", ImmutableList.of());

        // Update volume to copy root's authorized keys from the host
        volumes.put("/root/.ssh/authorized_keys", "/root/.ssh/authorized_keys");
        builder.put("volumes", Lists.newArrayList(volumes.entrySet()));
    }

    return builder.build();
}

From source file:com.google.cloud.bigquery.QueryJobConfiguration.java

@Override
com.google.api.services.bigquery.model.JobConfiguration toPb() {
    com.google.api.services.bigquery.model.JobConfiguration configurationPb = new com.google.api.services.bigquery.model.JobConfiguration();
    JobConfigurationQuery queryConfigurationPb = new JobConfigurationQuery();
    queryConfigurationPb.setQuery(query);
    if (!positionalParameters.isEmpty()) {
        List<QueryParameter> queryParametersPb = Lists.transform(positionalParameters,
                POSITIONAL_PARAMETER_TO_PB_FUNCTION);
        queryConfigurationPb.setQueryParameters(queryParametersPb);
    } else if (!namedParameters.isEmpty()) {
        List<QueryParameter> queryParametersPb = Lists.transform(namedParameters.entrySet().asList(),
                NAMED_PARAMETER_TO_PB_FUNCTION);
        queryConfigurationPb.setQueryParameters(queryParametersPb);
    }/*from w w w  . j  a  v  a2 s.co  m*/
    configurationPb.setDryRun(dryRun());
    if (allowLargeResults != null) {
        queryConfigurationPb.setAllowLargeResults(allowLargeResults);
    }
    if (createDisposition != null) {
        queryConfigurationPb.setCreateDisposition(createDisposition.toString());
    }
    if (destinationTable != null) {
        queryConfigurationPb.setDestinationTable(destinationTable.toPb());
    }
    if (defaultDataset != null) {
        queryConfigurationPb.setDefaultDataset(defaultDataset.toPb());
    }
    if (flattenResults != null) {
        queryConfigurationPb.setFlattenResults(flattenResults);
    }
    if (priority != null) {
        queryConfigurationPb.setPriority(priority.toString());
    }
    if (tableDefinitions != null) {
        queryConfigurationPb.setTableDefinitions(
                Maps.transformValues(tableDefinitions, ExternalTableDefinition.TO_EXTERNAL_DATA_FUNCTION));
    }
    if (useQueryCache != null) {
        queryConfigurationPb.setUseQueryCache(useQueryCache);
    }
    if (userDefinedFunctions != null) {
        queryConfigurationPb.setUserDefinedFunctionResources(
                Lists.transform(userDefinedFunctions, UserDefinedFunction.TO_PB_FUNCTION));
    }
    if (writeDisposition != null) {
        queryConfigurationPb.setWriteDisposition(writeDisposition.toString());
    }
    if (useLegacySql != null) {
        queryConfigurationPb.setUseLegacySql(useLegacySql);
    }
    if (maximumBillingTier != null) {
        queryConfigurationPb.setMaximumBillingTier(maximumBillingTier);
    }
    if (schemaUpdateOptions != null) {
        ImmutableList.Builder<String> schemaUpdateOptionsBuilder = new ImmutableList.Builder<>();
        for (JobInfo.SchemaUpdateOption schemaUpdateOption : schemaUpdateOptions) {
            schemaUpdateOptionsBuilder.add(schemaUpdateOption.name());
        }
        queryConfigurationPb.setSchemaUpdateOptions(schemaUpdateOptionsBuilder.build());
    }
    return configurationPb.setQuery(queryConfigurationPb);
}

From source file:org.apache.twill.yarn.YarnTwillPreparer.java

private JvmOptions saveJvmOptions(final Path targetPath) throws IOException {
    // Append runnable specific extra options.
    Map<String, String> runnableExtraOptions = Maps
            .newHashMap(Maps.transformValues(this.runnableExtraOptions, new Function<String, String>() {
                @Override/*from   w w  w  .  j  a v a 2  s.  c o  m*/
                public String apply(String options) {
                    return addClassLoaderClassName(
                            extraOptions.isEmpty() ? options : extraOptions + " " + options);
                }
            }));

    String globalOptions = addClassLoaderClassName(extraOptions);
    JvmOptions jvmOptions = new JvmOptions(globalOptions, runnableExtraOptions, debugOptions);
    if (globalOptions.isEmpty() && runnableExtraOptions.isEmpty()
            && JvmOptions.DebugOptions.NO_DEBUG.equals(debugOptions)) {
        // If no vm options, no need to localize the file.
        return jvmOptions;
    }

    LOG.debug("Creating {}", targetPath);
    try (Writer writer = Files.newBufferedWriter(targetPath, StandardCharsets.UTF_8)) {
        new Gson().toJson(new JvmOptions(globalOptions, runnableExtraOptions, debugOptions), writer);
    }
    LOG.debug("Done {}", targetPath);
    return jvmOptions;
}

From source file:org.apache.aurora.scheduler.thrift.SchedulerThriftInterface.java

private static Set<InstanceTaskConfig> buildInitialState(Map<Integer, ITaskConfig> tasks) {
    // Translate tasks into instance IDs.
    Multimap<ITaskConfig, Integer> instancesByConfig = HashMultimap.create();
    Multimaps.invertFrom(Multimaps.forMap(tasks), instancesByConfig);

    // Reduce instance IDs into contiguous ranges.
    Map<ITaskConfig, Set<Range<Integer>>> rangesByConfig = Maps.transformValues(instancesByConfig.asMap(),
            Numbers::toRanges);//from  w ww .  ja  va 2s .c  o m

    ImmutableSet.Builder<InstanceTaskConfig> builder = ImmutableSet.builder();
    for (Map.Entry<ITaskConfig, Set<Range<Integer>>> entry : rangesByConfig.entrySet()) {
        builder.add(new InstanceTaskConfig().setTask(entry.getKey().newBuilder())
                .setInstances(IRange.toBuildersSet(convertRanges(entry.getValue()))));
    }

    return builder.build();
}

From source file:org.apache.brooklyn.container.location.kubernetes.KubernetesLocation.java

protected Map<String, String> findMetadata(Entity entity, ConfigBag setup, String value) {
    Map<String, String> podMetadata = Maps.newLinkedHashMap();
    if (isDockerContainer(entity)) {
        podMetadata.put(IMMUTABLE_CONTAINER_KEY, value);
    } else {//  w  w  w  . j  a  v a2s .c o m
        podMetadata.put(SSHABLE_CONTAINER, value);
    }

    Map<String, Object> metadata = MutableMap.<String, Object>builder()
            .putAll(MutableMap.copyOf(setup.get(KubernetesPod.METADATA)))
            .putAll(MutableMap.copyOf(entity.config().get(KubernetesPod.METADATA))).putAll(podMetadata).build();
    return Maps.transformValues(metadata, Functions.toStringFunction());
}

From source file:org.apache.brooklyn.container.location.kubernetes.KubernetesLocation.java

/**
 * Sets the {@code BROOKLYN_ROOT_PASSWORD} variable in the container environment if appropriate.
 * This is (approximately) the same behaviour as the {@link DockerJcloudsLocation} used for
 * Swarm.//from  www .j a va 2s  .  c o m
 * <p>
 * Side-effects the location {@code config} to set the {@link KubernetesLocationConfig#LOGIN_USER_PASSWORD loginUser.password}
 * if one is auto-generated. Note that this injected value overrides any other settings configured for the
 * container environment.
 */
protected Map<String, String> findEnvironmentVariables(Entity entity, ConfigBag setup, String imageName) {
    String loginUser = setup.get(LOGIN_USER);
    String loginPassword = setup.get(LOGIN_USER_PASSWORD);
    Map<String, String> injections = Maps.newLinkedHashMap();

    // Check if login credentials should be injected
    Boolean injectLoginCredentials = setup.get(INJECT_LOGIN_CREDENTIAL);
    if (injectLoginCredentials == null) {
        for (String regex : IMAGE_DESCRIPTION_REGEXES_REQUIRING_INJECTED_LOGIN_CREDS) {
            if (imageName != null && imageName.matches(regex)) {
                injectLoginCredentials = true;
                break;
            }
        }
    }

    if (Boolean.TRUE.equals(injectLoginCredentials)) {
        if ((Strings.isBlank(loginUser) || "root".equals(loginUser))) {
            loginUser = "root";
            setup.configure(LOGIN_USER, loginUser);

            if (Strings.isBlank(loginPassword)) {
                loginPassword = Identifiers.makeRandomPassword(12);
                setup.configure(LOGIN_USER_PASSWORD, loginPassword);
            }

            injections.put(BROOKLYN_ROOT_PASSWORD, loginPassword);
        }
    }

    Map<String, Object> rawEnv = MutableMap.<String, Object>builder().putAll(MutableMap.copyOf(setup.get(ENV)))
            .putAll(MutableMap.copyOf(entity.config().get(DockerContainer.CONTAINER_ENVIRONMENT)))
            .putAll(injections).build();
    return Maps.transformValues(rawEnv, Functions.toStringFunction());
}

From source file:gobblin.compaction.mapreduce.MRCompactor.java

/**
 * Submit an event when completeness verification is successful
 */// www  .j a  va  2  s.c o  m
private void submitVerificationSuccessSlaEvent(Results.Result result) {
    try {
        CompactionSlaEventHelper.getEventSubmitterBuilder(result.dataset(), Optional.<Job>absent(), this.fs)
                .eventSubmitter(this.eventSubmitter)
                .eventName(CompactionSlaEventHelper.COMPLETION_VERIFICATION_SUCCESS_EVENT_NAME)
                .additionalMetadata(
                        Maps.transformValues(result.verificationContext(), Functions.toStringFunction()))
                .build().submit();
    } catch (Throwable t) {
        LOG.warn("Failed to submit verification success event:" + t, t);
    }
}

From source file:org.killbill.billing.plugin.adyen.api.AdyenPaymentPluginApi.java

private PaymentTransactionInfoPlugin getPaymentTransactionInfoPluginForHPP(
        final TransactionType transactionType, final UUID kbAccountId, final UUID kbPaymentId,
        final UUID kbTransactionId, final BigDecimal amount, final Currency currency,
        final Iterable<PluginProperty> properties, final TenantContext context)
        throws PaymentPluginApiException {
    final AdyenPaymentServiceProviderHostedPaymentPagePort hostedPaymentPagePort = adyenHppConfigurationHandler
            .getConfigurable(context.getTenantId());
    final Map<String, String> requestParameterMap = Maps
            .transformValues(PluginProperties.toStringMap(properties), new Function<String, String>() {
                @Override/* w w  w . j a v  a  2  s.  c om*/
                public String apply(final String input) {
                    // Adyen will encode parameters like merchantSig
                    return decode(input);
                }
            });
    final HppCompletedResult hppCompletedResult = hostedPaymentPagePort
            .parseAndVerifyRequestIntegrity(requestParameterMap);
    final PurchaseResult purchaseResult = new PurchaseResult(hppCompletedResult);

    final DateTime utcNow = clock.getUTCNow();
    try {
        final AdyenResponsesRecord adyenResponsesRecord = dao.addResponse(kbAccountId, kbPaymentId,
                kbTransactionId, transactionType, amount, currency, purchaseResult, utcNow,
                context.getTenantId());
        return buildPaymentTransactionInfoPlugin(adyenResponsesRecord);
    } catch (final SQLException e) {
        throw new PaymentPluginApiException("HPP payment came through, but we encountered a database error", e);
    }
}

From source file:com.google.cloud.hadoop.gcsio.GoogleCloudStorageImpl.java

/**
 * Helper for converting from a Map&lt;String, byte[]&gt; metadata map that may be in a
 * StorageObject into a Map&lt;String, String&gt; suitable for placement inside a
 * GoogleCloudStorageItemInfo.// w ww . java  2s  .  c o  m
 */
@VisibleForTesting
static Map<String, String> encodeMetadata(Map<String, byte[]> metadata) {
    return Maps.transformValues(metadata, ENCODE_METADATA_VALUES);
}

From source file:com.google.cloud.hadoop.gcsio.GoogleCloudStorageImpl.java

/**
 * Inverse function of {@link #encodeMetadata(Map)}.
 *///from w w w  .  j  a v  a 2 s.com
@VisibleForTesting
static Map<String, byte[]> decodeMetadata(Map<String, String> metadata) {
    return Maps.transformValues(metadata, DECODE_METADATA_VALUES);
}