<commit_before>/*
 * Source file of the Remove Duplicates project.
 * Copyright (c) 2016. All Rights Reserved.
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/2.0
 *
 * Contributors can be contacted by electronic mail via the project Web pages:
 *
 * https://github.com/pnemonic78/RemoveDuplicates
 *
 * Contributor(s):
 *   <NAME>
 *
 */
package com.github.duplicates;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CancellationException;

/**
 * Provider of duplicate items.
 *
 * @author moshe.w
 */
public abstract class DuplicateProvider<T extends DuplicateItem> {

    private final Context context;
    private DuplicateProviderListener<T, DuplicateProvider<T>> listener;
    private boolean cancelled;

    protected DuplicateProvider(Context context) {
        this.context = context;
    }

    public DuplicateProviderListener<T, DuplicateProvider<T>> getListener() {
        return listener;
    }

    public void setListener(DuplicateProviderListener<T, DuplicateProvider<T>> listener) {
        this.listener = listener;
    }

    protected Context getContext() {
        return context;
    }

    protected abstract Uri getContentUri();

    protected String[] getCursorProjection() {
        return null;
    }

    protected String getCursorSelection() {
        return null;
    }

    public abstract T createItem(Cursor cursor);

    public abstract void populateItem(Cursor cursor, T item);

    /**
     * Get the items from the system provider.
     *
     * @return the list of items.
     * @throws CancellationException if the provider has been cancelled.
     */
    public List<T> getItems() throws CancellationException {
        if (isCancelled()) {
            throw new CancellationException();
        }
        List<T> items = new ArrayList<>();
        Context context = getContext();
        ContentResolver cr = context.getContentResolver();

        Cursor cursor = cr.query(getContentUri(), getCursorProjection(), null, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                T item;
                do {
                    if (isCancelled()) {
                        break;
                    }
                    item = createItem(cursor);
                    populateItem(cursor, item);
                    items.add(item);
                } while (cursor.moveToNext());
            }
            cursor.close();
        }

        return items;
    }

    /**
     * Fetch the items from the system provider into the listener.
     *
     * @return the list of items.
     * @throws CancellationException if the provider has been cancelled.
     */
    public void fetchItems() throws CancellationException {
        if (isCancelled()) {
            throw new CancellationException();
        }
        DuplicateProviderListener<T, DuplicateProvider<T>> listener = getListener();
        if (listener == null) {
            return;
        }
        Context context = getContext();
        ContentResolver cr = context.getContentResolver();

        Uri contentUri = getContentUri();
        if (contentUri == null) {
            return;
        }
        Cursor cursor = cr.query(getContentUri(), getCursorProjection(), getCursorSelection(), null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                T item;
                int count = 0;
                do {
                    if (isCancelled()) {<commit_msg>cursor order.<commit_after>/*
 * Source file of the Remove Duplicates project.
 * Copyright (c) 2016. All Rights Reserved.
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/2.0
 *
 * Contributors can be contacted by electronic mail via the project Web pages:
 *
 * https://github.com/pnemonic78/RemoveDuplicates
 *
 * Contributor(s):
 *   <NAME>
 *
 */
package com.github.duplicates;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CancellationException;

/**
 * Provider of duplicate items.
 *
 * @author moshe.w
 */
public abstract class DuplicateProvider<T extends DuplicateItem> {

    private final Context context;
    private DuplicateProviderListener<T, DuplicateProvider<T>> listener;
    private boolean cancelled;

    protected DuplicateProvider(Context context) {
        this.context = context;
    }

    public DuplicateProviderListener<T, DuplicateProvider<T>> getListener() {
        return listener;
    }

    public void setListener(DuplicateProviderListener<T, DuplicateProvider<T>> listener) {
        this.listener = listener;
    }

    protected Context getContext() {
        return context;
    }

    protected abstract Uri getContentUri();

    protected String[] getCursorProjection() {
        return null;
    }

    protected String getCursorSelection() {
        return null;
    }

    protected String getCursorOrder() {
        return null;
    }

    public abstract T createItem(Cursor cursor);

    public abstract void populateItem(Cursor cursor, T item);

    /**
     * Get the items from the system provider.
     *
     * @return the list of items.
     * @throws CancellationException if the provider has been cancelled.
     */
    public List<T> getItems() throws CancellationException {
        if (isCancelled()) {
            throw new CancellationException();
        }
        List<T> items = new ArrayList<>();
        Context context = getContext();
        ContentResolver cr = context.getContentResolver();

        Cursor cursor = cr.query(getContentUri(), getCursorProjection(), null, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                T item;
                do {
                    if (isCancelled()) {
                        break;
                    }
                    item = createItem(cursor);
                    populateItem(cursor, item);
                    items.add(item);
                } while (cursor.moveToNext());
            }
            cursor.close();
        }

        return items;
    }

    /**
     * Fetch the items from the system provider into the listener.
     *
     * @return the list of items.
     * @throws CancellationException if the provider has been cancelled.
     */
    public void fetchItems() throws CancellationException {
        if (isCancelled()) {
            throw new CancellationException();
        }
        DuplicateProviderListener<T, DuplicateProvider<T>> listener = getListener();
        if (listener == null) {
            return;
        }
        Context context = getContext();
        ContentResolver cr = context.getContentResolver();

        Uri contentUri = getContentUri();
        if (contentUri == null) {
            return;
        }
        Cursor cursor = cr.query(getContentUri(), getCursorProjection(), getCursorSelection(), null, getCursorOrder());
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                T item;
                int count = 0;
                do {
                    if (isCancelled()) {
{'fraction_non_alphanumeric': 0.09437340153452685, 'fraction_numerical': 0.002813299232736573, 'mean_word_length': 2.262828535669587, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '5714494', 'n_tokens_mistral': 2046, 'n_tokens_neox': 1956, 'n_words': 608}