Example usage for com.google.common.collect ImmutableMultimap builder

List of usage examples for com.google.common.collect ImmutableMultimap builder

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableMultimap builder.

Prototype

public static <K, V> Builder<K, V> builder() 

Source Link

Document

Returns a new builder.

Usage

From source file:org.jclouds.cloudwatch.binders.MetricDataBinder.java

/**
 * {@inheritDoc}/*w  w  w .  ja  va2 s .  c o m*/
 */
@SuppressWarnings("unchecked")
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
    Iterable<MetricDatum> metrics = (Iterable<MetricDatum>) checkNotNull(input, "metrics must be set!");

    ImmutableMultimap.Builder<String, String> formParameters = ImmutableMultimap.builder();
    int metricDatumIndex = 1;

    for (MetricDatum metricDatum : metrics) {
        int dimensionIndex = 1;

        for (Dimension dimension : metricDatum.getDimensions()) {
            formParameters.put(
                    "MetricData.member." + metricDatumIndex + ".Dimensions.member." + dimensionIndex + ".Name",
                    dimension.getName());
            formParameters.put(
                    "MetricData.member." + metricDatumIndex + ".Dimensions.member." + dimensionIndex + ".Value",
                    dimension.getValue());
            dimensionIndex++;
        }

        formParameters.put("MetricData.member." + metricDatumIndex + ".MetricName",
                metricDatum.getMetricName());

        if (metricDatum.getStatisticValues().isPresent()) {
            StatisticValues statisticValues = metricDatum.getStatisticValues().get();

            formParameters.put("MetricData.member." + metricDatumIndex + ".StatisticValues.Maximum",
                    String.valueOf(statisticValues.getMaximum()));
            formParameters.put("MetricData.member." + metricDatumIndex + ".StatisticValues.Minimum",
                    String.valueOf(statisticValues.getMinimum()));
            formParameters.put("MetricData.member." + metricDatumIndex + ".StatisticValues.SampleCount",
                    String.valueOf(statisticValues.getSampleCount()));
            formParameters.put("MetricData.member." + metricDatumIndex + ".StatisticValues.Sum",
                    String.valueOf(statisticValues.getSum()));
        }

        if (metricDatum.getTimestamp().isPresent()) {
            formParameters.put("MetricData.member." + metricDatumIndex + ".Timestamp",
                    dateService.iso8601SecondsDateFormat(metricDatum.getTimestamp().get()));
        }

        formParameters.put("MetricData.member." + metricDatumIndex + ".Unit",
                String.valueOf(metricDatum.getUnit()));

        if (metricDatum.getValue().isPresent()) {
            formParameters.put("MetricData.member." + metricDatumIndex + ".Value",
                    String.valueOf(metricDatum.getValue().get()));
        }

        metricDatumIndex++;
    }

    return (R) request.toBuilder().replaceFormParams(formParameters.build()).build();
}

From source file:org.jclouds.gogrid.binders.BindVirtualIpPortPairToQueryParams.java

@SuppressWarnings("unchecked")
@Override/*from   w  ww  .  java2 s .com*/
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
    checkArgument(checkNotNull(input, "input is null") instanceof IpPortPair,
            "this binder is only valid for a IpPortPair argument");

    IpPortPair ipPortPair = (IpPortPair) input;

    checkNotNull(ipPortPair.getIp(), "There must be an IP address defined");
    checkNotNull(ipPortPair.getIp().getIp(), "There must be an IP address defined in Ip object");
    checkState(ipPortPair.getPort() > 0, "The port number must be a positive integer");

    ImmutableMultimap.Builder<String, String> builder = ImmutableMultimap.<String, String>builder();

    builder.put(VIRTUAL_IP_KEY + "ip", ipPortPair.getIp().getIp());
    builder.put(VIRTUAL_IP_KEY + "port", String.valueOf(ipPortPair.getPort()));

    return (R) request.toBuilder().replaceQueryParams(builder.build()).build();
}

From source file:org.jclouds.glesys.options.CreateServerOptions.java

@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, Object> postParams) {
    checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest,
            "this binder is only valid for GeneratedHttpRequests!");
    GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request;
    ImmutableMultimap.Builder<String, String> formParams = ImmutableMultimap.builder();
    for (Map.Entry<String, Object> entry : postParams.entrySet())
        formParams.put(entry.getKey(), (String) entry.getValue());
    ServerSpec serverSpec = ServerSpec.class
            .cast(find(gRequest.getInvocation().getArgs(), instanceOf(ServerSpec.class)));
    formParams.put("datacenter", serverSpec.getDatacenter());
    formParams.put("platform", serverSpec.getPlatform());
    formParams.put("templatename", serverSpec.getTemplateName());
    formParams.put("disksize", serverSpec.getDiskSizeGB() + "");
    formParams.put("memorysize", serverSpec.getMemorySizeMB() + "");
    formParams.put("cpucores", serverSpec.getCpuCores() + "");
    formParams.put("transfer", serverSpec.getTransferGB() + "");
    if (ip != null)
        formParams.put("ip", ip);
    if (description != null)
        formParams.put("description", description);

    request.setPayload(new UrlEncodedFormPayload(formParams.build()));
    return request;
}

From source file:org.zalando.logbook.QueryParameters.java

public static QueryParameters parse(@Nullable final String queryString) {
    if (queryString == null || queryString.isEmpty()) {
        return EMPTY;
    }//w ww.j a v  a 2  s. c om

    final ImmutableMultimap.Builder<String, String> entries = ImmutableMultimap.builder();

    for (final Map.Entry<String, String> entry : splitEntries(queryString)) {
        entries.put(entry.getKey(), entry.getValue());
    }

    return new QueryParameters(entries.build());
}

From source file:org.jclouds.cloudwatch.binders.GetMetricStatisticsBinder.java

@SuppressWarnings("unchecked")
@Override/*from   w w  w . j a  va 2s.  co  m*/
public <R extends HttpRequest> R bindToRequest(R request, Object payload) {
    GetMetricStatistics getRequest = GetMetricStatistics.class
            .cast(checkNotNull(payload, "GetMetricStatistics must be set!"));
    int dimensionIndex = 1;
    int statisticIndex = 1;
    ImmutableMultimap.Builder<String, String> formParameters = ImmutableMultimap.builder();

    for (Dimension dimension : getRequest.getDimensions()) {
        formParameters.put("Dimensions.member." + dimensionIndex + ".Name", dimension.getName());
        formParameters.put("Dimensions.member." + dimensionIndex + ".Value", dimension.getValue());
        dimensionIndex++;
    }

    if (getRequest.getEndTime().isPresent()) {
        formParameters.put("EndTime", dateService.iso8601SecondsDateFormat(getRequest.getEndTime().get()));
    }
    formParameters.put("MetricName", getRequest.getMetricName());
    formParameters.put("Namespace", getRequest.getNamespace());
    formParameters.put("Period", Integer.toString(getRequest.getPeriod()));
    if (getRequest.getStartTime().isPresent()) {
        formParameters.put("StartTime", dateService.iso8601SecondsDateFormat(getRequest.getStartTime().get()));
    }

    for (Statistics statistic : getRequest.getStatistics()) {
        formParameters.put("Statistics.member." + statisticIndex, statistic.toString());
        statisticIndex++;
    }

    if (getRequest.getUnit().isPresent()) {
        formParameters.put("Unit", getRequest.getUnit().get().toString());
    }

    return (R) request.toBuilder().replaceFormParams(formParameters.build()).build();
}

From source file:com.isotrol.impe3.core.impl.HeadersFactory.java

/**
 * Adds the parameters from a JAX-RS headers object.
 * @param headers HTTP Headers./*ww w . java  2s  . co  m*/
 * @return The request headers.
 */
public static Headers of(HttpHeaders headers) {
    Preconditions.checkNotNull(headers, "The request headers object cannot be null.");
    final ImmutableMultimap.Builder<CaseIgnoringString, String> builder = ImmutableMultimap.builder();
    for (Entry<String, List<String>> entry : headers.getRequestHeaders().entrySet()) {
        List<String> values = entry.getValue();
        if (values != null) {
            builder.putAll(CaseIgnoringString.valueOf(entry.getKey()), values);
        }
    }
    return new Immutable(builder.build());
}

From source file:io.airlift.event.client.NestedDummyEventClass.java

@EventField
public Multimap<String, String> getNamedStringList() {
    ImmutableMultimap.Builder<String, String> builder = ImmutableMultimap.builder();
    for (String s : strings) {
        builder.putAll(s, s, s, s); // three copies
    }// ww  w.  ja  v  a  2s .c  o  m
    return builder.build();
}

From source file:com.outerspacecat.icalendar.VCalendar.java

/**
 * Creates a new calendar./*from   w w w .  j  a v  a 2 s .c  o  m*/
 * 
 * @param properties the calendar properties. Must be non {@code null}, all
 *        elements must be non {@code null}, may be empty.
 * @param components the calendar components. Must be non {@code null}, all
 *        elements must be non {@code null}, may be empty.
 */
public VCalendar(final Iterable<Property> properties, final Iterable<Component> components) {
    Preconditions.checkNotNull(properties, "properties required");
    Preconditions.checkNotNull(components, "components required");

    ImmutableMultimap.Builder<String, Property> propBuilder = new ImmutableMultimap.Builder<String, Property>();
    for (Property prop : properties) {
        Preconditions.checkNotNull(prop, "properties must each be non null");
        propBuilder.put(prop.getName().getName(), prop);
    }
    this.properties = propBuilder.build();

    ImmutableMultimap.Builder<String, Component> compBuilder = new ImmutableMultimap.Builder<String, Component>();
    for (Component comp : components) {
        Preconditions.checkNotNull(comp, "components must each be non null");
        compBuilder.put(comp.getName(), comp);
    }
    this.components = compBuilder.build();
}

From source file:com.facebook.presto.sql.planner.assertions.ExpressionAliases.java

public void updateAssignments(Map<Symbol, Expression> assignments) {
    ImmutableMultimap.Builder<String, Expression> mapUpdate = ImmutableMultimap.builder();
    for (Map.Entry<Symbol, Expression> assignment : assignments.entrySet()) {
        for (String alias : map.keys()) {
            if (map.get(alias).contains(assignment.getKey().toSymbolReference())) {
                mapUpdate.put(alias, assignment.getValue());
            }//from   www  . j  a va2s .com
        }
    }
    map.putAll(mapUpdate.build());
}

From source file:com.isotrol.impe3.core.component.DirectInjectors.java

private DirectInjectors(final Class<T> type, final Predicate<Class<?>> valid, Iterable<Method> methods) {
    super(type);/*  w w  w .  j  a v a  2  s  .com*/
    final ImmutableMultimap.Builder<Class<?>, DirectInjector> builder = ImmutableMultimap.builder();
    for (Method m : methods) {
        final DirectInjector i = new DirectInjector(m, valid);
        Class<?> pt = i.getParameterType();
        if (Listing.class.isAssignableFrom(pt)) {
            pt = Listing.class;
        }
        builder.put(pt, i);
    }
    this.injectors = builder.build();
}