Example usage for com.google.common.collect Multimaps forMap

List of usage examples for com.google.common.collect Multimaps forMap

Introduction

In this page you can find the example usage for com.google.common.collect Multimaps forMap.

Prototype

public static <K, V> SetMultimap<K, V> forMap(Map<K, V> map) 

Source Link

Document

Returns a multimap view of the specified map.

Usage

From source file:org.jclouds.azureblob.binders.BindAzureContentMetadataToRequest.java

@SuppressWarnings("unchecked")
@Override//from w w  w  .  j  a v  a2 s .c o  m
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
    checkArgument(checkNotNull(input, "input") instanceof ContentMetadata,
            "this binder is only valid for ContentMetadata");
    checkNotNull(request, "request");
    ContentMetadata contentMetadata = (ContentMetadata) input;

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

    String cacheControl = contentMetadata.getCacheControl();
    if (cacheControl != null) {
        headers.put(AzureStorageHeaders.CACHE_CONTROL, cacheControl);
    }

    String contentDisposition = contentMetadata.getContentDisposition();
    if (contentDisposition != null) {
        headers.put(AzureStorageHeaders.CONTENT_DISPOSITION, contentDisposition);
    }

    String contentEncoding = contentMetadata.getContentEncoding();
    if (contentEncoding != null) {
        headers.put(AzureStorageHeaders.CONTENT_ENCODING, contentEncoding);
    }

    String contentLanguage = contentMetadata.getContentLanguage();
    if (contentLanguage != null) {
        headers.put(AzureStorageHeaders.CONTENT_LANGUAGE, contentLanguage);
    }

    String contentType = contentMetadata.getContentType();
    if (contentType != null) {
        headers.put(AzureStorageHeaders.CONTENT_TYPE, contentType);
    }

    return (R) request.toBuilder().replaceHeaders(Multimaps.forMap(headers.build())).build();
}

From source file:org.jclouds.aws.binders.BindTableToIndexedFormParams.java

@SuppressWarnings("unchecked")
@Override//from  ww w  .  j ava2 s .  co  m
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
    if (checkNotNull(input, "input") instanceof Map) {
        Builder<Object, Object, Object> builder = ImmutableTable.builder();
        int index = 1;
        for (Map.Entry<?, ?> entry : ((Map<?, ?>) input).entrySet())
            builder.put(index++, entry.getKey(), entry.getValue());
        input = builder.build();
    }
    checkArgument(checkNotNull(input, "input") instanceof Table, "this binder is only valid for Table");
    Table<?, ?, ?> table = Table.class.cast(input);

    ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
    int amazonOneBasedIndex = 1; // according to docs, counters must start
                                 // with 1
    for (Cell<?, ?, ?> cell : table.cellSet()) {
        // not null by contract
        builder.put(format(rowPattern, amazonOneBasedIndex), cell.getRowKey().toString());
        builder.put(format(columnPattern, amazonOneBasedIndex), cell.getColumnKey().toString());
        builder.put(format(valuePattern, amazonOneBasedIndex), cell.getValue().toString());

        amazonOneBasedIndex++;
    }
    Multimap<String, String> forms = Multimaps.forMap(builder.build());
    return forms.size() == 0 ? request : (R) request.toBuilder().replaceFormParams(forms).build();
}

From source file:org.jclouds.azureblob.binders.BindAzureBlobMetadataToRequest.java

