Example usage for java.util SortedMap putAll

List of usage examples for java.util SortedMap putAll

Introduction

In this page you can find the example usage for java.util SortedMap putAll.

Prototype

void putAll(Map<? extends K, ? extends V> m);

Source Link

Document

Copies all of the mappings from the specified map to this map (optional operation).

Usage

From source file:cz.hobrasoft.pdfmu.PreferenceListComparator.java

/**
 * Sorts a map by its keys using a comparator.
 *
 * @param <K> the type of keys/*from  w  w w.  jav  a  2  s.co m*/
 * @param <V> the type of values
 * @param unsorted the original (unsorted) map
 * @param comparator a comparator on K
 * @return a sorted map
 */
public static <K, V> SortedMap<K, V> sort(Map<K, V> unsorted, Comparator<K> comparator) {
    SortedMap<K, V> sorted = new TreeMap<>(comparator);
    sorted.putAll(unsorted);
    return sorted;
}

From source file:de.cosmocode.json.JSON.java

/**
 * Creates a {@link JSONObject} backed by a {@link SortedMap}
 * containing all pairs of the provided map.
 * /* ww w. j a v a 2  s  .  co m*/
 * @param <K> the generic key type
 * @param <V> the generic value type
 * @param map the source map all pairs will be copied from
 * @return a new {@link JSONObject} containing all pairs from map, backed by a sorted map
 */
public static <K extends Comparable<K>, V> JSONObject createSortedJSONObject(Map<K, V> map) {
    final SortedMap<K, V> sorted = Maps.newTreeMap();
    sorted.putAll(map);
    return new JSONObject(sorted);
}

From source file:org.eclipse.sw360.licenseinfo.outputGenerators.OutputGenerator.java

private static <U> SortedMap<String, U> sortStringKeyedMap(Map<String, U> unsorted) {
    SortedMap<String, U> sorted = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    sorted.putAll(unsorted);
    if (sorted.size() != unsorted.size()) {
        // there were key collisions and some data was lost -> throw away the sorted map and sort by case sensitive order
        sorted = new TreeMap<>();
        sorted.putAll(unsorted);/*from  w w w .j  a  va2 s . c o  m*/
    }
    return sorted;
}

From source file:Main.java

public static <T extends Comparable, U> SortedMap<T, U> putAllObjectsSortedMap(SortedMap<T, U> targetMap,
        Map<T, U> sourceMap) {
    if (targetMap == null && sourceMap == null) {
        return new TreeMap<T, U>();
    }/*from  ww w  .j a  v  a2s. c om*/
    if (targetMap == null) {
        return new TreeMap<T, U>(sourceMap);
    }
    if (sourceMap == null) {
        return targetMap; // nothing to add
    }
    targetMap.putAll(sourceMap);
    return targetMap;
}

From source file:com.wallellen.wechat.common.util.crypto.WxCryptUtil.java

/**
 * ???(?:http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=4_3)
 *
 * @param packageParams ?/*from  w w  w .j av  a2  s. co  m*/
 * @param signKey       Key(? Key)
 * @return ??
 */
public static String createSign(Map<String, String> packageParams, String signKey) {
    SortedMap<String, String> sortedMap = new TreeMap<>();
    sortedMap.putAll(packageParams);

    List<String> keys = new ArrayList<>(packageParams.keySet());
    Collections.sort(keys);

    StringBuffer toSign = new StringBuffer();
    for (String key : keys) {
        String value = packageParams.get(key);
        if (null != value && !"".equals(value) && !"sign".equals(key) && !"key".equals(key)) {
            toSign.append(key + "=" + value + "&");
        }
    }
    toSign.append("key=" + signKey);
    String sign = DigestUtils.md5Hex(toSign.toString()).toUpperCase();
    return sign;
}

From source file:com.comcast.cmb.common.util.AuthUtil.java

private static String normalizeQueryString(Map<String, String> parameters) throws UnsupportedEncodingException {

    SortedMap<String, String> sorted = new TreeMap<String, String>();
    sorted.putAll(parameters);

    StringBuilder builder = new StringBuilder();
    Iterator<Map.Entry<String, String>> pairs = sorted.entrySet().iterator();

    while (pairs.hasNext()) {

        Map.Entry<String, String> pair = pairs.next();
        String key = pair.getKey();
        String value = pair.getValue();
        builder.append(urlEncode(key, false));
        builder.append("=");
        builder.append(urlEncode(value, false));

        if (pairs.hasNext()) {
            builder.append("&");
        }/*from   w w w.j  a  va2s  . c om*/
    }

    return builder.toString();
}

From source file:com.comcast.cmb.common.util.AuthUtil.java

private static String constructV1DataToSign(Map<String, String> parameters) {

    StringBuilder data = new StringBuilder();
    SortedMap<String, String> sortedParameters = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
    sortedParameters.putAll(parameters);

    for (String key : sortedParameters.keySet()) {
        data.append(key);//from   w  w w.jav  a2s . c  o m
        data.append(sortedParameters.get(key));
    }

    return data.toString();
}

From source file:org.jets3t.service.utils.SignatureUtils.java

/**
 * Build the canonical request string for a REST/HTTP request to a storage
 * service for the AWS Request Signature version 4.
 *
 * {@link "http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html"}
 *
 * @param uri/* w w w .j a v  a  2 s  . com*/
 * @param httpMethod
 * the request's HTTP method just prior to sending
 * @param headersMap
 * @param requestPayloadHexSha256Hash
 * hex-encoded SHA256 hash of request's payload. May be null or "" in
 * which case the default SHA256 hash of an empty string is used.
 * @return canonical request string according to AWS Request Signature version 4
 */
public static String awsV4BuildCanonicalRequestString(URI uri, String httpMethod,
        Map<String, String> headersMap, String requestPayloadHexSha256Hash) {
    StringBuilder canonicalStringBuf = new StringBuilder();

    // HTTP Request method: GET, POST etc
    canonicalStringBuf.append(httpMethod).append("\n");

    // Canonical URI: URI-encoded version of the absolute path
    String absolutePath = uri.getPath();
    if (absolutePath.length() == 0) {
        canonicalStringBuf.append("/");
    } else {
        canonicalStringBuf.append(SignatureUtils.awsV4EncodeURI(absolutePath, false));
    }
    canonicalStringBuf.append("\n");

    // Canonical query string
    String query = uri.getQuery();
    if (query == null || query.length() == 0) {
        canonicalStringBuf.append("\n");
    } else {
        // Parse and sort query parameters and values from query string
        SortedMap<String, String> sortedQueryParameters = new TreeMap<String, String>();
        for (String paramPair : query.split("&")) {
            String[] paramNameValue = paramPair.split("=");
            String name = paramNameValue[0];
            String value = "";
            if (paramNameValue.length > 1) {
                value = paramNameValue[1];
            }
            // Add parameters to sorting map, URI-encoded appropriately
            sortedQueryParameters.put(SignatureUtils.awsV4EncodeURI(name, true),
                    SignatureUtils.awsV4EncodeURI(value, true));
        }
        // Add query parameters to canonical string
        boolean isPriorParam = false;
        for (Map.Entry<String, String> entry : sortedQueryParameters.entrySet()) {
            if (isPriorParam) {
                canonicalStringBuf.append("&");
            }
            canonicalStringBuf.append(entry.getKey()).append("=").append(entry.getValue());
            isPriorParam = true;
        }
        canonicalStringBuf.append("\n");
    }

    // Canonical Headers
    SortedMap<String, String> sortedHeaders = new TreeMap<String, String>();
    sortedHeaders.putAll(headersMap);
    for (Map.Entry<String, String> entry : sortedHeaders.entrySet()) {
        canonicalStringBuf.append(entry.getKey()).append(":").append(entry.getValue()).append("\n");
    }
    canonicalStringBuf.append("\n");

    // Signed headers
    boolean isPriorSignedHeader = false;
    for (Map.Entry<String, String> entry : sortedHeaders.entrySet()) {
        if (isPriorSignedHeader) {
            canonicalStringBuf.append(";");
        }
        canonicalStringBuf.append(entry.getKey());
        isPriorSignedHeader = true;
    }
    canonicalStringBuf.append("\n");

    // Hashed Payload.
    canonicalStringBuf.append(requestPayloadHexSha256Hash);

    return canonicalStringBuf.toString();
}

From source file:edu.brown.utils.CollectionUtil.java

/**
 * @param <K>//from   w ww.  ja  v a2 s  .c  o  m
 * @param <V>
 * @param map
 * @param reverse
 * @return
 */
public static <K, V extends Comparable<V>> Map<K, V> sortByValues(Map<K, V> map, boolean reverse) {
    SortedMap<K, V> sorted = new TreeMap<K, V>(new MapValueComparator<K, V>(map, reverse));
    sorted.putAll(map);
    return (sorted);
}

From source file:com.net2plan.utils.CollectionUtils.java

/**
 * Returns a {@code SortedMap} copy of the input map according to the values.
 *
 * @param <A> Key type//from ww w .  jav  a2 s. c  o  m
 * @param <B> Value type
 * @param map Input map
 * @param orderingType Ascending or descending
 * @return {@code SortedMap}. For keys sharing the same value, entries will be ordered according to ascending order of keys.
 */
public static <A extends Comparable<A>, B extends Comparable<B>> SortedMap<A, B> sort(final Map<A, B> map,
        final Constants.OrderingType orderingType) {
    SortedMap<A, B> sortedMap = new TreeMap<A, B>(new Comparator<A>() {
        @Override
        public int compare(A key1, A key2) {
            B value1 = map.get(key1);
            B value2 = map.get(key2);
            int value = orderingType == Constants.OrderingType.ASCENDING ? value1.compareTo(value2)
                    : value2.compareTo(value1);

            if (value == 0)
                return key1.compareTo(
                        key2); /* Return 0 would merge entries, so order keys according to ascending order */
            else
                return value;
        }
    });

    sortedMap.putAll(map);
    return sortedMap;
}