<commit_before>package com.raizlabs.tasks;

import java.util.LinkedList;
import java.util.List;

/**
 * Class which represents Events for {@link RZAsyncTask}s.
 * @author <NAME>
 *
 * @param <Result> The Result type of the {@link RZAsyncTask}.
 */
public class RZAsyncTaskEvent<Progress, Result> {
	private List<RZAsyncTaskListener<Progress, Result>> listeners;
	
	/**
	 * Creates a new RZAsyncTaskEvent.
	 */
	public RZAsyncTaskEvent() {
		listeners = new LinkedList<RZAsyncTaskListener<Progress, Result>>();
	}
	
	/**
	 * Adds an RZAsyncTaskListener to be notified when AsyncTaskEvents happen.
	 * @param listener The listener to be notified.
	 */
	public void addListener(RZAsyncTaskListener<Progress, Result> listener) {
		listeners.add(listener);
	}
	
	/**
	 * Removes an RZEventListener so it will no longer be notified of these
	 * events.
	 * @param listener The RZAsyncTaskListener to be removed.
	 * @return True if the listener was removed, false if it wasn't found.
	 */
	public boolean removeListener(RZAsyncTaskListener<Progress, Result> listener) {
		return listeners.remove(listener);
	}
	
	public void raiseCancelled(Result result) {
		for (RZAsyncTaskListener<Progress, Result> listener : listeners) {
			listener.onCancelled(result);
		}
	}
	
	/**
	 * Raises the PreExecute event on all listeners.
	 * @see RZAsyncTask#doPreExecute()
	 * @param isInstant true if the task will be fast.
	 */
	public void raisePreExecute(boolean isInstant) {
		for (RZAsyncTaskListener<Progress, Result> listener : listeners) {
			listener.onPreExecute(isInstant);
		}
	}
	
	/**
	 * Raises the PostExecute event on all listeners.
	 * @param result The result arguments to be passed to the listeners.
	 */
	public void raisePostExecute(Result result) {
		for (RZAsyncTaskListener<Progress, Result> listener : listeners) {
			listener.onPostExecute(result);
		}
	}
	
	/**
	 * Publishes progress to all listeners.
	 * @param progress The progress to be passed to the listeners.
	 */
	public void publishProgress(Progress... progress) {
		for (RZAsyncTaskListener<Progress, Result> listener: listeners) {
			listener.onUpdateProgress(progress);<commit_msg>Added synchronization to AsyncTaskEvents for thread safety.<commit_after>package com.raizlabs.tasks;

import java.util.LinkedList;
import java.util.List;

/**
 * Class which represents Events for {@link RZAsyncTask}s.
 * @author <NAME>
 *
 * @param <Result> The Result type of the {@link RZAsyncTask}.
 */
public class RZAsyncTaskEvent<Progress, Result> {
	private List<RZAsyncTaskListener<Progress, Result>> listeners;

	/**
	 * Creates a new RZAsyncTaskEvent.
	 */
	public RZAsyncTaskEvent() {
		listeners = new LinkedList<RZAsyncTaskListener<Progress, Result>>();
	}

	/**
	 * Adds an RZAsyncTaskListener to be notified when AsyncTaskEvents happen.
	 * @param listener The listener to be notified.
	 */
	public void addListener(RZAsyncTaskListener<Progress, Result> listener) {
		synchronized (listeners) {
			listeners.add(listener);
		}
	}

	/**
	 * Removes an RZEventListener so it will no longer be notified of these
	 * events.
	 * @param listener The RZAsyncTaskListener to be removed.
	 * @return True if the listener was removed, false if it wasn't found.
	 */
	public boolean removeListener(RZAsyncTaskListener<Progress, Result> listener) {
		synchronized (listeners) {
			return listeners.remove(listener);
		}
	}

	public void raiseCancelled(Result result) {
		synchronized (listeners) {
			for (RZAsyncTaskListener<Progress, Result> listener : listeners) {
				listener.onCancelled(result);
			}
		}
	}

	/**
	 * Raises the PreExecute event on all listeners.
	 * @see RZAsyncTask#doPreExecute()
	 * @param isInstant true if the task will be fast.
	 */
	public void raisePreExecute(boolean isInstant) {
		synchronized (listeners) {
			for (RZAsyncTaskListener<Progress, Result> listener : listeners) {
				listener.onPreExecute(isInstant);
			}
		}
	}

	/**
	 * Raises the PostExecute event on all listeners.
	 * @param result The result arguments to be passed to the listeners.
	 */
	public void raisePostExecute(Result result) {
		synchronized (listeners) {
			for (RZAsyncTaskListener<Progress, Result> listener : listeners) {
				listener.onPostExecute(result);
			}
		}
	}

	/**
	 * Publishes progress to all listeners.
	 * @param progress The progress to be passed to the listeners.
	 */
	public void publishProgress(Progress... progress) {
		synchronized (listeners) {
			for (RZAsyncTaskListener<Progress, Result> listener: listeners) {
				listener.onUpdateProgress(progress);
			}
{'fraction_non_alphanumeric': 0.09896861970594689, 'fraction_numerical': 0.0, 'mean_word_length': 4.209142857142857, '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': '6609801', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1318, 'n_words': 405}