<commit_before>/*
 * Copyright (c) 2017 Titan Robotics Club (http://www.titanrobotics.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package shell;

import java.util.ArrayList;
import java.util.HashMap;

import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonStructure;
import javax.json.JsonValue;

import webapi.TbaApiV3;

/**
 * This class implements a parser to parse and process TBA commmands.
 */
public class TBACommand
{
    /**
     * This class parses the filter part of the command and create a set of <key>=<value> pairs.
     */
    private class FilterSet
    {
        private HashMap<String, String> filters = new HashMap<>();

        /**
         * Constructor: Create an instance of the object.
         * A filter string contains a set of filters separated by '&'. A basic filter syntax is <key>=<value>.
         *
         * @param filterCommand specifies the filter command string.
         */
        public FilterSet(String filterCommand)
        {
            for (String s: filterCommand.split("&"))
            {
                String[] pair = s.split("=");
                if (pair.length != 2)
                {
                    throw new IllegalArgumentException("Invalid filter syntax, expecting \"<key>=<value>\".");
                }
                else
                {
                    filters.put(pair[0], pair[1]);
                }
            }
        }

        /**
         * This method finds the filter with the specified key and returns the filter value.
         *
         * @param key specifies the filter key.
         * @return corresponding filter value.
         */
        public String getValue(String key)
        {
            return filters.get(key);
        }   //getValue

        /**
         * This method returns the number of filters in the filter set.
         *
         * @return number of filters in the set.
         */
        int getNumFilters()
        {
            return filters.size();
        }   //getNumFilters

    }   //class FilterSet

    private TbaApiV3 tbaApi;

    /**
     * Constructor: Create an instance of the object.
     *
     * @param authorId specifies the author ID.
     * @param appName specifies the application name.
     * @param appVersion specifies the applicaton version.
     */
    public TBACommand(String authorId, String appName, String appVersion)
    {
        tbaApi = new TbaApiV3(authorId, appName, appVersion);
    }   //TBACommand

    /**
     * This method prints the command help message.
     *
     * @param longVersion specifies true to print the long version of the message, false to print the short version.
     */
    public void printCommandHelp(boolean longVersion)
    {
        System.out.print(
            "<Options>:\n" +
            "\t-(0|1|2)\t\t\t- Specifies output verbose level (0: minimum, 1: medium, 2: maximum - default is 1).\n" +
            "<Model>:\n" +
            "\tstatus[?team=<TeamKey>&event=<EventKey>]\n" +
            "\tteams?[(year=<Year>|team=<TeamKey>|event=<EventKey>|district=<DistrictKey>)]\n" +
            "\tevents?(year=<Year>|team=<TeamKey>[&year=<Year>]|event=<EventKey>|district=<DistrictKey>)\n" +
            "\tdistricts?(year=<Year>|team=<TeamKey>)\n" +
            "\tmatches?(team=<TeamKey>&year=<Year>|event=<EventKey>[&team=<TeamKey>]|match=<MatchKey>)\n" +
            "\tawards?(team=<TeamKey>[&year=<Year>]|event=<EventKey>[&team=<TeamKey>])\n" +
            "\trankings?(event=<EventKey>|district=<DistrictKey>)\n" +
            "\toprs?event=<EventKey>\n" +
            "\tdistrict_points?event=<EventKey>\n" +
            "\tinsights?event=<EventKey>\n" +
            "\tpredictions?event=<EventKey>\n" +
            "\talliances?event=<EventKey>\n" +
            "\tyears_participated?team=<TeamKey>\n" +
            "\trobots?team=<TeamKey>\n" +
            "\tmedia?team=<TeamKey>&year=<Year>\n" +
            "\tsocial_media?team=<TeamKey>\n");
        if (longVersion)
        {
            tbaApi.printApiHelp();
        }
    }   //printCommandHelp

