1 | // Jomic - a viewer for comic book archives. |
2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
3 | // |
4 | // This program is free software: you can redistribute it and/or modify |
5 | // it under the terms of the GNU General Public License as published by |
6 | // the Free Software Foundation, either version 3 of the License, or |
7 | // (at your option) any later version. |
8 | // |
9 | // This program is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | // GNU General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU General Public License |
15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | package net.sf.jomic.tools; |
17 | |
18 | import java.util.HashMap; |
19 | import java.util.Map; |
20 | |
21 | import org.apache.commons.logging.Log; |
22 | import org.apache.commons.logging.LogFactory; |
23 | |
24 | /** |
25 | * A task composed of sub-tasks. |
26 | * |
27 | * @author Thomas Aglassinger |
28 | */ |
29 | public class NestedTask extends AbstractTask implements ProgressChangeListener |
30 | { |
31 | private boolean afterErrorContinueWithNextTask; |
32 | private Log logger; |
33 | private long[] subTaskStartProgresses; |
34 | private Map subTaskToInvocationIndexMap; |
35 | private Task[] subTasks; |
36 | |
37 | public NestedTask(Task[] newSubTasks) { |
38 | this(newSubTasks, false); |
39 | } |
40 | |
41 | public NestedTask(Task[] newSubTasks, boolean newAfterErrorContinueWithNextTask) { |
42 | this(); |
43 | assert newSubTasks != null; |
44 | int subTaskCount = newSubTasks.length; |
45 | long nestedMaxProgress = 0; |
46 | int stepCount = subTaskCount; |
47 | |
48 | subTasks = new Task[subTaskCount]; |
49 | afterErrorContinueWithNextTask = newAfterErrorContinueWithNextTask; |
50 | System.arraycopy(newSubTasks, 0, subTasks, 0, subTaskCount); |
51 | subTaskStartProgresses = new long[stepCount]; |
52 | subTaskToInvocationIndexMap = new HashMap(); |
53 | for (int i = 0; i < stepCount; i += 1) { |
54 | Task currentTask = newSubTasks[i]; |
55 | |
56 | assert currentTask != null; |
57 | currentTask.addProgressChangeListener(this); |
58 | |
59 | Object previousItem = subTaskToInvocationIndexMap.put(currentTask, new Integer(i)); |
60 | |
61 | assert previousItem == null; |
62 | subTaskStartProgresses[i] = nestedMaxProgress; |
63 | nestedMaxProgress += currentTask.getMaxProgress(); |
64 | } |
65 | setMaxProgress(nestedMaxProgress); |
66 | } |
67 | |
68 | private NestedTask() { |
69 | super(); |
70 | logger = LogFactory.getLog(NestedTask.class); |
71 | } |
72 | |
73 | /** |
74 | * Listen to progress changes in sub-tasks and update the total progress of the nested task |
75 | * accordingly. |
76 | */ |
77 | public void progressChanged(Task source) { |
78 | assert source != null; |
79 | |
80 | Integer subTaskInvocationIndexAsInteger = (Integer) subTaskToInvocationIndexMap.get(source); |
81 | |
82 | assert subTaskInvocationIndexAsInteger != null; |
83 | int subTaskInvocationIndex = subTaskInvocationIndexAsInteger.intValue(); |
84 | long subProgress = source.getProgress(); |
85 | |
86 | setProgress(subTaskStartProgresses[subTaskInvocationIndex] + subProgress); |
87 | } |
88 | |
89 | /** |
90 | * Run the sub-tasks. |
91 | */ |
92 | public void start() |
93 | throws Exception { |
94 | for (int i = 0; !isInterrupted() && (i < subTasks.length); i += 1) { |
95 | Task subTask = subTasks[i]; |
96 | |
97 | if (afterErrorContinueWithNextTask) { |
98 | try { |
99 | subTask.start(); |
100 | } catch (Throwable error) { |
101 | logger.warn("ignoring error and continuing with next task", error); |
102 | } finally { |
103 | subTask.removeProgressChangeListener(this); |
104 | } |
105 | } else { |
106 | try { |
107 | subTask.start(); |
108 | } finally { |
109 | subTask.removeProgressChangeListener(this); |
110 | } |
111 | } |
112 | } |
113 | } |
114 | } |