<commit_before>package com.alexstyl.specialdates.events;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.ContactsContract;

import com.alexstyl.specialdates.DisplayName;
import com.alexstyl.specialdates.ErrorTracker;
import com.alexstyl.specialdates.contact.Birthday;
import com.alexstyl.specialdates.contact.Contact;
import com.alexstyl.specialdates.contact.DeviceContact;
import com.alexstyl.specialdates.date.DateParseException;
import com.alexstyl.specialdates.date.DayDate;
import com.alexstyl.specialdates.events.database.EventSQLiteOpenHelper;
import com.alexstyl.specialdates.util.DateParser;
import com.alexstyl.specialdates.util.Utils;
import com.novoda.notils.exception.DeveloperError;

import java.util.ArrayList;
import java.util.List;

class BirthdayDatabaseRefresher {

    private final ContentResolver contentResolver;
    private final DateParser dateParser;
    private final PeopleEventsPersister persister;
    private final BirthdayContentValuesMarshaller marshaller;

    static BirthdayDatabaseRefresher newInstance(Context context) {
        ContentResolver cr = context.getContentResolver();
        DateParser dateParser = new DateParser();
        PeopleEventsPersister persister = new PeopleEventsPersister(new EventSQLiteOpenHelper(context));
        BirthdayContentValuesMarshaller marshaller = new BirthdayContentValuesMarshaller();
        return new BirthdayDatabaseRefresher(cr, dateParser, persister, marshaller);
    }

    BirthdayDatabaseRefresher(ContentResolver contentResolver, DateParser dateParser, PeopleEventsPersister persister, BirthdayContentValuesMarshaller marshaller) {
        this.contentResolver = contentResolver;
        this.dateParser = dateParser;
        this.persister = persister;
        this.marshaller = marshaller;
    }

    public void refreshBirthdays() {
        clearAllBirthdays();
        List<Contact> contacts = loadBirtdaysFromDisk();
        storeContactsToProvider(contacts);
    }

    private void clearAllBirthdays() {
        persister.deleteAllBirthdays();
    }

    private List<Contact> loadBirtdaysFromDisk() {
        Cursor cursor = Query.query(contentResolver);
        if (isInvalid(cursor)) {
            throw new DeveloperError("Cursor was invalid");
        }
        List<Contact> contacts = new ArrayList<>();
        try {
            while (cursor.moveToNext()) {
                String dateOfBirthText = cursor.getString(Query.BIRTHDAY);
                Birthday dateOfBirth;
                try {
                    dateOfBirth = birthdayOn(dateOfBirthText);
                } catch (DateParseException e) {
                    ErrorTracker.track(e);
                    continue;
                }
                long id = cursor.getLong(Query.ID);

                DisplayName display = getDisplayNameFrom(cursor);
                String lookup = cursor.getString(Query.LOOKUP_KEY);
                DeviceContact contact = new DeviceContact(id, display, lookup, dateOfBirth);
                contacts.add(contact);
            }
        } finally {<commit_msg>Return no contacts instead of throwing exception<commit_after>package com.alexstyl.specialdates.events;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.ContactsContract;

import com.alexstyl.specialdates.DisplayName;
import com.alexstyl.specialdates.ErrorTracker;
import com.alexstyl.specialdates.contact.Birthday;
import com.alexstyl.specialdates.contact.Contact;
import com.alexstyl.specialdates.contact.DeviceContact;
import com.alexstyl.specialdates.date.DateParseException;
import com.alexstyl.specialdates.date.DayDate;
import com.alexstyl.specialdates.events.database.EventSQLiteOpenHelper;
import com.alexstyl.specialdates.util.DateParser;
import com.alexstyl.specialdates.util.Utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class BirthdayDatabaseRefresher {

    @SuppressWarnings("unchecked")
    private static final List<Contact> NO_CONTACTS = Collections.EMPTY_LIST;

    private final ContentResolver contentResolver;
    private final DateParser dateParser;
    private final PeopleEventsPersister persister;
    private final BirthdayContentValuesMarshaller marshaller;

    static BirthdayDatabaseRefresher newInstance(Context context) {
        ContentResolver cr = context.getContentResolver();
        DateParser dateParser = new DateParser();
        PeopleEventsPersister persister = new PeopleEventsPersister(new EventSQLiteOpenHelper(context));
        BirthdayContentValuesMarshaller marshaller = new BirthdayContentValuesMarshaller();
        return new BirthdayDatabaseRefresher(cr, dateParser, persister, marshaller);
    }

    BirthdayDatabaseRefresher(ContentResolver contentResolver, DateParser dateParser, PeopleEventsPersister persister, BirthdayContentValuesMarshaller marshaller) {
        this.contentResolver = contentResolver;
        this.dateParser = dateParser;
        this.persister = persister;
        this.marshaller = marshaller;
    }

    public void refreshBirthdays() {
        clearAllBirthdays();
        List<Contact> contacts = loadBirtdaysFromDisk();
        storeContactsToProvider(contacts);
    }

    private void clearAllBirthdays() {
        persister.deleteAllBirthdays();
    }

    private List<Contact> loadBirtdaysFromDisk() {
        Cursor cursor = Query.query(contentResolver);
        if (isInvalid(cursor)) {
            return NO_CONTACTS;
        }
        List<Contact> contacts = new ArrayList<>();
        try {
            while (cursor.moveToNext()) {
                String dateOfBirthText = cursor.getString(Query.BIRTHDAY);
                Birthday dateOfBirth;
                try {
                    dateOfBirth = birthdayOn(dateOfBirthText);
                } catch (DateParseException e) {
                    ErrorTracker.track(e);
                    continue;
                }
                long id = cursor.getLong(Query.ID);

                DisplayName display = getDisplayNameFrom(cursor);
                String lookup = cursor.getString(Query.LOOKUP_KEY);
                DeviceContact contact = new DeviceContact(id, display, lookup, dateOfBirth);
                contacts.add(contact);
            }
        } finally {
{'fraction_non_alphanumeric': 0.0776670138372266, 'fraction_numerical': 0.0, 'mean_word_length': 3.3592736705577173, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2374655', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1736, 'n_words': 348}