<commit_before>package io.branch.referral;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;

/**
 * <p>The Branch SDK can queue up requests whilst it is waiting for initialization of a session to
 * complete. This allows you to start sending requests to the Branch API as soon as your app is
 * opened.</p>
 */
class ServerRequestQueue {
    private static final String PREF_KEY = "BNCServerRequestQueue";
    private static final int MAX_ITEMS = 25;
    private static ServerRequestQueue SharedInstance;
    private SharedPreferences sharedPref;
    private SharedPreferences.Editor editor;
    private final List<ServerRequest> queue;

    /**
     * <p>Singleton method to return the pre-initialised, or newly initialise and return, a singleton
     * object of the type {@link ServerRequestQueue}.</p>
     *
     * @param c A {@link Context} from which this call was made.
     * @return An initialised {@link ServerRequestQueue} object, either fetched from a
     * pre-initialised instance within the singleton class, or a newly instantiated
     * object where one was not already requested during the current app lifecycle.
     */
    public static ServerRequestQueue getInstance(Context c) {
        if (SharedInstance == null) {
            synchronized (ServerRequestQueue.class) {
                if (SharedInstance == null) {
                    SharedInstance = new ServerRequestQueue(c);
                }
            }
        }
        return SharedInstance;
    }

    /**
     * <p>The main constructor of the ServerRequestQueue class is private because the class uses the
     * Singleton pattern.</p>
     *
     * @param c A {@link Context} from which this call was made.
     */
    @SuppressLint("CommitPrefEdits")
    private ServerRequestQueue(Context c) {
        sharedPref = c.getSharedPreferences("BNC_Server_Request_Queue", Context.MODE_PRIVATE);
        editor = sharedPref.edit();
        queue = retrieve(c);
    }

    private void persist() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (queue) {
                    JSONArray jsonArr = new JSONArray();
                    for (ServerRequest aQueue : queue) {
                        if(aQueue.isPersistable()) {
                            JSONObject json = aQueue.toJSON();
                            if (json != null) {
                                jsonArr.put(json);
                            }
                        }
                    }
                    boolean succeeded = false;
                    try {
                        editor.putString(PREF_KEY, jsonArr.toString()).commit();
                        succeeded = true;
                    } catch (ConcurrentModificationException ex) {
                        PrefHelper.Debug("Persisting Queue: ", "Failed to persit queue " + ex.getMessage());
                    } finally {
                        if (!succeeded) {
                            try {
                                editor.putString(PREF_KEY, jsonArr.toString()).commit();
                            } catch (ConcurrentModificationException ignored) {
                            }
                        }
                    }

                }
            }
        }).start();
    }
    
    private List<ServerRequest> retrieve(Context context) {
        List<ServerRequest> result = Collections.synchronizedList(new LinkedList<ServerRequest>());
        String jsonStr = sharedPref.getString(PREF_KEY, null);
        
        if (jsonStr != null) {
            try {
                JSONArray jsonArr = new JSONArray(jsonStr);
                for (int i = 0; i < Math.min(jsonArr.length(), MAX_ITEMS); i++) {
                    JSONObject json = jsonArr.getJSONObject(i);
                    ServerRequest req = ServerRequest.fromJSON(json, context);
                    if (req != null) {
                        result.add(req);
                    }
                }
            } catch (JSONException ignored) {
            }<commit_msg>catch NPE<commit_after>package io.branch.referral;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;

/**
 * <p>The Branch SDK can queue up requests whilst it is waiting for initialization of a session to
 * complete. This allows you to start sending requests to the Branch API as soon as your app is
 * opened.</p>
 */
class ServerRequestQueue {
    private static final String PREF_KEY = "BNCServerRequestQueue";
    private static final int MAX_ITEMS = 25;
    private static ServerRequestQueue SharedInstance;
    private SharedPreferences sharedPref;
    private SharedPreferences.Editor editor;
    private final List<ServerRequest> queue;

    /**
     * <p>Singleton method to return the pre-initialised, or newly initialise and return, a singleton
     * object of the type {@link ServerRequestQueue}.</p>
     *
     * @param c A {@link Context} from which this call was made.
     * @return An initialised {@link ServerRequestQueue} object, either fetched from a
     * pre-initialised instance within the singleton class, or a newly instantiated
     * object where one was not already requested during the current app lifecycle.
     */
    public static ServerRequestQueue getInstance(Context c) {
        if (SharedInstance == null) {
            synchronized (ServerRequestQueue.class) {
                if (SharedInstance == null) {
                    SharedInstance = new ServerRequestQueue(c);
                }
            }
        }
        return SharedInstance;
    }

    /**
     * <p>The main constructor of the ServerRequestQueue class is private because the class uses the
     * Singleton pattern.</p>
     *
     * @param c A {@link Context} from which this call was made.
     */
    @SuppressLint("CommitPrefEdits")
    private ServerRequestQueue(Context c) {
        sharedPref = c.getSharedPreferences("BNC_Server_Request_Queue", Context.MODE_PRIVATE);
        editor = sharedPref.edit();
        queue = retrieve(c);
    }

    private void persist() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (queue) {
                    JSONArray jsonArr = new JSONArray();
                    for (ServerRequest aQueue : queue) {
                        if(aQueue.isPersistable()) {
                            JSONObject json = aQueue.toJSON();
                            if (json != null) {
                                jsonArr.put(json);
                            }
                        }
                    }
                    boolean succeeded = false;
                    try {
                        editor.putString(PREF_KEY, jsonArr.toString()).commit();
                        succeeded = true;
                    } catch (ConcurrentModificationException ex) {
                        PrefHelper.Debug("Persisting Queue: ", "Failed to persist queue " + ex.getMessage());
                    } catch (NullPointerException ex) {
                        PrefHelper.Debug("Persisting Queue: ", "Failed to persist queue " + ex.getMessage());
                    } finally {
                        if (!succeeded) {
                            try {
                                editor.putString(PREF_KEY, jsonArr.toString()).commit();
                            } catch (ConcurrentModificationException ignored) {
                            }
                        }
                    }

                }
            }
        }).start();
    }
    
    private List<ServerRequest> retrieve(Context context) {
        List<ServerRequest> result = Collections.synchronizedList(new LinkedList<ServerRequest>());
        String jsonStr = sharedPref.getString(PREF_KEY, null);
        
        if (jsonStr != null) {
            try {
                JSONArray jsonArr = new JSONArray(jsonStr);
                for (int i = 0; i < Math.min(jsonArr.length(), MAX_ITEMS); i++) {
                    JSONObject json = jsonArr.getJSONObject(i);
                    ServerRequest req = ServerRequest.fromJSON(json, context);
                    if (req != null) {
                        result.add(req);
                    }
                }
            } catch (JSONException ignored) {
            }
{'fraction_non_alphanumeric': 0.08399066770358848, 'fraction_numerical': 0.0006665926008221309, 'mean_word_length': 1.7588109102053324, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 23, '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': '2011070', 'n_tokens_mistral': 2210, 'n_tokens_neox': 2120, 'n_words': 623}