All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datacleaner.job.concurrent.TaskRunnable Maven / Gradle / Ivy

/**
 * DataCleaner (community edition)
 * Copyright (C) 2014 Neopost - Customer Information Management
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * 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 Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.datacleaner.job.concurrent;

import org.datacleaner.job.tasks.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Represents a Runnable wrapper for a task. A task might have also a listener
 * which will be invoked based on the outcome of the task.
 */
public final class TaskRunnable implements Runnable {

    private static final Logger logger = LoggerFactory.getLogger(TaskRunnable.class);
    private final Task _task;
    private final TaskListener _listener;

    public TaskRunnable(final Task task, final TaskListener listener) {
        if (task == null && listener == null) {
            throw new IllegalArgumentException("both task and listener cannot be null");
        }
        _task = task;
        _listener = listener;
    }

    @Override
    public void run() {
        if (_listener == null) {

            // execute without listener
            try {
                _task.execute();
            } catch (final Throwable t) {
                logger.warn("No TaskListener to inform of error!", t);
            }

        } else {

            // execute with listener
            _listener.onBegin(_task);
            try {
                if (_task != null) {
                    _task.execute();
                }
                _listener.onComplete(_task);
            } catch (final Throwable t) {
                _listener.onError(_task, t);
            }
        }
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append('[');
        if (_task != null) {
            sb.append("task=" + _task);
            if (_listener != null) {
                sb.append(',');
            }
        }
        if (_listener != null) {
            sb.append("listener=" + _listener);
        }
        sb.append(']');
        return sb.toString();
    }

    /**
     * Gets the task to run, or null if none exists
     *
     * @return the task to run, or null if none exists
     */
    public Task getTask() {
        return _task;
    }

    /**
     * Gets the task listener for the task, or null if none exists.
     *
     * @return the task listener for the task, or null if none exists.
     */
    public TaskListener getListener() {
        return _listener;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy