<commit_before>package com.danikula.android16.core.content;

import java.util.HashMap;
import java.util.Map;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

/**
 *       .
 * <p>
 *  :
 * <ul>
 * <li>
 *         {@link #registerMultipleItem(int, String)}
 * {@link #registerSingleItem(int, String, String)}      {@link #getType(Uri)}</li>
 * <li>   CRUD!</li>
 * <li>to be continued....</li>
 * </ul>
 * </p>
 *     - :
 * 
 * <pre>
 * public class TasksContentProvider extends DatabaseContentProvider {
 * 
 *     private static final String CONTENT_AUTHORITY = &quot;com.ineed.taskprovider&quot;;
 *     static final Uri BASE_CONTENT_URI = Uri.parse(&quot;content://&quot; + CONTENT_AUTHORITY);
 * 
 *     private static final int TASKS = 100;
 *     private static final int TASK_ITEM = 101;
 * 
 *     public TasksContentProvider() {
 *         addMathURI(CONTENT_AUTHORITY, TaskItem.PATH, TASKS);
 *         addMathURI(CONTENT_AUTHORITY, TaskItem.PATH + &quot;/*&quot;, TASK_ITEM);
 * 
 *         registerMultipleItem(TASKS, Tables.TASK);
 *         registerSingleItem(TASK_ITEM, Tables.TASK, TaskItem._ID);
 *     }
 * 
 *     &#064;Override
 *     protected DatabaseHelper getDatabaseHelper() {
 *         return new TaskDatabaseHelper(getContext());
 *     }
 * }
 * </pre>
 * 
 * @author danik
 * 
 */
public abstract class DatabaseContentProvider extends ContentProvider {

    private static final String CONTENT_TYPE_ITEM_PATTERN = "vnd.android.cursor.item/vnd.%s.%s";
    private static final String CONTENT_TYPE_DIR_PATTERN = "vnd.android.cursor.dir/vnd.%s.%s";

    private Map<Integer, Item> contentTypeRegister = new HashMap<Integer, Item>();

    private DatabaseHelper dbHelper;

    private UriMatcher uriMathcer = new UriMatcher(UriMatcher.NO_MATCH);

    @Override
    public boolean onCreate() {
        dbHelper = getDatabaseHelper();
        return dbHelper != null;
    }

    /**
     * {@inheritDoc}
     * <p>
     *         ,   
     * </p>
     * */
    @Override
    public String getType(Uri uri) {
        int code = match(uri);
        checkCodeExist(code);
        Item item = contentTypeRegister.get(code);
        String pattern = item.isSingle() ? CONTENT_TYPE_ITEM_PATTERN : CONTENT_TYPE_DIR_PATTERN;
        return String.format(pattern, getContext().getPackageName(), item.getTable());
    }

    private void checkCodeExist(int code) {
        if (!contentTypeRegister.containsKey(code)) {
            throw new IllegalArgumentException(String.format("Type with code '%s' is not registered!. "
                    + "Use method 'registerContentType' for registering a new content type", code));
        }
    }

