Example usage for java.util SortedMap put

List of usage examples for java.util SortedMap put

Introduction

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

Prototype

V put(K key, V value);

Source Link

Document

Associates the specified value with the specified key in this map (optional operation).

Usage

From source file:com.kolich.aws.services.s3.impl.KolichS3Signer.java

/**
  * Calculate the canonical string for a REST/HTTP request to S3.
  *//*  w  ww  . j av  a 2 s . c o m*/
private static final String getS3CanonicalString(final AwsHttpRequest request) {
    // A few standard headers we extract for conveinence.
    final String contentType = CONTENT_TYPE.toLowerCase(), contentMd5 = CONTENT_MD5.toLowerCase(),
            date = DATE.toLowerCase();
    // Start with the empty string ("").
    final StringBuilder buf = new StringBuilder();
    // Next is the HTTP verb and a newline.
    buf.append(request.getMethod() + LINE_SEPARATOR_UNIX);
    // Add all interesting headers to a list, then sort them.
    // "Interesting" is defined as Content-MD5, Content-Type, Date,
    // and x-amz-... headers.
    final Map<String, String> headersMap = getHeadersAsMap(request);
    final SortedMap<String, String> interesting = Maps.newTreeMap();
    if (!headersMap.isEmpty()) {
        Iterator<Map.Entry<String, String>> it = headersMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> entry = it.next();
            final String key = entry.getKey(), value = entry.getValue();
            if (key == null) {
                continue;
            }
            final String lk = key.toLowerCase(Locale.getDefault());
            // Ignore any headers that are not interesting.
            if (lk.equals(contentType) || lk.equals(contentMd5) || lk.equals(date)
                    || lk.startsWith(AMAZON_PREFIX)) {
                interesting.put(lk, value);
            }
        }
    }
    // Remove default date timestamp if "x-amz-date" is set.
    if (interesting.containsKey(S3_ALTERNATE_DATE)) {
        interesting.put(date, "");
    }
    // These headers require that we still put a new line in after them,
    // even if they don't exist.
    if (!interesting.containsKey(contentType)) {
        interesting.put(contentType, "");
    }
    if (!interesting.containsKey(contentMd5)) {
        interesting.put(contentMd5, "");
    }
    // Add all the interesting headers
    for (Iterator<Map.Entry<String, String>> i = interesting.entrySet().iterator(); i.hasNext();) {
        final Map.Entry<String, String> entry = i.next();
        final String key = entry.getKey();
        final Object value = entry.getValue();
        if (key.startsWith(AMAZON_PREFIX)) {
            buf.append(key).append(':').append(value);
        } else {
            buf.append(value);
        }
        buf.append(LINE_SEPARATOR_UNIX);
    }
    // The CanonicalizedResource this request is working with.
    // If the request specifies a bucket using the HTTP Host header
    // (virtual hosted-style), append the bucket name preceded by a
    // "/" (e.g., "/bucketname"). For path-style requests and requests
    // that don't address a bucket, do nothing.
    if (request.getResource() != null) {
        buf.append("/" + request.getResource() + request.getURI().getRawPath());
    } else {
        buf.append(request.getURI().getRawPath());
    }
    // Amazon requires us to sort the query string parameters.
    final List<SortableBasicNameValuePair> params = sortParams(URLEncodedUtils.parse(request.getURI(), UTF_8));
    String separator = "?";
    for (final NameValuePair pair : params) {
        final String name = pair.getName(), value = pair.getValue();
        // Skip any parameters that aren't part of the
        // canonical signed string.
        if (!INTERESTING_PARAMETERS.contains(name)) {
            continue;
        }
        buf.append(separator).append(name);
        if (value != null) {
            buf.append("=").append(value);
        }
        separator = "&";
    }
    return buf.toString();
}

From source file:de.tudarmstadt.ukp.experiments.dip.wp1.documents.Step11GoldDataStatistics.java

/**
 * (1) Plain text with 4 columns: (1) the rank of the document in the list
 * (2) average agreement rate over queries (3) standard deviation of
 * agreement rate over queries. (4) average length of the document in the
 * rank.//from   ww w .ja va 2s  . com
 */
