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

import android.content.ContentProviderOperation;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Data;

import com.alexstyl.specialdates.date.ContactEvent;
import com.alexstyl.specialdates.date.Date;
import com.alexstyl.specialdates.events.peopleevents.EventType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

final class OperationsFactory {
    private final int rawContactID;

    static OperationsFactory forNewContact() {
        return new OperationsFactory(0);
    }

    OperationsFactory(int rawContactID) {
        this.rawContactID = rawContactID;
    }

    ContentProviderOperation newInsertFor(EventType eventType, Date date) {
        return ContentProviderOperation
                .newInsert(Data.CONTENT_URI)
                .withValue(Event.RAW_CONTACT_ID, rawContactID)
                .withValue(Event.TYPE, eventType.getAndroidType())
                .withValue(Event.MIMETYPE, Event.CONTENT_ITEM_TYPE)
                .withValue(Event.START_DATE, date.toShortDate())
                .build();
    }

    ArrayList<ContentProviderOperation> deleteEvents(List<ContactEvent> contactEvents) {
        ArrayList<ContentProviderOperation> ops = new ArrayList<>();
        for (ContactEvent contactEvent : contactEvents) {
            long eventId = contactEvent.getDeviceEventId().get();
            ops.add(
                    ContentProviderOperation
                            .newDelete(Data.CONTENT_URI)
                            .withSelection(Event._ID + "= " + eventId, null)
                            .build());
        }
        return ops;
    }

    List<ContentProviderOperation> createContactIn(AccountData account, String contactName) {
        List<ContentProviderOperation> ops = new ArrayList<>(2);
        ops.add(ContentProviderOperation
                        .newInsert(ContactsContract.RawContacts.CONTENT_URI)
                        .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, account.getAccountName())
                        .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, account.getAccountType())
                        .build());
        ops.add(ContentProviderOperation
                        .newInsert(Data.CONTENT_URI)
                        .withValueBackReference(Data.RAW_CONTACT_ID, rawContactID)
                        .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                        .withValue(StructuredName.DISPLAY_NAME, contactName)
                        .build());

        return ops;
    }

    List<ContentProviderOperation> insertEvents(Set<Map.Entry<EventType, Date>> events) {
        List<ContentProviderOperation> operations = new ArrayList<>(events.size());
        for (Map.Entry<EventType, Date> entry : events) {
            EventType eventType = entry.getKey();
            Date date = entry.getValue();
            ContentProviderOperation operation = newInsertFor(eventType, date);
            operations.add(operation);
        }
        return operations;<commit_msg>Use RAW_CONTACT_ID withBackReference for new contacts<commit_after>package com.alexstyl.specialdates.addevent;

import android.content.ContentProviderOperation;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Data;

import com.alexstyl.specialdates.date.ContactEvent;
import com.alexstyl.specialdates.date.Date;
import com.alexstyl.specialdates.events.peopleevents.EventType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

final class OperationsFactory {
    private final int rawContactID;

    static OperationsFactory forNewContact() {
        return new OperationsFactory(0);
    }

    OperationsFactory(int rawContactID) {
        this.rawContactID = rawContactID;
    }

    ContentProviderOperation newInsertFor(EventType eventType, Date date) {
        return ContentProviderOperation
                .newInsert(Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, rawContactID)
                .withValue(Data.MIMETYPE, Event.CONTENT_ITEM_TYPE)
                .withValue(Event.TYPE, eventType.getAndroidType())
                .withValue(Event.START_DATE, date.toShortDate())
                .build();
    }

    ArrayList<ContentProviderOperation> deleteEvents(List<ContactEvent> contactEvents) {
        ArrayList<ContentProviderOperation> ops = new ArrayList<>();
        for (ContactEvent contactEvent : contactEvents) {
            long eventId = contactEvent.getDeviceEventId().get();
            ops.add(
                    ContentProviderOperation
                            .newDelete(Data.CONTENT_URI)
                            .withSelection(Event._ID + "= " + eventId, null)
                            .build());
        }
        return ops;
    }


    List<ContentProviderOperation> createContactIn(AccountData account, String contactName) {
        List<ContentProviderOperation> ops = new ArrayList<>(2);
        ops.add(ContentProviderOperation
                        .newInsert(ContactsContract.RawContacts.CONTENT_URI)
                        .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, account.getAccountName())
                        .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, account.getAccountType())
                        .build());
        ops.add(ContentProviderOperation
                        .newInsert(Data.CONTENT_URI)
                        .withValueBackReference(Data.RAW_CONTACT_ID, rawContactID)
                        .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                        .withValue(StructuredName.DISPLAY_NAME, contactName)
                        .build());

        return ops;
    }

    List<ContentProviderOperation> insertEvents(Set<Map.Entry<EventType, Date>> events) {
        List<ContentProviderOperation> operations = new ArrayList<>(events.size());
        for (Map.Entry<EventType, Date> entry : events) {
            EventType eventType = entry.getKey();
            Date date = entry.getValue();
            ContentProviderOperation operation = newInsertFor(eventType, date);
            operations.add(operation);
        }
        return operations;
{'fraction_non_alphanumeric': 0.08664041199636474, 'fraction_numerical': 0.0006058770069675856, 'mean_word_length': 2.5595687331536388, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '5566261', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1652, 'n_words': 276}