EMMA Coverage Report (generated Sun Apr 20 22:38:01 CEST 2008)
[all classes][net.sf.jomic.tools]

COVERAGE SUMMARY FOR SOURCE FILE [NestedTask.java]

nameclass, %method, %block, %line, %
NestedTask.java100% (1/1)100% (6/6)74%  (163/220)83%  (37.2/45)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class NestedTask100% (1/1)100% (6/6)74%  (163/220)83%  (37.2/45)
start (): void 100% (1/1)48%  (27/56)54%  (6.5/12)
NestedTask (): void 100% (1/1)64%  (9/14)88%  (2.6/3)
progressChanged (Task): void 100% (1/1)78%  (29/37)89%  (6.2/7)
<static initializer> 100% (1/1)80%  (12/15)80%  (0.8/1)
NestedTask (Task [], boolean): void 100% (1/1)87%  (81/93)95%  (19.1/20)
NestedTask (Task []): void 100% (1/1)100% (5/5)100% (2/2)

1// Jomic - a viewer for comic book archives.
2// Copyright (C) 2004-2008 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/>.
16package net.sf.jomic.tools;
17 
18import java.util.HashMap;
19import java.util.Map;
20 
21import net.sf.wraplog.Logger;
22 
23/**
24 *  A task composed of sub-tasks.
25 *
26 * @author    Thomas Aglassinger
27 */
28public class NestedTask extends AbstractTask implements ProgressChangeListener
29{
30    private boolean afterErrorContinueWithNextTask;
31    private Logger logger;
32    private long[] subTaskStartProgresses;
33    private Map subTaskToInvocationIndexMap;
34    private Task[] subTasks;
35 
36    public NestedTask(Task[] newSubTasks) {
37        this(newSubTasks, false);
38    }
39 
40    public NestedTask(Task[] newSubTasks, boolean newAfterErrorContinueWithNextTask) {
41        this();
42        assert newSubTasks != null;
43        int subTaskCount = newSubTasks.length;
44        long nestedMaxProgress = 0;
45        int stepCount = subTaskCount;
46 
47        subTasks = new Task[subTaskCount];
48        afterErrorContinueWithNextTask = newAfterErrorContinueWithNextTask;
49        System.arraycopy(newSubTasks, 0, subTasks, 0, subTaskCount);
50        subTaskStartProgresses = new long[stepCount];
51        subTaskToInvocationIndexMap = new HashMap();
52        for (int i = 0; i < stepCount; i += 1) {
53            Task currentTask = newSubTasks[i];
54 
55            assert currentTask != null;
56            currentTask.addProgressChangeListener(this);
57 
58            Object previousItem = subTaskToInvocationIndexMap.put(currentTask, new Integer(i));
59 
60            assert previousItem == null;
61            subTaskStartProgresses[i] = nestedMaxProgress;
62            nestedMaxProgress += currentTask.getMaxProgress();
63        }
64        setMaxProgress(nestedMaxProgress);
65    }
66 
67    private NestedTask() {
68        super();
69        logger = Logger.getLogger(NestedTask.class);
70    }
71 
72    /**
73     *  Listen to progress changes in sub-tasks and update the total progress of the nested task
74     *  accordingly.
75     */
76    public void progressChanged(Task source) {
77        assert source != null;
78 
79        Integer subTaskInvocationIndexAsInteger = (Integer) subTaskToInvocationIndexMap.get(source);
80 
81        assert subTaskInvocationIndexAsInteger != null;
82        int subTaskInvocationIndex = subTaskInvocationIndexAsInteger.intValue();
83        long subProgress = source.getProgress();
84 
85        setProgress(subTaskStartProgresses[subTaskInvocationIndex] + subProgress);
86    }
87 
88    /**
89     *  Run the sub-tasks.
90     */
91    public void start()
92        throws Exception {
93        for (int i = 0; !isInterrupted() && (i < subTasks.length); i += 1) {
94            Task subTask = subTasks[i];
95 
96            if (afterErrorContinueWithNextTask) {
97                try {
98                    subTask.start();
99                } catch (Throwable error) {
100                    logger.warn("ignoring error and continuing with next task", error);
101                } finally {
102                    subTask.removeProgressChangeListener(this);
103                }
104            } else {
105                try {
106                    subTask.start();
107                } finally {
108                    subTask.removeProgressChangeListener(this);
109                }
110            }
111        }
112    }
113}

[all classes][net.sf.jomic.tools]
EMMA 2.0.4217 (C) Vladimir Roubtsov