public static void statistics1(File inputDir, File outputDir) throws Exception {
    SortedMap<Integer, DescriptiveStatistics> mapDocumentRankObservedAgreement = new TreeMap<>();
    SortedMap<Integer, DescriptiveStatistics> mapDocumentRankDocLength = new TreeMap<>();

    // iterate over query containers
    for (File f : FileUtils.listFiles(inputDir, new String[] { "xml" }, false)) {
        QueryResultContainer queryResultContainer = QueryResultContainer
                .fromXML(FileUtils.readFileToString(f, "utf-8"));

        for (QueryResultContainer.SingleRankedResult rankedResult : queryResultContainer.rankedResults) {
            // add new entries
            if (!mapDocumentRankObservedAgreement.containsKey(rankedResult.rank)) {
                mapDocumentRankObservedAgreement.put(rankedResult.rank, new DescriptiveStatistics());
            }
            if (!mapDocumentRankDocLength.containsKey(rankedResult.rank)) {
                mapDocumentRankDocLength.put(rankedResult.rank, new DescriptiveStatistics());
            }

            Double observedAgreement = rankedResult.observedAgreement;

            if (observedAgreement == null) {
                System.err
                        .println("Observed agreement is null; " + f.getName() + ", " + rankedResult.clueWebID);
            } else {
                // update value
                mapDocumentRankObservedAgreement.get(rankedResult.rank).addValue(observedAgreement);
                mapDocumentRankDocLength.get(rankedResult.rank).addValue(rankedResult.plainText.length());
            }
        }
    }

    PrintWriter pw = new PrintWriter(new FileWriter(new File(outputDir, "stats1.csv")));
    for (Map.Entry<Integer, DescriptiveStatistics> entry : mapDocumentRankObservedAgreement.entrySet()) {
        pw.printf(Locale.ENGLISH, "%d\t%.4f\t%.4f\t%.4f\t%.4f%n", entry.getKey(), entry.getValue().getMean(),
                entry.getValue().getStandardDeviation(), mapDocumentRankDocLength.get(entry.getKey()).getMean(),
                mapDocumentRankDocLength.get(entry.getKey()).getStandardDeviation());
    }
    pw.close();
}

From source file:com.basho.riak.client.raw.pbc.ConversionUtil.java

/**
 * Take a link walked m/r result and make it into a WalkResult.
 *
 * This is a little bit nasty since the JSON is parsed to a Map.
 *
 * @param secondPhaseResult/* w  ww.j a  v a  2s . co m*/
 *            the contents of which *must* be a json array of {step: int, v:
 *            riakObjectMap}
 * @return a WalkResult of RiakObjects grouped by first-phase step
 * @throws IOException
 */