    /** {@inheritDoc} */
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        final SelectionBuilder builder = buildSelection(uri);
        return builder.where(selection, selectionArgs).query(getReadableDb(), projection, sortOrder);
    }

    /** {@inheritDoc} */
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        String type = getType(uri);
        // some kind of name convention: type equals to table's name
        String table = type.substring(type.lastIndexOf('.') + 1);
        long rowId = getDb().insertOrThrow(table, null, values);
        Uri newUri = ContentUris.withAppendedId(uri, rowId);
        getContext().getContentResolver().notifyChange(newUri, null);
        return newUri;
    }

    /** {@inheritDoc} */
    @Override
    public int bulkInsert(Uri uri, ContentValues[] values) {
        int insertedCount = super.bulkInsert(uri, values);
        if (insertedCount != values.length) {
            throw new SQLException(String.format(
                    "Error invokinkg bulk insert: expected to insert %s, but there were inserted %s", values.length,
                    insertedCount));<commit_msg>notify query about updates<commit_after>package com.danikula.android16.core.content;

import java.util.HashMap;
import java.util.Map;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

/**
 *       .
 * <p>
 *  :
 * <ul>
 * <li>
 *         {@link #registerMultipleItem(int, String)}
 * {@link #registerSingleItem(int, String, String)}      {@link #getType(Uri)}</li>
 * <li>   CRUD!</li>
 * <li>to be continued....</li>
 * </ul>
 * </p>
 *     - :
 * 
 * <pre>
 * public class TasksContentProvider extends DatabaseContentProvider {
 * 
 *     private static final String CONTENT_AUTHORITY = &quot;com.ineed.taskprovider&quot;;
 *     static final Uri BASE_CONTENT_URI = Uri.parse(&quot;content://&quot; + CONTENT_AUTHORITY);
 * 
 *     private static final int TASKS = 100;
 *     private static final int TASK_ITEM = 101;
 * 
 *     public TasksContentProvider() {
 *         addMathURI(CONTENT_AUTHORITY, TaskItem.PATH, TASKS);
 *         addMathURI(CONTENT_AUTHORITY, TaskItem.PATH + &quot;/*&quot;, TASK_ITEM);
 * 
 *         registerMultipleItem(TASKS, Tables.TASK);
 *         registerSingleItem(TASK_ITEM, Tables.TASK, TaskItem._ID);
 *     }
 * 
 *     &#064;Override
 *     protected DatabaseHelper getDatabaseHelper() {
 *         return new TaskDatabaseHelper(getContext());
 *     }
 * }
 * </pre>
 * 
 * @author danik
 * 
 */
public abstract class DatabaseContentProvider extends ContentProvider {

    private static final String CONTENT_TYPE_ITEM_PATTERN = "vnd.android.cursor.item/vnd.%s.%s";
    private static final String CONTENT_TYPE_DIR_PATTERN = "vnd.android.cursor.dir/vnd.%s.%s";

    private Map<Integer, Item> contentTypeRegister = new HashMap<Integer, Item>();

    private DatabaseHelper dbHelper;

    private UriMatcher uriMathcer = new UriMatcher(UriMatcher.NO_MATCH);

    @Override
    public boolean onCreate() {
        dbHelper = getDatabaseHelper();
        return dbHelper != null;
    }

    /**
     * {@inheritDoc}
     * <p>
     *         ,   
     * </p>
     * */
    @Override
    public String getType(Uri uri) {
        int code = match(uri);
        checkCodeExist(code);
        Item item = contentTypeRegister.get(code);
        String pattern = item.isSingle() ? CONTENT_TYPE_ITEM_PATTERN : CONTENT_TYPE_DIR_PATTERN;
        return String.format(pattern, getContext().getPackageName(), item.getTable());
    }

    private void checkCodeExist(int code) {
        if (!contentTypeRegister.containsKey(code)) {
            throw new IllegalArgumentException(String.format("Type with code '%s' is not registered!. "
                    + "Use method 'registerContentType' for registering a new content type", code));
        }
    }

    /** {@inheritDoc} */
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        final SelectionBuilder builder = buildSelection(uri);
        Cursor cursor = builder.where(selection, selectionArgs).query(getReadableDb(), projection, sortOrder);
        // Register the contexts ContentResolver to be notified if the cursor result set changes.
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
    }

    /** {@inheritDoc} */
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        String type = getType(uri);
        // some kind of name convention: type equals to table's name
        String table = type.substring(type.lastIndexOf('.') + 1);
        long rowId = getDb().insertOrThrow(table, null, values);
        Uri newUri = ContentUris.withAppendedId(uri, rowId);
        getContext().getContentResolver().notifyChange(newUri, null);
        return newUri;
    }

    /** {@inheritDoc} */
    @Override
    public int bulkInsert(Uri uri, ContentValues[] values) {
        int insertedCount = super.bulkInsert(uri, values);
        if (insertedCount != values.length) {
            throw new SQLException(String.format(
                    "Error invokinkg bulk insert: expected to insert %s, but there were inserted %s", values.length,
                    insertedCount));
{'fraction_non_alphanumeric': 0.11908127208480565, 'fraction_numerical': 0.0028268551236749115, 'mean_word_length': 3.0375653827864957, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '6546228', 'n_tokens_mistral': 2514, 'n_tokens_neox': 2399, 'n_words': 580}