@SuppressWarnings("unchecked")
@Override/* w  w w. j  ava  2  s.c  o m*/
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
    checkArgument(checkNotNull(input, "input") instanceof AzureBlob,
            "this binder is only valid for AzureBlobs!");
    checkNotNull(request, "request");
    AzureBlob blob = AzureBlob.class.cast(input);

    checkArgument(blob.getPayload().getContentMetadata().getContentLength() != null
            && blob.getPayload().getContentMetadata().getContentLength() >= 0, "size must be set");

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

    headers.put("x-ms-blob-type", blob.getProperties().getType().toString());

    switch (blob.getProperties().getType()) {
    case PAGE_BLOB:
        headers.put(HttpHeaders.CONTENT_LENGTH, "0");
        headers.put("x-ms-blob-content-length",
                blob.getPayload().getContentMetadata().getContentLength().toString());
        break;
    case BLOCK_BLOB:
        checkArgument(checkNotNull(blob.getPayload().getContentMetadata().getContentLength(),
                "blob.getContentLength()") <= 64l * 1024 * 1024, "maximum size for put Blob is 64MB");
        break;
    }
    request = (R) request.toBuilder().replaceHeaders(Multimaps.forMap(headers.build())).build();

    return blobBinder.bindToRequest(request, azureBlob2Blob.apply(blob));
}

From source file:brooklyn.networking.cloudstack.HttpUtil.java

public static HttpToolResponse httpGet(HttpClient httpClient, URI uri, Map<String, String> headers) {
    return httpGet(httpClient, uri, Multimaps.forMap(headers));
}

From source file:eu.lp0.cursus.scoring.scores.impl.GenericOverallPositionData.java

@Override
protected LinkedListMultimap<Integer, Pilot> calculateOverallPositionsWithOrder() {
    // Invert race points with ordered lists of pilots
    Comparator<Pilot> racePlacings = new PilotRacePlacingComparator<T>(scores, placingMethod);
    Comparator<Pilot> fallbackOrdering = new PilotRaceNumberComparator();
    TreeMultimap<Integer, Pilot> invOverallPoints = TreeMultimap.create(Ordering.natural(),
            Ordering.from(racePlacings).compound(fallbackOrdering));
    Multimaps.invertFrom(Multimaps.forMap(scores.getOverallPoints()), invOverallPoints);

    // Calculate overall positions
    LinkedListMultimap<Integer, Pilot> overallPositions = LinkedListMultimap.create();
    List<Pilot> collectedPilots = new ArrayList<Pilot>(scores.getPilots().size());
    LinkedList<SortedSet<Pilot>> pilotPointsOrdering = new LinkedList<SortedSet<Pilot>>();
    int position = 1;

    if (allSimulatedToEnd) {
        final Map<Pilot, ? extends Set<Race>> simulatedPilotPoints = scores.getSimulatedPilotPoints();
        Predicate<Pilot> allSimulatedPilot = new Predicate<Pilot>() {
            private final int raceCount = scores.getRaces().size();

            @Override//from   w w w.  j  a v a 2s. c om
            public boolean apply(Pilot input) {
                return simulatedPilotPoints.get(input).size() == raceCount;
            }
        };

        for (Integer points : invOverallPoints.keySet()) {
            SortedSet<Pilot> pilots = Sets.filter(invOverallPoints.get(points),
                    Predicates.not(allSimulatedPilot));
            if (!pilots.isEmpty()) {
                pilotPointsOrdering.add(pilots);
            }
        }
        for (Integer points : invOverallPoints.keySet()) {
            SortedSet<Pilot> pilots = Sets.filter(invOverallPoints.get(points), allSimulatedPilot);
            if (!pilots.isEmpty()) {
                pilotPointsOrdering.add(pilots);
            }
        }
    } else {
        for (Integer points : invOverallPoints.keySet()) {
            pilotPointsOrdering.add(invOverallPoints.get(points));
        }
    }

    for (SortedSet<Pilot> pilots : pilotPointsOrdering) {
        switch (equalPositioning) {
        case ALWAYS:
            // Always put pilots with the same points in the same position
            overallPositions.putAll(position, pilots);
            position += pilots.size();
            break;

        case IF_REQUIRED:
            // Try to put pilots with the same points in separate positions
            PeekingIterator<Pilot> it = Iterators.peekingIterator(pilots.iterator());
            while (it.hasNext()) {
                Pilot pilot = it.next();
                collectedPilots.add(pilot);

                // If this pilot compares equally with the next pilot, add them too
                while (it.hasNext() && racePlacings.compare(it.peek(), pilot) == 0) {
                    collectedPilots.add(it.next());
                }

                // Sort them by an arbitrary order
                Collections.sort(collectedPilots, fallbackOrdering);

                // Add them all to this position
                overallPositions.putAll(position, collectedPilots);
                position += collectedPilots.size();

                collectedPilots.clear();
            }
            break;
        }
    }

    return overallPositions;
}

