001/** 002 * Copyright (C) 2014 Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/ 003 * 004 * This file is part of Dicoogle/dicoogle-sdk. 005 * 006 * Dicoogle/dicoogle-sdk is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Dicoogle/dicoogle-sdk is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Dicoogle. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package pt.ua.dicoogle.sdk.task; 020 021import java.util.ArrayList; 022import java.util.concurrent.Callable; 023import java.util.concurrent.FutureTask; 024 025/** An entity for describing an asynchronous task in Dicoogle. 026 * 027 * @param <Type> the return type of the FutureTask 028 * 029 * @author psytek 030 */ 031public class Task<Type> extends FutureTask<Type> { 032 033 private String taskName = "unnamed task"; 034 private Callable callable; 035 ArrayList<Runnable> toRunWhenComplete; 036 037 public Task(Callable<Type> c){ 038 super(c); 039 callable = c; 040 toRunWhenComplete = new ArrayList<>();//we could lazy initialize this in onCompletion 041 } 042 043 public Task(String name, Callable<Type> c){ 044 super(c); 045 taskName = name; 046 toRunWhenComplete = new ArrayList<>();//we could lazy initialize this in onCompletion 047 } 048 049 @Override 050 protected void set(Type ret){ 051 super.set(ret); 052 for(Runnable r : toRunWhenComplete){ 053 r.run(); 054 } 055 } 056 057 public void onCompletion(Runnable r){ 058 toRunWhenComplete.add(r); 059 } 060 061 /** Gets the task's name 062 * @return a task name, for presentation purposes 063 */ 064 public String getName(){return this.taskName;} 065 066 /** Sets the task's name 067 * @param name the new task's name, for presentation purposes 068 */ 069 public void setName(String name){this.taskName = name;} 070 071 /** Gets the task's progress 072 * @return the task's progress from 0 to 1, or -1 if the task is unbounded 073 */ 074 public float getProgress(){ 075 if (callable instanceof ProgressCallable){ 076 return ((ProgressCallable)this.callable).getProgress(); 077 } 078 return -1; 079 } 080 081} 082