<commit_before>package com.github.ignition.core.tasks;

import java.util.concurrent.Callable;

import android.content.Context;
import android.os.AsyncTask;

/**
 * An extension to AsyncTask that removes some of the boilerplate that's typically involved. Some of
 * the things handled for you by this class are:
 * <ul>
 * <li>handles a {@link Context} reference for you, which is passed to all status callbacks</li>
 * <li>allows worker method to throw exceptions, which will be passed to
 * {@link #onTaskFailed(Context, Exception)}</li>
 * <li>allows re-using a task as a skeleton that delegates to different worker implementations via
 * {@link IgnitedAsyncTaskCallable}</li>
 * <li>allows a {@link Context} to register itself as a callback handler via
 * {@link IgnitedAsyncTaskContextHandler}</li>
 * <li>allows any arbitrary object to register itself as a callback handler via
 * {@link IgnitedAsyncTaskHandler}</li>
 * </ul>
 * <p>
 * Since this class keeps a reference to a Context (either directly or indirectly through a callback
 * handler), you MUST ensure that this reference is cleared when the Context gets destroyed. You can
 * handle Context and handler connection and disconnection using the {@link #connect} and
 * {@link #disconnect} methods. For Activities, a good place to call them is onCreate and onDestroy
 * respectively.
 * </p>
 * <p>
 * Please note that the callbacks added by this class will ONLY be called if the context reference
 * is valid. You can still receive AsyncTask's callbacks by overriding the callbacks defined in that
 * class.
 * </p>
 * 
 * @author <NAME>
 * 
 * @param <ContextT>
 * @param <ParameterT>
 * @param <ProgressT>
 * @param <ReturnT>
 */
public abstract class IgnitedAsyncTask<ContextT extends Context, ParameterT, ProgressT, ReturnT>
        extends AsyncTask<ParameterT, ProgressT, ReturnT> implements
        IgnitedAsyncTaskHandler<ContextT, ProgressT, ReturnT> {

    public interface IgnitedAsyncTaskCallable<ContextT extends Context, ParameterT, ProgressT, ReturnT> {
        public ReturnT call(IgnitedAsyncTask<ContextT, ParameterT, ProgressT, ReturnT> task)
                throws Exception;
    }

    private ContextT context;
    private IgnitedAsyncTaskContextHandler<ProgressT, ReturnT> contextHandler;
    private IgnitedAsyncTaskHandler<ContextT, ProgressT, ReturnT> delegateHandler;
    private IgnitedAsyncTaskCallable<ContextT, ParameterT, ProgressT, ReturnT> callable;

    private Exception error;

    public IgnitedAsyncTask() {
    }

    @SuppressWarnings("unchecked")
    public void connect(ContextT context) {
        this.context = context;
        if (context instanceof IgnitedAsyncTaskContextHandler) {
            this.contextHandler = (IgnitedAsyncTaskContextHandler<ProgressT, ReturnT>) context;
        } else if (context instanceof IgnitedAsyncTaskHandler) {
            this.delegateHandler = (IgnitedAsyncTaskHandler<ContextT, ProgressT, ReturnT>) context;
        }
    }

    public void connect(IgnitedAsyncTaskHandler<ContextT, ProgressT, ReturnT> handler) {
        this.delegateHandler = handler;
        this.context = handler.getContext();
    }

    public void disconnect() {
        this.contextHandler = null;
        this.delegateHandler = null;
        this.context = null;
    }

    @Override
    public ContextT getContext() {
        return context;
    }

    /**
     * If you rely on a valid context reference, override {@link #onTaskStarted(Context)} instead.
     */
    @Override
    protected void onPreExecute() {
        if (context != null) {
            onTaskStarted(context);
            if (contextHandler != null) {
                contextHandler.onTaskStarted();
            }
            if (delegateHandler != null) {
                delegateHandler.onTaskStarted(context);
            }
        }
    }

    /**
     * Override this method to prepare task execution. The default implementation does nothing.
     * 
     * @see {@link AsyncTask#onPreExecute}
     * @param context
     *            The most recent instance of the Context that executed this IgnitedAsyncTask
     */
    @Override
    public void onTaskStarted(ContextT context) {
    }

    /**
     * If you rely on a valid context reference, override {@link #onProgress} instead.
     */
    @Override
    protected void onProgressUpdate(ProgressT... values) {
        if (context != null) {
            onTaskProgress(context, values);
            if (contextHandler != null) {
                contextHandler.onTaskProgress(values);
            }
            if (delegateHandler != null) {
                delegateHandler.onTaskProgress(context, values);
            }
        }
    }

    /**
     * Override this method to update progress elements on the UI thread. The default implementation
     * does nothing.
     * 
     * @see {@link AsyncTask#publishProgress}
     * @see {@link AsyncTask#onProgressUpdate}
     * @param context
     *            The most recent instance of the Context that executed this IgnitedAsyncTask
     * @param progress
     *            the progress values
     */
    @Override
    public void onTaskProgress(ContextT context, ProgressT... progress) {
    }

    /**
     * Wrapper around {@link AsyncTask#execute} that does not take a varargs argument, but a single
     * parameter of type {@code ParameterT}. This is useful if you only ever need to pass one
     * argument to your tasks, but want to avoid running into compiler warning if the argument is
     * itself parameterized (i.e. a generic type). However, for a single argument, this is actually
     * typesafe.
     * 
     * @param arg
     *            the single argument you want to pass to the task's {@link #run} method.
     * @return
     */
    @SuppressWarnings("unchecked")
    public AsyncTask<ParameterT, ProgressT, ReturnT> execute(ParameterT arg) {
        return super.execute(arg);
    }

    /**
     * No, you want to override {@link #run(Object...)} instead.
     */
    @Override
    protected final ReturnT doInBackground(ParameterT... params) {
        ReturnT result = null;<commit_msg>Also overload execute() without arguments to prevent compiler warnings when ParameterT is a generic type<commit_after>package com.github.ignition.core.tasks;

import java.util.concurrent.Callable;

import android.content.Context;
import android.os.AsyncTask;

/**
 * An extension to AsyncTask that removes some of the boilerplate that's typically involved. Some of
 * the things handled for you by this class are:
 * <ul>
 * <li>handles a {@link Context} reference for you, which is passed to all status callbacks</li>
 * <li>allows worker method to throw exceptions, which will be passed to
 * {@link #onTaskFailed(Context, Exception)}</li>
 * <li>allows re-using a task as a skeleton that delegates to different worker implementations via
 * {@link IgnitedAsyncTaskCallable}</li>
 * <li>allows a {@link Context} to register itself as a callback handler via
 * {@link IgnitedAsyncTaskContextHandler}</li>
 * <li>allows any arbitrary object to register itself as a callback handler via
 * {@link IgnitedAsyncTaskHandler}</li>
 * </ul>
 * <p>
 * Since this class keeps a reference to a Context (either directly or indirectly through a callback
 * handler), you MUST ensure that this reference is cleared when the Context gets destroyed. You can
 * handle Context and handler connection and disconnection using the {@link #connect} and
 * {@link #disconnect} methods. For Activities, a good place to call them is onCreate and onDestroy
 * respectively.
 * </p>
 * <p>
 * Please note that the callbacks added by this class will ONLY be called if the context reference
 * is valid. You can still receive AsyncTask's callbacks by overriding the callbacks defined in that
 * class.
 * </p>
 * 
 * @author <NAME>
 * 
 * @param <ContextT>
 * @param <ParameterT>
 * @param <ProgressT>
 * @param <ReturnT>
 */
public abstract class IgnitedAsyncTask<ContextT extends Context, ParameterT, ProgressT, ReturnT>
        extends AsyncTask<ParameterT, ProgressT, ReturnT> implements
        IgnitedAsyncTaskHandler<ContextT, ProgressT, ReturnT> {

    public interface IgnitedAsyncTaskCallable<ContextT extends Context, ParameterT, ProgressT, ReturnT> {
        public ReturnT call(IgnitedAsyncTask<ContextT, ParameterT, ProgressT, ReturnT> task)
                throws Exception;
    }

    private ContextT context;
    private IgnitedAsyncTaskContextHandler<ProgressT, ReturnT> contextHandler;
    private IgnitedAsyncTaskHandler<ContextT, ProgressT, ReturnT> delegateHandler;
    private IgnitedAsyncTaskCallable<ContextT, ParameterT, ProgressT, ReturnT> callable;

    private Exception error;

    public IgnitedAsyncTask() {
    }

    @SuppressWarnings("unchecked")
    public void connect(ContextT context) {
        this.context = context;
        if (context instanceof IgnitedAsyncTaskContextHandler) {
            this.contextHandler = (IgnitedAsyncTaskContextHandler<ProgressT, ReturnT>) context;
        } else if (context instanceof IgnitedAsyncTaskHandler) {
            this.delegateHandler = (IgnitedAsyncTaskHandler<ContextT, ProgressT, ReturnT>) context;
        }
    }

    public void connect(IgnitedAsyncTaskHandler<ContextT, ProgressT, ReturnT> handler) {
        this.delegateHandler = handler;
        this.context = handler.getContext();
    }

    public void disconnect() {
        this.contextHandler = null;
        this.delegateHandler = null;
        this.context = null;
    }

    @Override
    public ContextT getContext() {
        return context;
    }

    /**
     * If you rely on a valid context reference, override {@link #onTaskStarted(Context)} instead.
     */
    @Override
    protected void onPreExecute() {
        if (context != null) {
            onTaskStarted(context);
            if (contextHandler != null) {
                contextHandler.onTaskStarted();
            }
            if (delegateHandler != null) {
                delegateHandler.onTaskStarted(context);
            }
        }
    }

    /**
     * Override this method to prepare task execution. The default implementation does nothing.
     * 
     * @see {@link AsyncTask#onPreExecute}
     * @param context
     *            The most recent instance of the Context that executed this IgnitedAsyncTask
     */
    @Override
    public void onTaskStarted(ContextT context) {
    }

    /**
     * If you rely on a valid context reference, override {@link #onProgress} instead.
     */
    @Override
    protected void onProgressUpdate(ProgressT... values) {
        if (context != null) {
            onTaskProgress(context, values);
            if (contextHandler != null) {
                contextHandler.onTaskProgress(values);
            }
            if (delegateHandler != null) {
                delegateHandler.onTaskProgress(context, values);
            }
        }
    }

    /**
     * Override this method to update progress elements on the UI thread. The default implementation
     * does nothing.
     * 
     * @see {@link AsyncTask#publishProgress}
     * @see {@link AsyncTask#onProgressUpdate}
     * @param context
     *            The most recent instance of the Context that executed this IgnitedAsyncTask
     * @param progress
     *            the progress values
     */
    @Override
    public void onTaskProgress(ContextT context, ProgressT... progress) {
    }

    /**
     * Wrapper around {@link AsyncTask#execute} that does not take a varargs argument, but a single
     * parameter of type {@code ParameterT}. This is useful if you only ever need to pass one
     * argument to your tasks, and want to avoid running into compiler warning if the argument type
     * is itself parameterized (i.e. a generic type). However, for a single argument, this is
     * actually typesafe.
     * 
     * @param arg
     *            the single argument you want to pass to the task's {@link #run} method.
     */
    @SuppressWarnings("unchecked")
    public AsyncTask<ParameterT, ProgressT, ReturnT> execute(ParameterT arg) {
        return super.execute(arg);
    }

    /**
     * Wrapper around {@link AsyncTask#execute} that does not take any arguments. This is useful if
     * arguments to your tasks are nullable, and you want to avoid running into compiler warnings if
     * the argument type is itself parameterized (i.e. a generic type).
     */
    @SuppressWarnings("unchecked")
    public AsyncTask<ParameterT, ProgressT, ReturnT> execute() {
        return super.execute();
    }

    /**
     * No, you want to override {@link #run(Object...)} instead.
     */
    @Override
    protected final ReturnT doInBackground(ParameterT... params) {
        ReturnT result = null;
{'fraction_non_alphanumeric': 0.08456891817682592, 'fraction_numerical': 0.0, 'mean_word_length': 2.9152334152334154, 'pattern_counts': {'":': 0, '<': 70, '<?xml version=': 0, '>': 70, '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': '5111244', 'n_tokens_mistral': 3315, 'n_tokens_neox': 3230, 'n_words': 1185}