From source file:org.jclouds.azureblob.binders.BindAzureBlobMetadataToMultipartRequest.java

@SuppressWarnings("unchecked")
@Override/*  w w  w  .  ja  v a 2  s. c  om*/
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
    checkArgument(checkNotNull(input, "input") instanceof AzureBlob,
            "this binder is only valid for AzureBlobs!");
    checkNotNull(request, "request");
    AzureBlob blob = AzureBlob.class.cast(input);

    checkArgument(blob.getPayload().getContentMetadata().getContentLength() != null
            && blob.getPayload().getContentMetadata().getContentLength() >= 0, "size must be set");

    // bind BlockList-specific headers
    ImmutableMap.Builder<String, String> headers = ImmutableMap.builder();
    ContentMetadata contentMetadata = blob.getProperties().getContentMetadata();
    String cacheControl = contentMetadata.getCacheControl();
    if (cacheControl != null) {
        headers.put("x-ms-blob-cache-control", cacheControl);
    }
    String contentDisposition = contentMetadata.getContentDisposition();
    if (contentDisposition != null) {
        headers.put("x-ms-blob-content-disposition", contentDisposition);
    }
    String contentEncoding = contentMetadata.getContentEncoding();
    if (contentEncoding != null) {
        headers.put("x-ms-blob-content-encoding", contentEncoding);
    }
    String contentLanguage = contentMetadata.getContentLanguage();
    if (contentLanguage != null) {
        headers.put("x-ms-blob-content-language", contentLanguage);
    }
    String contentType = contentMetadata.getContentType();
    if (contentType != null) {
        headers.put("x-ms-blob-content-type", contentType);
    }
    request = (R) request.toBuilder().replaceHeaders(Multimaps.forMap(headers.build())).build();

    return blobBinder.bindToRequest(request, azureBlob2Blob.apply(blob));
}

From source file:org.jclouds.atmos.filters.SignRequest.java

@Override
public HttpRequest filter(HttpRequest request) throws HttpException {
    Builder<String, String> builder = ImmutableMap.builder();
    builder.put(AtmosHeaders.UID, creds.get().identity);
    String date = timeStampProvider.get();
    builder.put(HttpHeaders.DATE, date);
    if (request.getHeaders().containsKey(AtmosHeaders.DATE))
        builder.put(AtmosHeaders.DATE, date);
    request = request.toBuilder().replaceHeaders(Multimaps.forMap(builder.build())).build();
    String signature = calculateSignature(createStringToSign(request));
    request = request.toBuilder().replaceHeader(AtmosHeaders.SIGNATURE, signature).build();
    utils.logRequest(signatureLog, request, "<<");
    return request;
}

From source file:eu.lp0.cursus.scoring.scores.impl.GenericRacePositionsData.java

