|
1 |
| -package net.lecousin.framework.progress; |
2 |
| - |
3 |
| -import java.util.ArrayList; |
4 |
| -import java.util.List; |
5 |
| - |
6 |
| -import net.lecousin.framework.concurrent.synch.JoinPoint; |
7 |
| - |
8 |
| -/** Implementation of WorkProgress, composed of sub-WorkProgress. */ |
9 |
| -public class MultiTaskProgress extends WorkProgressImpl implements WorkProgress.MultiTask { |
10 |
| - |
11 |
| - /** Constructor. */ |
12 |
| - public MultiTaskProgress(String text) { |
13 |
| - super(0, text); |
14 |
| - } |
15 |
| - |
16 |
| - protected ArrayList<WorkProgress> tasks = new ArrayList<>(); |
17 |
| - protected JoinPoint<Exception> jp = null; |
18 |
| - |
19 |
| - /** Create a sub-progress for the given amount of work (this amount is added to the total amount to be done). */ |
20 |
| - public WorkProgress createTaskProgress(long amount, String text) { |
21 |
| - this.amount += amount; |
22 |
| - SubWorkProgress task = new SubWorkProgress(this, amount, amount, text); |
23 |
| - synchronized (tasks) { |
24 |
| - tasks.add(task); |
25 |
| - if (jp != null) jp.addToJoin(task.getSynch()); |
26 |
| - } |
27 |
| - return task; |
28 |
| - } |
29 |
| - |
30 |
| - /** Add the given sub-progress as a sub-task for the given amount of work (this amount is added to the total amount to be done). */ |
31 |
| - public void addTask(WorkProgress task, long amount) { |
32 |
| - this.amount += amount; |
33 |
| - synchronized (tasks) { |
34 |
| - tasks.add(task); |
35 |
| - if (jp != null) jp.addToJoin(task.getSynch()); |
36 |
| - } |
37 |
| - WorkProgressUtil.propagateToParent(task, this, amount); |
38 |
| - } |
39 |
| - |
40 |
| - /** Signal that no more task will be added, meaning that once all current sub-tasks are done, this WorkProgress is done. */ |
41 |
| - public void endOfTasks() { |
42 |
| - jp.start(); |
43 |
| - } |
44 |
| - |
45 |
| - @Override |
46 |
| - public List<? extends WorkProgress> getTasks() { |
47 |
| - synchronized (tasks) { |
48 |
| - return new ArrayList<>(tasks); |
49 |
| - } |
50 |
| - } |
51 |
| - |
52 |
| - /** Automatically call the done or error method of this WorkProgress once all current sub-tasks are done. */ |
53 |
| - public void doneOnSubTasksDone() { |
54 |
| - if (jp != null) return; |
55 |
| - synchronized (tasks) { |
56 |
| - jp = new JoinPoint<>(); |
57 |
| - for (WorkProgress task : tasks) jp.addToJoin(task.getSynch()); |
58 |
| - } |
59 |
| - jp.listenInline(new Runnable() { |
60 |
| - @Override |
61 |
| - public void run() { |
62 |
| - if (jp.hasError()) error(jp.getError()); |
63 |
| - else done(); |
64 |
| - } |
65 |
| - }); |
66 |
| - jp.start(); |
67 |
| - } |
68 |
| - |
69 |
| -} |
| 1 | +package net.lecousin.framework.progress; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import net.lecousin.framework.concurrent.synch.JoinPoint; |
| 7 | + |
| 8 | +/** Implementation of WorkProgress, composed of sub-WorkProgress. */ |
| 9 | +public class MultiTaskProgress extends WorkProgressImpl implements WorkProgress.MultiTask { |
| 10 | + |
| 11 | + /** Constructor. */ |
| 12 | + public MultiTaskProgress(String text) { |
| 13 | + super(0, text); |
| 14 | + } |
| 15 | + |
| 16 | + protected ArrayList<WorkProgress> tasks = new ArrayList<>(); |
| 17 | + protected JoinPoint<Exception> jp = null; |
| 18 | + |
| 19 | + /** Create a sub-progress for the given amount of work (this amount is added to the total amount to be done). */ |
| 20 | + public WorkProgress createTaskProgress(long amount, String text) { |
| 21 | + this.amount += amount; |
| 22 | + SubWorkProgress task = new SubWorkProgress(this, amount, amount, text); |
| 23 | + synchronized (tasks) { |
| 24 | + tasks.add(task); |
| 25 | + if (jp != null) jp.addToJoin(task.getSynch()); |
| 26 | + } |
| 27 | + return task; |
| 28 | + } |
| 29 | + |
| 30 | + /** Add the given sub-progress as a sub-task for the given amount of work (this amount is added to the total amount to be done). */ |
| 31 | + public void addTask(WorkProgress task, long amount) { |
| 32 | + this.amount += amount; |
| 33 | + synchronized (tasks) { |
| 34 | + tasks.add(task); |
| 35 | + if (jp != null) jp.addToJoin(task.getSynch()); |
| 36 | + } |
| 37 | + WorkProgressUtil.propagateToParent(task, this, amount); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public List<? extends WorkProgress> getTasks() { |
| 42 | + synchronized (tasks) { |
| 43 | + return new ArrayList<>(tasks); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** Automatically call the done or error method of this WorkProgress once all current sub-tasks are done. */ |
| 48 | + public void doneOnSubTasksDone() { |
| 49 | + if (jp != null) return; |
| 50 | + synchronized (tasks) { |
| 51 | + jp = new JoinPoint<>(); |
| 52 | + for (WorkProgress task : tasks) jp.addToJoin(task.getSynch()); |
| 53 | + } |
| 54 | + jp.listenInline(new Runnable() { |
| 55 | + @Override |
| 56 | + public void run() { |
| 57 | + if (jp.hasError()) error(jp.getError()); |
| 58 | + else done(); |
| 59 | + } |
| 60 | + }); |
| 61 | + jp.start(); |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments