package com.indeed.proctor.service;

import com.google.common.base.Predicates;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

/**
 * Stores the query parameters for a /groups/identify request.
 *
 * Parses out the special query parameters like ctx.country and id.USER and stores them in maps.
 * Also parses the comma-separated test parameter into a List.
 *
 * This does NO conversion of types. Everything stays a string just as we got it from the request
 * because at this point we still don't know the intended types of context and id variables.
 */
public class RawParameters {
    final private Map<String, String> context;
    final private Map<String, String> identifiers;
    final private List<String> test;

    public RawParameters(Map<String, String> context, Map<String, String> identifiers, List<String> test) {
        this.context = context;
        this.identifiers = identifiers;
        this.test = test;
    }

    /*public RawParameters(final Map<String, String> queryParams) {
        context = Maps.newHashMap();
        parseDotParameters(queryParams, "ctx", context);
        identifiers = Maps.newHashMap();
        parseDotParameters(queryParams, "id", identifiers);

        // Parse the comma-separated tests into a list.
        if (queryParams.containsKey("test")) {
            test = Lists.newArrayList(Splitter.on(',').trimResults().omitEmptyStrings().split(queryParams.get("test")));
        } else {
            test = null;
        }
    }*/

    /**
     * Takes all the query parameters, finds the ones that start with prefix and a period, strips it out, and places
     * the key value pair into destination.
     */
    private void parseDotParameters(
            final Map<String, String> queryParams, final String prefix, final Map<String, String> destination) {

        // Regex to match beginning of input, prefix, and period.
        for (final Map.Entry<String, String> e : Maps.filterKeys(
                queryParams, Predicates.containsPattern("\\A" + prefix + "\\.")).entrySet()) {
            // Get rid of the prefix and period character, which are always at the beginning of the string.
            final String key = e.getKey().substring(prefix.length() + 1);
            destination.put(key, e.getValue());
        }
    }

    public Map<String, String> getContext() {
        return context;
    }

    public Map<String, String> getIdentifiers() {
        return identifiers;
    }

    /**
     * Returns a list of test names to filter by.
     *
     * Returns an empty list if the query parameter was present but empty (ex: "?test=")
     * Returns null if the query parameter was absent.
     */
    public List<String> getTest() {
        return test;
    }
}

@@ -34,90 +34,8 @@
 e;%0A%0A
-import com.google.common.base.Predicates;%0Aimport com.google.common.collect.Maps;%0A%0A
 impo
@@ -886,1359 +886,8 @@
  %7D%0A%0A
-    /*public RawParameters(final Map%3CString, String%3E queryParams) %7B%0A        context = Maps.newHashMap();%0A        parseDotParameters(queryParams, %22ctx%22, context);%0A        identifiers = Maps.newHashMap();%0A        parseDotParameters(queryParams, %22id%22, identifiers);%0A%0A        // Parse the comma-separated tests into a list.%0A        if (queryParams.containsKey(%22test%22)) %7B%0A            test = Lists.newArrayList(Splitter.on(',').trimResults().omitEmptyStrings().split(queryParams.get(%22test%22)));%0A        %7D else %7B%0A            test = null;%0A        %7D%0A    %7D*/%0A%0A    /**%0A     * Takes all the query parameters, finds the ones that start with prefix and a period, strips it out, and places%0A     * the key value pair into destination.%0A     */%0A    private void parseDotParameters(%0A            final Map%3CString, String%3E queryParams, final String prefix, final Map%3CString, String%3E destination) %7B%0A%0A        // Regex to match beginning of input, prefix, and period.%0A        for (final Map.Entry%3CString, String%3E e : Maps.filterKeys(%0A                queryParams, Predicates.containsPattern(%22%5C%5CA%22 + prefix + %22%5C%5C.%22)).entrySet()) %7B%0A            // Get rid of the prefix and period character, which are always at the beginning of the string.%0A            final String key = e.getKey().substring(prefix.length() + 1);%0A            destination.put(key, e.getValue());%0A        %7D%0A    %7D%0A%0A
     

Remove unused method and commented out method I forgot about.