@Override
protected LinkedListMultimap<Integer, Pilot> calculateRacePositionsWithOrder(Race race) {
    // Create a lap order list containing all pilots
    List<Pilot> lapOrder = new ArrayList<Pilot>(scores.getPilots().size());
    lapOrder.addAll(scores.getLapOrder(race));
    Set<Pilot> pilotsWithLaps = ImmutableSet.copyOf(lapOrder);
    SortedSet<Pilot> pilotsWithoutLaps = new TreeSet<Pilot>(new PilotRaceNumberComparator());
    pilotsWithoutLaps.addAll(Sets.difference(scores.getPilots(), pilotsWithLaps));
    lapOrder.addAll(pilotsWithoutLaps);/*ww w  .j av a 2 s . com*/

    // Add penalties to race points
    Map<Pilot, Integer> racePoints = scores.getRacePoints(race);
    Map<Pilot, Integer> racePenalties = scores.getRacePenalties(race);
    Map<Pilot, Integer> racePointsWithPenalties = new HashMap<Pilot, Integer>(scores.getPilots().size() * 2);
    for (Pilot pilot : scores.getPilots()) {
        racePointsWithPenalties.put(pilot, racePoints.get(pilot) + racePenalties.get(pilot));
    }

    // Invert race points with ordered lists of pilots
    TreeMultimap<Integer, Pilot> invRacePoints = TreeMultimap.create(Ordering.natural(),
            Ordering.explicit(lapOrder));
    Multimaps.invertFrom(Multimaps.forMap(racePointsWithPenalties), invRacePoints);

    // Calculate race positions
    LinkedListMultimap<Integer, Pilot> racePositions = LinkedListMultimap.create();
    LinkedList<SortedSet<Pilot>> pilotPointsOrdering = new LinkedList<SortedSet<Pilot>>();
    int position = 1;

    if (simulatedToEnd) {
        Set<Pilot> simulatedRacePoints = scores.getSimulatedRacePoints(race);
        for (Integer points : invRacePoints.keySet()) {
            SortedSet<Pilot> pilots = Sets.filter(invRacePoints.get(points),
                    Predicates.not(Predicates.in(simulatedRacePoints)));
            if (!pilots.isEmpty()) {
                pilotPointsOrdering.add(pilots);
            }
        }
        for (Integer points : invRacePoints.keySet()) {
            SortedSet<Pilot> pilots = Sets.filter(invRacePoints.get(points),
                    Predicates.in(simulatedRacePoints));
            if (!pilots.isEmpty()) {
                pilotPointsOrdering.add(pilots);
            }
        }
    } else {
        for (Integer points : invRacePoints.keySet()) {
            pilotPointsOrdering.add(invRacePoints.get(points));
        }
    }

    for (SortedSet<Pilot> pilots : pilotPointsOrdering) {
        switch (equalPositioning) {
        case ALWAYS:
            // Always put pilots with the same points in the same position
            racePositions.putAll(position, pilots);
            position += pilots.size();
            break;

        case WITHOUT_LAPS:
            // Add everyone with laps (by removing pilots without laps from the set) in separate positions
            for (Pilot pilot : Sets.difference(pilots, pilotsWithoutLaps)) {
                racePositions.put(position, pilot);
                position++;
            }

            // Add everyone without laps (by removing pilots with laps from the set) in the same position
            Set<Pilot> pilots2 = Sets.difference(pilots, pilotsWithLaps);
            racePositions.putAll(position, pilots2);
            position += pilots2.size();
            break;

        case NEVER:
            // Always put pilots with the same points in separate positions
            for (Pilot pilot : pilots) {
                racePositions.put(position, pilot);
                position++;
            }
            break;
        }
    }

    return racePositions;
}

From source file:org.jclouds.aws.ec2.options.AWSDescribeImagesOptions.java

/**
 * @see #filters(Multimap)//www.jav  a2 s . com
 */
public AWSDescribeImagesOptions filters(Map<String, String> filters) {
    return filters(Multimaps.forMap(checkNotNull(filters, "filters")));
}

From source file:org.jclouds.grandcloud.storage.v1.filters.SignRequest.java

@Override
public HttpRequest filter(HttpRequest request) throws HttpException {
    Builder<String, String> builder = ImmutableMap.builder();
    String date = timeStampProvider.get();
    builder.put(HttpHeaders.DATE, date);
    if (request.getHeaders().containsKey(GrandCloudHeaders.DATE))
        builder.put(GrandCloudHeaders.DATE, date);
    request = request.toBuilder().replaceHeaders(Multimaps.forMap(builder.build())).build();

    String stringToSign = createStringToSign(request);

    String signature = calculateSignature(stringToSign);
    builder.put(HttpHeaders.AUTHORIZATION, "SNDA " + creds.get().identity + ":" + signature);
    request = request.toBuilder().replaceHeaders(Multimaps.forMap(builder.build())).build();
    utils.logRequest(signatureLog, request, "<<");
    return request;
}