<commit_before>package com.markupartist.sthlmtraveling;

import static com.markupartist.sthlmtraveling.ApiSettings.STHLM_TRAVELING_API_ENDPOINT;

import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.xml.sax.InputSource;

import android.util.Log;

public class Planner {
    private static final String TAG = "Planner";
    private static Planner instance = null;
    private boolean mUseMockData = true;
    /**
     * Needed to keep track of which routes to remove when getting earlier and
     * later routes. Later on we will return the full set then this will be removed.
     */
    private int mPreviousRouteCount;
    private int mRequestCount;
    private String mIdent;
    private StopParser mStopFinder;
    private RouteParser mRouteFinder;
    private RouteDetailParser mRouteDetailFinder;
    private ArrayList<Route> mRoutes = null;
    private ArrayList<String> mRouteDetails = null;

    private Planner() {
        mStopFinder = new StopParser();
        mRouteFinder = new RouteParser();
        mRouteDetailFinder = new RouteDetailParser();
    }

    public ArrayList<String> findStop(String name) {
        ArrayList<String> stops = new ArrayList<String>();
        try {
            InputSource input;
            if (mUseMockData) {
                StringReader sr = new StringReader(mStopsXml);
                input = new InputSource(sr);
            } else {
                URL endpoint = new URL(STHLM_TRAVELING_API_ENDPOINT
                        + "?method=findStop&name=" + URLEncoder.encode(name));
                input = new InputSource(endpoint.openStream());
            }

            stops = mStopFinder.parseStops(input);
        } catch (MalformedURLException e) {
            Log.e(TAG, e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
        }
        return stops;
    }

    public ArrayList<Route> findRoutes(String from, String to) {
        Log.d(TAG, "Searching for from=" + from + ",to=" + to);

        String fromEncoded = URLEncoder.encode(from);
        String toEncoded = URLEncoder.encode(to);

        mRoutes = new ArrayList<Route>();
        try {
            InputSource input;
            if (mUseMockData) {
                StringReader sr = new StringReader(mRoutesXml);
                input = new InputSource(sr);
            } else {
                URL endpoint = new URL(STHLM_TRAVELING_API_ENDPOINT
                        + "?method=findRoutes&from=" 
                        + fromEncoded + "&to=" + toEncoded);
                input = new InputSource(endpoint.openStream());
            }
            mRoutes = mRouteFinder.parseRoutes(input);

            mPreviousRouteCount = mRoutes.size();
            // Update the request count, needed for all request that needs an ident.
            mRequestCount = mRouteFinder.getRequestCount();
            mIdent = mRouteFinder.getIdent();
        } catch (MalformedURLException e) {
            Log.e(TAG, e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
        }
        return mRoutes;
    }

    public ArrayList<Route> findEarlierRoutes() {
        if (mIdent == null) {
            Log.e(TAG, "findEarlierRoutes was accessed before findRoutes.");
            throw new IllegalStateException("findRoutes must be run firsts.");
        }

        String ident = URLEncoder.encode(mIdent);<commit_msg>added support for time in findRoutes<commit_after>package com.markupartist.sthlmtraveling;

import static com.markupartist.sthlmtraveling.ApiSettings.STHLM_TRAVELING_API_ENDPOINT;

import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.xml.sax.InputSource;

import android.text.format.Time;
import android.util.Log;

public class Planner {
    private static final String TAG = "Planner";
    private static Planner instance = null;
    private boolean mUseMockData = false;
    /**
     * Needed to keep track of which routes to remove when getting earlier and
     * later routes. Later on we will return the full set then this will be removed.
     */
    //private int mPreviousRouteCount;
    private int mRequestCount;
    private String mIdent;
    private StopParser mStopFinder;
    private RouteParser mRouteFinder;
    private RouteDetailParser mRouteDetailFinder;
    private ArrayList<Route> mRoutes = null;
    private ArrayList<String> mRouteDetails = null;

    private Planner() {
        mStopFinder = new StopParser();
        mRouteFinder = new RouteParser();
        mRouteDetailFinder = new RouteDetailParser();
    }

    public ArrayList<String> findStop(String name) {
        ArrayList<String> stops = new ArrayList<String>();
        try {
            InputSource input;
            if (mUseMockData) {
                StringReader sr = new StringReader(mStopsXml);
                input = new InputSource(sr);
            } else {
                URL endpoint = new URL(STHLM_TRAVELING_API_ENDPOINT
                        + "?method=findStop&name=" + URLEncoder.encode(name));
                input = new InputSource(endpoint.openStream());
            }

            stops = mStopFinder.parseStops(input);
        } catch (MalformedURLException e) {
            Log.e(TAG, e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
        }
        return stops;
    }

    public ArrayList<Route> findRoutes(String startPoint, String endPoint, 
            Time time) {
        Log.d(TAG, "Searching for startPoint=" + startPoint + ",endPoint=" + endPoint);

        String startPointEncoded = URLEncoder.encode(startPoint);
        String endPointEncoded = URLEncoder.encode(endPoint);
        String timeEncoded = URLEncoder.encode(time.format("%Y-%m-%d %H:%M"));

        mRoutes = new ArrayList<Route>();
        try {
            InputSource input;
            if (mUseMockData) {
                StringReader sr = new StringReader(mRoutesXml);
                input = new InputSource(sr);
            } else {
                URL endpoint = new URL(STHLM_TRAVELING_API_ENDPOINT
                        + "?method=findRoutes" 
                        + "&from=" + startPointEncoded 
                        + "&to=" + endPointEncoded
                        + "&time=" + timeEncoded);
                input = new InputSource(endpoint.openStream());
            }
            mRoutes = mRouteFinder.parseRoutes(input);

            //mPreviousRouteCount = mRoutes.size();
            // Update the request count, needed for all request that needs an ident.
            mRequestCount = mRouteFinder.getRequestCount();
            mIdent = mRouteFinder.getIdent();
        } catch (MalformedURLException e) {
            Log.e(TAG, e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
        }
        return mRoutes;
    }

    public ArrayList<Route> findEarlierRoutes() {
        if (mIdent == null) {
            Log.e(TAG, "findEarlierRoutes was accessed before findRoutes.");
            throw new IllegalStateException("findRoutes must be run firsts.");
        }

        String ident = URLEncoder.encode(mIdent);
{'fraction_non_alphanumeric': 0.0930106800054076, 'fraction_numerical': 0.0, 'mean_word_length': 2.256161971830986, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '5210034', 'n_tokens_mistral': 2054, 'n_tokens_neox': 2014, 'n_words': 511}