Skip to content

Commit 044aa8b

Browse files
committed
Provide id for tasks
We might want to add a description to each Task, to tell them apart if a single listener listens to multiple tasks
1 parent 7861767 commit 044aa8b

5 files changed

Lines changed: 255 additions & 168 deletions

File tree

‎scijava/scijava-progress/src/main/java/org/scijava/progress/Progress.java‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public static void addListener(Object progressible, ProgressListener l) {
108108
}
109109
addListenerToList(progressible, l);
110110
}
111-
111+
112112
private static void addListenerToList(Object progressible,
113113
ProgressListener l)
114114
{
@@ -132,27 +132,42 @@ private static synchronized void createListenerList(Object progressible) {
132132
public static void complete() {
133133
// update completed task
134134
ProgressibleObject completed = progressibleStack.get().pop();
135-
completed.task().complete();
136-
// ping relevant listeners
137-
pingListeners(completed);
138-
if (progressibleStack.get().peek() != null) {
139-
pingListeners(progressibleStack.get().peek());
135+
if (!completed.task().isComplete()) {
136+
completed.task().complete();
137+
// ping relevant listeners
138+
pingListeners(completed);
139+
if (progressibleStack.get().peek() != null) {
140+
pingListeners(progressibleStack.get().peek());
141+
}
140142
}
141143
}
142144

145+
/**
146+
* Creates a new {@link Task} for {@code progressible}. This method makes the
147+
* assumption that {@code progressible} is responsible for any calls to
148+
* {@link Progress}' progress-reporting API between the time this method is
149+
* called and the time when {@link Progress#complete()} is called.
150+
*
151+
* @param progressible an {@link Object} that would like to report its progress.
152+
*/
153+
public static void register(final Object progressible) {
154+
register(progressible, progressible.toString());
155+
}
156+
143157
/**
144158
* Creates a new {@link Task} for {@code progressible}. This method makes the
145159
* assumption that {@code progressible} is responsible for any calls to
146160
* {@link Progress}' progress-reporting API between the time this method is
147161
* called and the time when {@link Progress#complete()} is called.
148162
*
149163
* @param progressible an {@link Object} that would like to report its progress.
164+
* @param description a {@link String} describing {@code progressible}
150165
*/
151-
public static void register(Object progressible) {
166+
public static void register(final Object progressible, final String description) {
152167
Task t;
153168
if (progressibleStack.get().size() == 0) {
154169
// completely new execution hierarchy
155-
t = new Task();
170+
t = new Task(description);
156171
}
157172
else {
158173
// part of an existing execution hierarchy
@@ -228,7 +243,7 @@ public static void setStatus(String status) {
228243
*
229244
* @see Task#defineTotalProgress(int)
230245
*/
231-
public static void defineTotalProgress(int numStages) {
246+
public static void defineTotalProgress(long numStages) {
232247
currentTask().defineTotalProgress(numStages);
233248
}
234249

@@ -237,7 +252,7 @@ public static void defineTotalProgress(int numStages) {
237252
*
238253
* @see Task#defineTotalProgress(int, int)
239254
*/
240-
public static void defineTotalProgress(int numStages, int numSubTasks) {
255+
public static void defineTotalProgress(long numStages, long numSubTasks) {
241256
currentTask().defineTotalProgress(numStages, numSubTasks);
242257
}
243258

‎scijava/scijava-progress/src/main/java/org/scijava/progress/Task.java‎

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
/**
3737
* Keeps track of progress associated with an execution of progressable code.
38-
* {@Link Task}s should <b>not</b> be updated by the code itself; it should be
38+
* {@link Task}s should <b>not</b> be updated by the code itself; it should be
3939
* update via {@link Progress#update()}.
4040
*
4141
* @author Gabriel Selzer
@@ -48,7 +48,7 @@ public class Task {
4848
private final Task parent;
4949

5050
/** Subtasks created by this task */
51-
private Set<Task> subTasks = new HashSet<>();
51+
private final Set<Task> subTasks = new HashSet<>();
5252

5353
/** Designates task completion */
5454
private boolean completed = false;
@@ -60,34 +60,39 @@ public class Task {
6060
private long numSubTasks = 0;
6161

6262
/** Progress in current stage */
63-
private AtomicLong current = new AtomicLong(0);
63+
private final AtomicLong current = new AtomicLong(0);
6464

6565
/** Maximum of current stage */
66-
private AtomicLong max = new AtomicLong(1);
66+
private final AtomicLong max = new AtomicLong(1);
6767

6868
/** Number of within-task stages completed */
69-
private AtomicLong stagesCompleted = new AtomicLong(0);
69+
private final AtomicLong stagesCompleted = new AtomicLong(0);
7070

7171
/** Number of subtasks completed */
72-
private AtomicLong subTasksCompleted = new AtomicLong(0);
72+
private final AtomicLong subTasksCompleted = new AtomicLong(0);
7373

7474
/**
75-
* True iff a call to {@link Task#defineTotalProgress(int, int)} has been made
75+
* True iff a call to {@link Task#defineTotalProgress(long, long)} has been made
7676
*/
7777
private boolean tasksDefined = false;
7878

7979
/** True iff {@link Task#max} has been defined for the current stage */
8080
private boolean updateDefined = false;
8181

82+
/** Computation status as defined by the task */
83+
private final String id;
84+
8285
/** Computation status as defined by the task */
8386
private String status = "Executing...";
8487

85-
public Task() {
88+
public Task(final String id) {
8689
this.parent = null;
90+
this.id = id;
8791
}
8892

89-
public Task(final Task parent) {
93+
public Task(final Task parent, final String id) {
9094
this.parent = parent;
95+
this.id = id;
9196
}
9297

9398
/**
@@ -112,7 +117,7 @@ public void complete() {
112117
* @return the subtask.
113118
*/
114119
public synchronized Task createSubtask() {
115-
final Task sub = new Task(this);
120+
final Task sub = new Task(this, "Subtask " + subTasks.size() + 1 + " of Task \"" + id + "\"");
116121
subTasks.add(sub);
117122
return sub;
118123
}
@@ -124,11 +129,11 @@ public synchronized Task createSubtask() {
124129
* {@link Task#progress()} are called, as otherwise total progress cannot be
125130
* defined.
126131
* <p>
127-
* Under the hood, this method calls {@link #defineTotalProgress(int, int)}
132+
* Under the hood, this method calls {@link #defineTotalProgress(long, long)}
128133
*
129134
* @param totalStages the number of computation stages within the task
130135
*/
131-
public void defineTotalProgress(final int totalStages) {
136+
public void defineTotalProgress(final long totalStages) {
132137
defineTotalProgress(totalStages, 0);
133138
}
134139

@@ -144,8 +149,8 @@ public void defineTotalProgress(final int totalStages) {
144149
* within the task. This <b>is not</b> the same as the number of
145150
* subtasks used (as one subtask may run multiple times).
146151
*/
147-
public void defineTotalProgress(final int totalStages,
148-
final int totalSubTasks)
152+
public void defineTotalProgress(final long totalStages,
153+
final long totalSubTasks)
149154
{
150155
this.numStages = totalStages;
151156
this.numSubTasks = totalSubTasks;
@@ -164,7 +169,7 @@ public boolean isComplete() {
164169
/**
165170
* Calculates and returns the progress of the associated progressible
166171
* {@link Object}. If the total progress is defined using
167-
* {@link Task#defineTotalProgress(int, int)}, then this method will return a
172+
* {@link Task#defineTotalProgress(long, long)}, then this method will return a
168173
* {@code double} within the range [0, 1]. If the progress is <b>not</b>
169174
* defined, then this task will return {@code 0} until {@link #complete()} is
170175
* called; after that call this method will return {@code 1.}.
@@ -180,15 +185,6 @@ public double progress() {
180185
return totalCompletion / (numStages + numSubTasks);
181186
}
182187

183-
/**
184-
* Returns {@code true} iff progress has been defined
185-
*
186-
* @return {@code true} iff progress has been defined
187-
*/
188-
public boolean progressDefined() {
189-
return tasksDefined;
190-
}
191-
192188
/**
193189
* Iff {@code task} was a task generated by this {@link Task}, then we update
194190
* progress based on its completion.
@@ -206,8 +202,6 @@ private void resetStage() {
206202
current.set(0);
207203
stagesCompleted.incrementAndGet();
208204
this.updateDefined = false;
209-
if (stagesCompleted.longValue() == numStages && subTasksCompleted
210-
.longValue() == numSubTasks) completed = true;
211205
}
212206

213207
/**
@@ -241,6 +235,15 @@ public String status() {
241235
return status;
242236
}
243237

238+
/**
239+
* The id of the progressible {@link Object}
240+
*
241+
* @return the id
242+
*/
243+
public String id() {
244+
return id;
245+
}
246+
244247
/**
245248
* Used by a progressible {@link Object}, through {@link Progress} to
246249
* increment progress

0 commit comments

Comments
 (0)