<commit_before>package org.cytoscape.work.internal.sync;

/*
 * #%L
 * org.cytoscape.work-impl
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2006 - 2013 The Cytoscape Consortium
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 2.1 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */


import java.util.Map;

import org.cytoscape.work.AbstractTaskManager;
import org.cytoscape.work.ObservableTask;
import org.cytoscape.work.SynchronousTaskManager;
import org.cytoscape.work.Task;
import org.cytoscape.work.TaskFactory;
import org.cytoscape.work.TaskIterator;
import org.cytoscape.work.TaskObserver;
import org.cytoscape.work.TunableRecorder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Uses Swing components to create a user interface for the <code>Task</code>.
 *
 * This will not work if the application is running in headless mode.
 */
public class SyncTaskManager extends AbstractTaskManager<Object, Map<String, Object>> implements
		SynchronousTaskManager<Object> {

	private static final Logger logger = LoggerFactory.getLogger(SyncTaskManager.class);


	private final SyncTunableMutator<?> syncTunableMutator;

	/**
	 * Construct with default behavior.
	 */
	public SyncTaskManager(final SyncTunableMutator<?> tunableMutator) {
		super(tunableMutator);
		this.syncTunableMutator = tunableMutator;
	}


	@Override 
	public void setExecutionContext(final Map<String,Object> o) {
		syncTunableMutator.setConfigurationContext(o);
	}


	@Override 
	public Object getConfiguration(TaskFactory factory, Object context) {
		throw new UnsupportedOperationException("There is no configuration available for a SyncrhonousTaskManager");	
	}

	@Override
	public void execute(final TaskIterator taskIterator, TaskObserver observer) {
		// System.out.println("SyncTaskManager.execute");
		final LoggingTaskMonitor taskMonitor = new LoggingTaskMonitor();
		
		try {
			while (taskIterator.hasNext()) {
				final Task task = taskIterator.next();
				taskMonitor.setTask(task);

				if (!displayTunables(task))
					return;

				task.run(taskMonitor);

				if (task instanceof ObservableTask && observer != null) {
					observer.taskFinished((ObservableTask)task);
				} 
			}
			if (observer != null)
				observer.allFinished();

		} catch (Exception exception) {
			taskMonitor.showException(exception);
		}
	}

	@Override
	public void execute(final TaskIterator taskIterator) {
		execute(taskIterator, null);
	}
<commit_msg>Added support for cancelled() and failed() to task observers<commit_after>package org.cytoscape.work.internal.sync;

/*
 * #%L
 * org.cytoscape.work-impl
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2006 - 2013 The Cytoscape Consortium
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 2.1 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */


import java.util.Map;

import org.cytoscape.work.AbstractTaskManager;
import org.cytoscape.work.ObservableTask;
import org.cytoscape.work.SynchronousTaskManager;
import org.cytoscape.work.Task;
import org.cytoscape.work.TaskFactory;
import org.cytoscape.work.TaskIterator;
import org.cytoscape.work.TaskObserver;
import org.cytoscape.work.TunableRecorder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Uses Swing components to create a user interface for the <code>Task</code>.
 *
 * This will not work if the application is running in headless mode.
 */
public class SyncTaskManager extends AbstractTaskManager<Object, Map<String, Object>> implements
		SynchronousTaskManager<Object> {

	private static final Logger logger = LoggerFactory.getLogger(SyncTaskManager.class);


	private final SyncTunableMutator<?> syncTunableMutator;

	/**
	 * Construct with default behavior.
	 */
	public SyncTaskManager(final SyncTunableMutator<?> tunableMutator) {
		super(tunableMutator);
		this.syncTunableMutator = tunableMutator;
	}


	@Override 
	public void setExecutionContext(final Map<String,Object> o) {
		syncTunableMutator.setConfigurationContext(o);
	}


	@Override 
	public Object getConfiguration(TaskFactory factory, Object context) {
		throw new UnsupportedOperationException("There is no configuration available for a SyncrhonousTaskManager");	
	}

	@Override
	public void execute(final TaskIterator taskIterator, TaskObserver observer) {
		// System.out.println("SyncTaskManager.execute");
		final LoggingTaskMonitor taskMonitor = new LoggingTaskMonitor();
		
        Task task = null;
		try {
			while (taskIterator.hasNext()) {
				task = taskIterator.next();
				taskMonitor.setTask(task);

				if (!displayTunables(task)) {
                    if (observer != null) observer.cancelled(task);
					return;
                }

				task.run(taskMonitor);

				if (task instanceof ObservableTask && observer != null) {
					observer.taskFinished((ObservableTask)task);
				} 
			}
			if (observer != null) observer.allFinished();

		} catch (Exception exception) {
			taskMonitor.showException(exception);
            if (observer != null && task != null) observer.failed(task, exception);
		}
	}

	@Override
	public void execute(final TaskIterator taskIterator) {
		execute(taskIterator, null);
	}

{'fraction_non_alphanumeric': 0.09495878249841472, 'fraction_numerical': 0.004438807863031071, 'mean_word_length': 4.266277128547579, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, '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': '1376226', 'n_tokens_mistral': 2005, 'n_tokens_neox': 1800, 'n_words': 548}