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

com.appslandia.javafx.tasks.AsyncTask Maven / Gradle / Ivy

There is a newer version: 3.8.1
Show newest version
// The MIT License (MIT)
// Copyright © 2015 AppsLandia. All rights reserved.

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package com.appslandia.javafx.tasks;

import java.lang.ref.WeakReference;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.appslandia.common.threading.DaemonThreadFactory;
import com.appslandia.common.utils.AssertUtils;
import com.appslandia.javafx.base.Progress;
import com.appslandia.javafx.base.ProgressImpl;

import javafx.concurrent.Task;
import javafx.stage.Window;

/**
 *
 * @author Loc Ha
 *
 */
public abstract class AsyncTask extends Task {

	protected final WeakReference callbacksRef;
	protected final int taskId;

	protected Object[] params;
	protected boolean progress;

	public AsyncTask(TaskCallbacks callbacks, int taskId) {
		this.callbacksRef = new WeakReference<>(callbacks);
		this.taskId = taskId;
	}

	public AsyncTask params(Object... params) {
		this.params = params;
		return this;
	}

	@Override
	protected void succeeded() {
		super.succeeded();
		if (progress) {
			removeProgress();
		}
		TaskCallbacks callbacks = callbacksRef.get();
		if (callbacks != null) {
			callbacks.onTaskCompleted(taskId, params, getValue());
		}
	}

	@Override
	protected void failed() {
		super.failed();
		if (progress) {
			removeProgress();
		}
		TaskCallbacks callbacks = callbacksRef.get();
		if (callbacks != null) {
			callbacks.onTaskFailed(taskId, params, getException());
		}
	}

	public void start() {
		TaskCallbacks callbacks = callbacksRef.get();
		AssertUtils.assertNotNull(callbacks);
		getExecutor().execute(this);
	}

	public void startProgress(Window owner) {
		this.progress = true;
		TaskCallbacks callbacks = callbacksRef.get();
		AssertUtils.assertNotNull(callbacks);

		showProgress(owner);
		getExecutor().execute(this);
	}

	protected void showProgress(Window owner) {
		new ProgressImpl().owner(owner).showAsync();
	}

	protected void removeProgress() {
		Progress.remove();
	}

	private static volatile ExecutorService executor;
	private static final Object MUTEX = new Object();
	private static final int DEFAULT_THREAD_POOL_SIZE = 5;

	public static ExecutorService getExecutor() {
		ExecutorService obj = executor;
		if (obj == null) {
			synchronized (MUTEX) {
				if ((obj = executor) == null) {
					executor = obj = Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE, new DaemonThreadFactory());
				}
			}
		}
		return obj;
	}

	public static void setExecutor(ExecutorService executor) {
		AssertUtils.assertNull(AsyncTask.executor);
		AsyncTask.executor = executor;
	}
}