@SuppressWarnings({ "rawtypes" })
static WalkResult convert(MapReduceResult secondPhaseResult) throws IOException {
    final SortedMap<Integer, Collection<IRiakObject>> steps = new TreeMap<Integer, Collection<IRiakObject>>();

    try {
        Collection<Map> results = secondPhaseResult.getResult(Map.class);
        for (Map o : results) {
            final int step = Integer.parseInt((String) o.get("step"));
            Collection<IRiakObject> stepAccumulator = steps.get(step);

            if (stepAccumulator == null) {
                stepAccumulator = new ArrayList<IRiakObject>();
                steps.put(step, stepAccumulator);
            }

            final Map data = (Map) o.get("v");

            stepAccumulator.add(mapToRiakObject(data));
        }
    } catch (ConversionException e) {
        throw new IOException(e.getMessage());
    }
    // create a result instance
    return new WalkResult() {
        public Iterator<Collection<IRiakObject>> iterator() {
            return new UnmodifiableIterator<Collection<IRiakObject>>(steps.values().iterator());
        }
    };
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.feature.coreference.CoreferenceFeatures.java

/**
 * Returns a sorted map of [sentencePos, list[coreferenceLink]] for the given chain.
 * The keys are not continuous, only those that have coreference link to the given chain
 * are stored//  w  w  w  .j  a v a 2 s . c  o  m
 *
 * @param chain chain
 * @param jCas  jcas
 * @return map
 */
private static SortedMap<Integer, List<CoreferenceLink>> extractSentencesAndLinksFromChain(
        List<CoreferenceLink> chain, JCas jCas) {
    SortedMap<Integer, List<CoreferenceLink>> result = new TreeMap<>();

    // iterate over sentences
    List<Sentence> sentences = new ArrayList<>(JCasUtil.select(jCas, Sentence.class));
    for (int sentenceNo = 0; sentenceNo < sentences.size(); sentenceNo++) {
        Sentence sentence = sentences.get(sentenceNo);

        for (CoreferenceLink link : chain) {
            // is there a link in a sentence?
            if (link.getBegin() >= sentence.getBegin() && link.getEnd() <= sentence.getEnd()) {
                // put it to the map
                if (!result.containsKey(sentenceNo)) {
                    result.put(sentenceNo, new ArrayList<CoreferenceLink>());
                }
                result.get(sentenceNo).add(link);
            }
        }
    }

    return result;
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.evaluation.F1ScoreTableAggregator.java

public static void evaluatePredictionsFoldersFullCV(File masterFolder) throws Exception {
    SortedMap<String, List<String>> featureSetsResults = new TreeMap<>();

    File[] foldersFeatureSets = masterFolder.listFiles(EvalHelper.DIRECTORY_FILTER);

    for (File folderFeatureSet : foldersFeatureSets) {
        String[] split = folderFeatureSet.getName().split("_");
        String featureSet = split[0];
        String paramE = split[1];
        String paramT = split[2];

        if ("e0".equals(paramE) && "t1".equals(paramT)) {

            Map<String, File> foldersData = EvalHelper.listSubFoldersAndRemoveUUID(folderFeatureSet);
            for (Map.Entry<String, File> folderData : foldersData.entrySet()) {

                String data = folderData.getKey();

                if (data.contains("ArgumentSequenceLabelingCV")) {
                    File resultSummary = new File(folderData.getValue(), "resultSummary.txt");

                    List<String> values = extractValues(resultSummary);

                    featureSetsResults.put(featureSet, values);
                }/* www. j av a2  s. co  m*/
            }
        }
    }

    // print results
    int rows = featureSetsResults.values().iterator().next().size();
    for (String featureSet : featureSetsResults.keySet()) {
        System.out.printf("%s\t", featureSet);
    }
    System.out.println();
    for (int i = 0; i < rows; i++) {
        for (List<String> result : featureSetsResults.values()) {
            System.out.printf("%s\t", result.get(i));
        }
        System.out.println();
    }
}

From source file:fr.ribesg.bukkit.ncore.updater.FileDescription.java

public static SortedMap<String, FileDescription> parse(final String jsonString) {
    final SortedMap<String, FileDescription> result = new TreeMap<>(new Comparator<String>() {

        @Override// w ww  .  j a va 2s .c  om
        public int compare(final String a, final String b) {
            return -a.compareTo(b);
        }
    });
    final JSONArray array = (JSONArray) JSONValue.parse(jsonString);
    for (final Object o : array.toArray()) {
        final JSONObject object = (JSONObject) o;
        final String fileName = (String) object.get(FILENAME_KEY);
        final String version = VersionUtil.getVersion((String) object.get(VERSION_KEY));
        final String bukkitVersion = (String) object.get(BUKKIT_VERSION_KEY);
        final String type = (String) object.get(TYPE_KEY);
        final String link = (String) object.get(DOWNLOAD_URL_KEY);
        final FileDescription fileDescription = new FileDescription(fileName, version, link, type,
                bukkitVersion);
        if (VersionUtil.isRelease(version)) {
            result.put(version, fileDescription);
        }
    }
    return result;
}

From source file:jef.tools.collection.CollectionUtils.java

/**
 * ?MapMapKey?//w  w w  . j a v a  2s  . com
 * 
 * @param array
 *            
 * @param keyExtractor
 *            ???
 * @param comp
 *            
 * @return ??????Map?key?
 *         {@linkplain #group(Collection, Function)}
 */
@SuppressWarnings("unchecked")
public static <K, V> SortedMap<K, V> groupToSortedMap(V[] array, Function<V, K> keyExtractor,
        Comparator<K> comp) {
    if (array == null || array.length == 0)
        return EMPTY_SORTEDMAP;
    SortedMap<K, V> result = new TreeMap<K, V>(comp);
    for (V value : array) {
        K key = keyExtractor.apply(value);
        result.put(key, value);
    }
    return result;
}

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. ja v  a2 s  . c o  m*/
 * @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:jef.tools.collection.CollectionUtils.java

/**
 * ??MapMapKey?/* w ww.j av  a 2 s. c  o m*/
 * 
 * @param collection
 *            ?
 * @param keyExtractor
 *            ???
 * @param comp
 *            
 * @return ??????Map?key?
 *         {@linkplain #group(Collection, Function)}
 */
@SuppressWarnings("unchecked")
public static <K, V> SortedMap<K, V> groupToSortedMap(Collection<V> collection, Function<V, K> keyExtractor,
        Comparator<K> comp) {
    if (collection == null || collection.size() == 0)
        return EMPTY_SORTEDMAP;
    SortedMap<K, V> result = new TreeMap<K, V>(comp);
    for (V value : collection) {
        K key = keyExtractor.apply(value);
        result.put(key, value);
    }
    return result;
}

From source file:com.opengamma.web.server.WebView.java

private static SortedMap<Integer, Long> processViewportData(final Map<String, Object> viewportData) {
    final SortedMap<Integer, Long> result = new TreeMap<Integer, Long>();
    if (viewportData.isEmpty()) {
        return result;
    }/*  w  w  w.  j  a  v a  2  s.c  o m*/
    final Object[] ids = (Object[]) viewportData.get("rowIds");
    final Object[] lastTimes = (Object[]) viewportData.get("lastTimestamps");
    for (int i = 0; i < ids.length; i++) {
        if (ids[i] instanceof Number) {
            final long jsRowId = (Long) ids[i];
            final int rowId = (int) jsRowId;
            if (lastTimes[i] != null) {
                final Long lastTime = (Long) lastTimes[i];
                result.put(rowId, lastTime);
            } else {
                result.put(rowId, null);
            }
        } else {
            throw new OpenGammaRuntimeException("Unexpected type of webId: " + ids[i]);
        }
    }
    return result;
}