    /**
     * This method processes a TBA request command.
     * The syntax of a basic TBA request command is:
     * Command   ::= [-<Options>] <Model>?[<FilterSet>]
     * Options   ::= Specifies command options such as verbose level.
     * Model     ::= Specifies data model such as teams, events, districts, matches etc.
     * FilterSet ::= <Filter>{&<Filter>}
     * Filter    ::= <Key>=<Value>
     * Key       ::= Specifies the filter key.
     * Value     ::= Specifies the filter value.
     *
     * @param tokens specifies the tokens split from the command line.
     */
    public void processCommand(String[] tokens)
    {
        String errorMsg = null;
<commit_msg>Added more comments.<commit_after>/*
 * Copyright (c) 2017 Titan Robotics Club (http://www.titanrobotics.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package shell;

import java.util.ArrayList;
import java.util.HashMap;

import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonStructure;
import javax.json.JsonValue;

import webapi.TbaApiV3;

/**
 * This class implements a parser to parse and process TBA commmands.
 */
public class TBACommand
{
    /**
     * This class parses the filter part of the command and create a set of <key>=<value> pairs.
     */
    private class FilterSet
    {
        private HashMap<String, String> filters = new HashMap<>();

        /**
         * Constructor: Create an instance of the object.
         * A filter string contains a set of filters separated by '&'. A basic filter syntax is <key>=<value>.
         *
         * @param filterCommand specifies the filter command string.
         */
        public FilterSet(String filterCommand)
        {
            for (String s: filterCommand.split("&"))
            {
                String[] pair = s.split("=");
                if (pair.length != 2)
                {
                    throw new IllegalArgumentException("Invalid filter syntax, expecting \"<key>=<value>\".");
                }
                else
                {
                    filters.put(pair[0], pair[1]);
                }
            }
        }   //FilterSet

        /**
         * This method finds the filter with the specified key and returns the filter value.
         *
         * @param key specifies the filter key.
         * @return corresponding filter value.
         */
        public String getValue(String key)
        {
            return filters.get(key);
        }   //getValue

        /**
         * This method returns the number of filters in the filter set.
         *
         * @return number of filters in the set.
         */
        int getNumFilters()
        {
            return filters.size();
        }   //getNumFilters

    }   //class FilterSet

    private TbaApiV3 tbaApi;

    /**
     * Constructor: Create an instance of the object.
     *
     * @param authorId specifies the author ID.
     * @param appName specifies the application name.
     * @param appVersion specifies the application version.
     */
    public TBACommand(String authorId, String appName, String appVersion)
    {
        tbaApi = new TbaApiV3(authorId, appName, appVersion);
    }   //TBACommand

    /**
     * This method prints the command help message.
     *
     * @param longVersion specifies true to print the long version of the message, false to print the short version.
     */
    public void printCommandHelp(boolean longVersion)
    {
        System.out.print(
            "<Options>:\n" +
            "\t-(0|1|2)\t\t\t- Specifies output verbose level (0: minimum, 1: medium, 2: maximum - default is 1).\n" +
            "<Model>:\n" +
            "\tstatus[?team=<TeamKey>&event=<EventKey>]\n" +
            "\tteams?[(year=<Year>|team=<TeamKey>|event=<EventKey>|district=<DistrictKey>)]\n" +
            "\tevents?(year=<Year>|team=<TeamKey>[&year=<Year>]|event=<EventKey>|district=<DistrictKey>)\n" +
            "\tdistricts?(year=<Year>|team=<TeamKey>)\n" +
            "\tmatches?(team=<TeamKey>&year=<Year>|event=<EventKey>[&team=<TeamKey>]|match=<MatchKey>)\n" +
            "\tawards?(team=<TeamKey>[&year=<Year>]|event=<EventKey>[&team=<TeamKey>])\n" +
            "\trankings?(event=<EventKey>|district=<DistrictKey>)\n" +
            "\toprs?event=<EventKey>\n" +
            "\tdistrict_points?event=<EventKey>\n" +
            "\tinsights?event=<EventKey>\n" +
            "\tpredictions?event=<EventKey>\n" +
            "\talliances?event=<EventKey>\n" +
            "\tyears_participated?team=<TeamKey>\n" +
            "\trobots?team=<TeamKey>\n" +
            "\tmedia?team=<TeamKey>&year=<Year>\n" +
            "\tsocial_media?team=<TeamKey>\n");
        if (longVersion)
        {
            tbaApi.printApiHelp();
        }
    }   //printCommandHelp

    /**
     * This method processes a TBA request command.
     * The syntax of a basic TBA request command is:
     *  Command     ::= [-<Options>] <Model>?[<FilterSet>]
     *  Options     ::= Specifies command options such as verbose level.
     *  Model       ::= Specifies data model such as teams, events, districts, matches etc.
     *  FilterSet   ::= <Filter>{&<Filter>}
     *  Filter      ::= <Key>=<Value>
     *  Key         ::= Specifies the filter key.
     *  Value       ::= Specifies the filter value.
     *
     * @param tokens specifies the tokens split from the command line.
     */
    public void processCommand(String[] tokens)
    {
        String errorMsg = null;

{'fraction_non_alphanumeric': 0.1150047238684188, 'fraction_numerical': 0.002920209567980761, 'mean_word_length': 2.3173789173789174, 'pattern_counts': {'":': 0, '<': 105, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4707525', 'n_tokens_mistral': 3326, 'n_tokens_neox': 3217, 'n_words': 1023}