<commit_before>package info.nightscout.client.data;

import com.crashlytics.android.Crashlytics;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.TimeZone;

import info.nightscout.androidaps.Constants;
import info.nightscout.utils.DecimalFormatter;

public class NSProfile {
    private static Logger log = LoggerFactory.getLogger(NSProfile.class);

    private JSONObject json = null;
    private String activeProfile = null;

    public NSProfile(JSONObject json, String activeProfile) {
        this.json = json;
        this.activeProfile = activeProfile;
    }

    JSONObject getDefaultProfile() {
        String defaultProfileName = null;
        JSONObject store;
        JSONObject profile = null;
        try {
            defaultProfileName = (String) json.get("defaultProfile");
            store = json.getJSONObject("store");
            if (activeProfile != null && store.has(activeProfile)) {
                defaultProfileName = activeProfile;
            }
            profile = store.getJSONObject(defaultProfileName);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return profile;
    }

    public JSONObject getSpecificProfile(String profileName) {
        JSONObject profile = null;
        try {
            JSONObject store = json.getJSONObject("store");
            if (store.has(profileName)) {
                profile = store.getJSONObject(profileName);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return profile;
    }

    public ArrayList<CharSequence> getProfileList() {
        ArrayList<CharSequence> ret = new ArrayList<CharSequence>();

        JSONObject store;
        JSONObject profile = null;
        try {
            store = json.getJSONObject("store");
            Iterator<?> keys = store.keys();

            while (keys.hasNext()) {
                String profileName = (String) keys.next();
                ret.add(profileName);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return ret;
    }

    public String log() {
        String ret = "\n";
        for (Integer hour = 0; hour < 24; hour++) {
            double value = getBasal(hour * 60 * 60);
            ret += "NS basal value for " + hour + ":00 is " + value + "\n";
        }
        ret += "NS units: " + getUnits();
        return ret;
    }

    public JSONObject getData() {
        return json;
    }

    public Double getDia() {
        return getDia(getDefaultProfile());
    }

    public Double getDia(JSONObject profile) {
        Double dia;
        if (profile != null) {
            try {
                dia = profile.getDouble("dia");
                return dia;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return 3D;
    }

    public Double getCarbAbsorbtionRate() {
        return getCarbAbsorbtionRate(getDefaultProfile());
    }

    public Double getCarbAbsorbtionRate(JSONObject profile) {
        Double carbAbsorptionRate;
        if (profile != null) {
            try {
                carbAbsorptionRate = profile.getDouble("carbs_hr");
                return carbAbsorptionRate;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return 0D;
    }

    // mmol or mg/dl
    public String getUnits() {
        return getUnits(getDefaultProfile());
    }

    public String getUnits(JSONObject profile) {
        String units;
        if (profile != null) {
            try {
                units = profile.getString("units");
                return units.toLowerCase();
            } catch (JSONException e) {
                log.error("Profile not found. Failing over to main JSON");
                try {
                    return json.getString("units").toLowerCase();
                } catch (JSONException e1) {
                    e1.printStackTrace();
                    Crashlytics.log("Profile failover failed too");
                }
            }
        }
        return "mg/dl";
    }

    public TimeZone getTimeZone() {
        return getTimeZone(getDefaultProfile());
    }

    public TimeZone getTimeZone(JSONObject profile) {
        TimeZone timeZone;
        if (profile != null) {
            try {
                return TimeZone.getTimeZone(profile.getString("timezone"));
            } catch (JSONException e) {
                e.printStackTrace();<commit_msg>Constant instead of string in profile<commit_after>package info.nightscout.client.data;

import com.crashlytics.android.Crashlytics;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.TimeZone;

import info.nightscout.androidaps.Constants;
import info.nightscout.utils.DecimalFormatter;

public class NSProfile {
    private static Logger log = LoggerFactory.getLogger(NSProfile.class);

    private JSONObject json = null;
    private String activeProfile = null;

    public NSProfile(JSONObject json, String activeProfile) {
        this.json = json;
        this.activeProfile = activeProfile;
    }

    JSONObject getDefaultProfile() {
        String defaultProfileName = null;
        JSONObject store;
        JSONObject profile = null;
        try {
            defaultProfileName = (String) json.get("defaultProfile");
            store = json.getJSONObject("store");
            if (activeProfile != null && store.has(activeProfile)) {
                defaultProfileName = activeProfile;
            }
            profile = store.getJSONObject(defaultProfileName);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return profile;
    }

    public JSONObject getSpecificProfile(String profileName) {
        JSONObject profile = null;
        try {
            JSONObject store = json.getJSONObject("store");
            if (store.has(profileName)) {
                profile = store.getJSONObject(profileName);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return profile;
    }

    public ArrayList<CharSequence> getProfileList() {
        ArrayList<CharSequence> ret = new ArrayList<CharSequence>();

        JSONObject store;
        JSONObject profile = null;
        try {
            store = json.getJSONObject("store");
            Iterator<?> keys = store.keys();

            while (keys.hasNext()) {
                String profileName = (String) keys.next();
                ret.add(profileName);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return ret;
    }

    public String log() {
        String ret = "\n";
        for (Integer hour = 0; hour < 24; hour++) {
            double value = getBasal(hour * 60 * 60);
            ret += "NS basal value for " + hour + ":00 is " + value + "\n";
        }
        ret += "NS units: " + getUnits();
        return ret;
    }

    public JSONObject getData() {
        return json;
    }

    public Double getDia() {
        return getDia(getDefaultProfile());
    }

    public Double getDia(JSONObject profile) {
        Double dia;
        if (profile != null) {
            try {
                dia = profile.getDouble("dia");
                return dia;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return 3D;
    }

    public Double getCarbAbsorbtionRate() {
        return getCarbAbsorbtionRate(getDefaultProfile());
    }

    public Double getCarbAbsorbtionRate(JSONObject profile) {
        Double carbAbsorptionRate;
        if (profile != null) {
            try {
                carbAbsorptionRate = profile.getDouble("carbs_hr");
                return carbAbsorptionRate;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return 0D;
    }

    // mmol or mg/dl
    public String getUnits() {
        return getUnits(getDefaultProfile());
    }

    public String getUnits(JSONObject profile) {
        String units;
        if (profile != null) {
            try {
                units = profile.getString("units");
                return units.toLowerCase();
            } catch (JSONException e) {
                log.error("Profile not found. Failing over to main JSON");
                try {
                    return json.getString("units").toLowerCase();
                } catch (JSONException e1) {
                    e1.printStackTrace();
                    Crashlytics.log("Profile failover failed too");
                }
            }
        }
        return Constants.MGDL;
    }

    public TimeZone getTimeZone() {
        return getTimeZone(getDefaultProfile());
    }

    public TimeZone getTimeZone(JSONObject profile) {
        TimeZone timeZone;
        if (profile != null) {
            try {
                return TimeZone.getTimeZone(profile.getString("timezone"));
            } catch (JSONException e) {
                e.printStackTrace();
{'fraction_non_alphanumeric': 0.0983950487779293, 'fraction_numerical': 0.003146963180530788, 'mean_word_length': 1.839189994044074, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2274096', 'n_tokens_mistral': 2527, 'n_tokens_neox': 2432, 'n_words': 590}