<commit_before>package com.xtremelabs.robolectric.shadows;

import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import android.os.AsyncTask;
import android.os.ShadowAsyncTaskBridge;

import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
import com.xtremelabs.robolectric.internal.RealObject;

@Implements(AsyncTask.class)
public class ShadowAsyncTask<Params, Progress, Result> {

	@RealObject private AsyncTask<Params, Progress, Result> realAsyncTask;
    
	private final FutureTask<Result> future;
	private final BackgroundWorker worker;
	
	public ShadowAsyncTask() {
		worker = new BackgroundWorker();
		future = new FutureTask<Result>(worker) {
        	@Override
        	protected void done() {
				try {
					final Result result = get();
					Robolectric.getUiThreadScheduler().post(new Runnable() {
						@Override public void run() {
							getBridge().onPostExecute(result);
						}
					});
				} catch (CancellationException e) {
					Robolectric.getUiThreadScheduler().post(new Runnable() {
						@Override public void run() {
							getBridge().onCancelled();
						}
					});
					return;
				} catch (InterruptedException e) {
					// Ignore.
				} catch (Throwable t) {
					throw new RuntimeException("An error occured while executing doInBackground()",
							t.getCause());
				}
        	}
        };
	}

//    public android.os.AsyncTask.Status getStatus() {
//        return null;
//    }

	@Implementation
    public boolean isCancelled() {
        return future.isCancelled();
    }

    @Implementation
    public boolean cancel(boolean mayInterruptIfRunning) {
        return future.cancel(mayInterruptIfRunning);
    }

    public Result get() throws InterruptedException, ExecutionException {
        return future.get();
    }

    public Result get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        return future.get(timeout, unit);
    }

    @Implementation
    public AsyncTask<Params, Progress, Result> execute(final Params... params) {
        getBridge().onPreExecute();
        
        worker.params = params;

        Robolectric.getBackgroundScheduler().post(new Runnable() {
            @Override public void run() {
            	future.run();
            }
        });
        
        return null;
    }

    /**<commit_msg>adding @Implementation to the get() methods.<commit_after>package com.xtremelabs.robolectric.shadows;

import android.os.AsyncTask;
import android.os.ShadowAsyncTaskBridge;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
import com.xtremelabs.robolectric.internal.RealObject;

import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@Implements(AsyncTask.class)
public class ShadowAsyncTask<Params, Progress, Result> {

	@RealObject private AsyncTask<Params, Progress, Result> realAsyncTask;
    
	private final FutureTask<Result> future;
	private final BackgroundWorker worker;
	
	public ShadowAsyncTask() {
		worker = new BackgroundWorker();
		future = new FutureTask<Result>(worker) {
        	@Override
        	protected void done() {
				try {
					final Result result = get();
					Robolectric.getUiThreadScheduler().post(new Runnable() {
						@Override public void run() {
							getBridge().onPostExecute(result);
						}
					});
				} catch (CancellationException e) {
					Robolectric.getUiThreadScheduler().post(new Runnable() {
						@Override public void run() {
							getBridge().onCancelled();
						}
					});
					return;
				} catch (InterruptedException e) {
					// Ignore.
				} catch (Throwable t) {
					throw new RuntimeException("An error occured while executing doInBackground()",
							t.getCause());
				}
        	}
        };
	}

	@Implementation
    public boolean isCancelled() {
        return future.isCancelled();
    }

    @Implementation
    public boolean cancel(boolean mayInterruptIfRunning) {
        return future.cancel(mayInterruptIfRunning);
    }

    @Implementation
    public Result get() throws InterruptedException, ExecutionException {
        return future.get();
    }

    @Implementation
    public Result get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        return future.get(timeout, unit);
    }

    @Implementation
    public AsyncTask<Params, Progress, Result> execute(final Params... params) {
        getBridge().onPreExecute();
        
        worker.params = params;

        Robolectric.getBackgroundScheduler().post(new Runnable() {
            @Override public void run() {
            	future.run();
            }
        });
        
        return null;
    }

    /**
{'fraction_non_alphanumeric': 0.1015016156624216, 'fraction_numerical': 0.0, 'mean_word_length': 3.1795075456711674, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '4442478', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1446, 'n_words': 314}