Skip to content

Commit 4246cdb

Browse files
committed
ProgressListener -> Consumer<Task>
1 parent ce68f72 commit 4246cdb

File tree

11 files changed

+84
-128
lines changed

11 files changed

+84
-128
lines changed

scijava-ops-legacy/src/main/java/org/scijava/legacy/service/DefaultOpEnvironmentService.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.scijava.plugin.Parameter;
3636
import org.scijava.plugin.Plugin;
3737
import org.scijava.progress.Progress;
38-
import org.scijava.progress.ProgressListener;
3938
import org.scijava.progress.Task;
4039
import org.scijava.script.ScriptService;
4140
import org.scijava.service.AbstractService;
@@ -44,6 +43,7 @@
4443

4544
import java.util.Map;
4645
import java.util.WeakHashMap;
46+
import java.util.function.Consumer;
4747

4848
/**
4949
* Default implementation of {@link OpEnvironmentService}
@@ -70,22 +70,22 @@ public void initialize() {
7070

7171
// Set up progress, if StatusService available
7272
if (taskService != null) {
73-
Progress.addGlobalListener(new SciJavaProgressListener(taskService));
73+
Progress.addGlobalListener(new SciJavaTaskConsumer(taskService));
7474
}
7575
}
7676

77-
private static class SciJavaProgressListener implements ProgressListener {
77+
private static class SciJavaTaskConsumer implements Consumer<Task> {
7878

7979
private final Map<Task, org.scijava.task.Task> taskMap;
8080
private final TaskService tasks;
8181

82-
public SciJavaProgressListener(TaskService tasks) {
82+
public SciJavaTaskConsumer(TaskService tasks) {
8383
this.tasks = tasks;
8484
this.taskMap = new WeakHashMap<>();
8585
}
8686

8787
@Override
88-
public void acknowledgeUpdate(Task task) {
88+
public void accept(Task task) {
8989
if (task.isSubTask()) return;
9090
var sjTask = taskMap.computeIfAbsent( //
9191
task, //

scijava-ops-tutorial/src/main/java/org/scijava/ops/tutorial/ReportingProgress.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@
3131

3232
import java.util.ArrayList;
3333
import java.util.List;
34+
import java.util.function.Consumer;
3435
import java.util.function.Function;
3536

3637
import org.scijava.ops.api.Hints;
3738
import org.scijava.ops.api.OpEnvironment;
3839
import org.scijava.ops.spi.OpCollection;
3940
import org.scijava.ops.spi.OpField;
4041
import org.scijava.progress.Progress;
41-
import org.scijava.progress.ProgressListener;
4242
import org.scijava.progress.StandardOutputProgressLogger;
43+
import org.scijava.progress.Task;
4344
import org.scijava.types.Nil;
4445

4546
/**
@@ -68,7 +69,7 @@
6869
* <li><em>subtask</em>s are phases of computation done <b>by other Ops</b></li>
6970
* </ul>
7071
* Users are then notified by the progress of Ops by installing
71-
* {@link ProgressListener}s.
72+
* {@link Consumer<Task>}s.
7273
*
7374
* @author Gabriel Selzer
7475
*/
@@ -122,16 +123,16 @@ public static void main(String... args) {
122123
// To enable Progress Reporting, you must enable the progress tracking hint!
123124
ops.setDefaultHints(new Hints("progress.TRACK"));
124125

125-
// ProgressListeners consume task updates.
126-
// This ProgressListener simply logs to standard output, but we could print
126+
// Consumer<Task>s consume task updates.
127+
// This Consumer simply logs to standard output, but we could print
127128
// out something else, or pass this information somewhere else.
128-
ProgressListener l = new StandardOutputProgressLogger();
129-
// To listen to Op progress updates, the ProgressListener must be registered
129+
Consumer<Task> l = new StandardOutputProgressLogger();
130+
// To listen to Op progress updates, the Consumer must be registered
130131
// through the Progress API. To listen to all Op executions, use the
131132
// following call:
132133
Progress.addGlobalListener(l);
133134
// If listening to every Op would be overwhelming, the Progress API also
134-
// allows ProgressListeners to be registered for a specific Op, using the
135+
// allows Consumers to be registered for a specific Op, using the
135136
// following call:
136137
// Progress.addListener(op, l);
137138

@@ -143,7 +144,7 @@ public static void main(String... args) {
143144
.function();
144145

145146
// When we apply the Op, we will automatically print the progress out to
146-
// the console, thanks to our ProgressListener above.
147+
// the console, thanks to our Consumers above.
147148
var numPrimes = 100;
148149
var primes = op.apply(numPrimes);
149150
System.out.println("First " + numPrimes + " primes: " + primes);

scijava-progress/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ Ops can also set their `status` through the `Progress` framework, using the meth
6565

6666
### Accessing `Progress` as a listener
6767

68-
Progress is accessed by listeners using the `Progress.addListener(Object progressible, ProgressListener l)` method. This method must be called **before** `progressible`'s code is executed, and all executions of `progressible` will then be sent to `l`.
68+
Progress is accessed by listeners using the `Progress.addListener(Object progressible, Consumer<Task> l)` method. This method must be called **before** `progressible`'s code is executed, and all executions of `progressible` will then be sent to `l`.
6969

70-
`ProgressListener` is, at its core, a `FunctionalInterface`, allowing `ProgressListener`s to be defined as lambdas. The functional method, `acknowledgeUpdate(Task t)`, is then called when **any execution** of `l` calls **either** `Progress.update()` or `Progress.setStatus()`. Below is an example of how one might write a `ProgressListener` for `HeavyCalculator`:
70+
`l` can be written as a lambda (shown below), or as an explicit implementation of the `Consumer` interface. The functional method, `accept(Task t)`, is then called when **any execution** of `progressible` calls `Progress.register`, `Progress.complete`, `Progress.update()`, or `Progress.setStatus()`. Below is an example of how one might write a `Consumer<Task>` for `HeavyCalculator`:
7171

7272
```java
7373
HeavyCalculator calc = new HeavyCalculator();

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

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import java.util.*;
3333
import java.util.concurrent.CopyOnWriteArrayList;
34+
import java.util.function.Consumer;
3435

3536
/**
3637
* A static utility class serving as the interface between progress reporters
@@ -51,7 +52,7 @@ private Progress() {}
5152
* concurrency we use {@link CopyOnWriteArrayList} as the backing
5253
* implementation
5354
*/
54-
private static final List<ProgressListener> globalListeners =
55+
private static final List<Consumer<Task>> globalListeners =
5556
new CopyOnWriteArrayList<>();
5657

5758
/**
@@ -60,7 +61,7 @@ private Progress() {}
6061
* concurrency we use {@link CopyOnWriteArrayList} as the backing
6162
* implementation
6263
*/
63-
private static final Map<Object, List<ProgressListener>> progressibleListeners =
64+
private static final Map<Object, List<Consumer<Task>>> progressibleListeners =
6465
new WeakHashMap<>();
6566

6667
/** Singleton NOP task */
@@ -92,27 +93,27 @@ protected ArrayDeque<Task> initialValue() {
9293
};
9394

9495
/**
95-
* Records {@link ProgressListener} {@code l} as a callback for all
96-
* progressible {@link Object}s
96+
* Records {@link Consumer<Task>} {@code l} as a callback for all progressible
97+
* {@link Object}s
9798
*
98-
* @param l a {@link ProgressListener} that would like to know about the
99+
* @param l a {@link Consumer<Task>} that would like to know about the
99100
* progress of {@code progressible} {@link Object}s
100101
*/
101-
public static void addGlobalListener(ProgressListener l) {
102+
public static void addGlobalListener(Consumer<Task> l) {
102103
if (!globalListeners.contains(l)) {
103104
globalListeners.add(l);
104105
}
105106
}
106107

107108
/**
108-
* Records {@link ProgressListener} {@code l} as a callback for progressible
109+
* Records {@link Consumer<Task>} {@code l} as a callback for progressible
109110
* {@link Object} {@code progressible}
110111
*
111112
* @param progressible an {@link Object} that reports its progress
112-
* @param l a {@link ProgressListener} that would like to know about the
113+
* @param l a {@link Consumer<Task>} that would like to know about the
113114
* progress of {@code progressible}
114115
*/
115-
public static void addListener(Object progressible, ProgressListener l) {
116+
public static void addListener(Object progressible, Consumer<Task> l) {
116117
if (!progressibleListeners.containsKey(progressible)) {
117118
createListenerList(progressible);
118119
}
@@ -127,7 +128,7 @@ private static synchronized void createListenerList(Object progressible) {
127128
/**
128129
* Completes the current task on this {@link Thread}'s execution hierarchy,
129130
* removing it in the process. This method also takes care to ping relevant
130-
* {@link ProgressListener}s.
131+
* {@link Consumer<Task>}s.
131132
*
132133
* @see Task#complete()
133134
*/
@@ -194,7 +195,7 @@ public static void register(final Object progressible,
194195
}
195196

196197
/**
197-
* Activates all callback {@link ProgressListener}s listening for progress
198+
* Activates all callback {@link Consumer <Task>}s listening for progress
198199
* updates on executions of {@code o}
199200
*
200201
* @param task an {@link Object} reporting its progress.
@@ -204,16 +205,14 @@ private static void pingListeners(Task task) {
204205
return;
205206
}
206207
// Ping object-specific listeners
207-
List<ProgressListener> list = progressibleListeners.getOrDefault( //
208+
List<Consumer<Task>> list = progressibleListeners.getOrDefault( //
208209
task.progressible(), //
209210
Collections.emptyList() //
210211
);
211-
synchronized (list) {
212-
list.forEach(l -> l.acknowledgeUpdate(task));
213-
}
212+
list.forEach(l -> l.accept(task));
214213
// Ping global listeners
215214
for (var l : globalListeners)
216-
l.acknowledgeUpdate(task);
215+
l.accept(task);
217216
// Ping parent
218217
if (task.isSubTask()) {
219218
pingListeners(task.parent());
@@ -231,7 +230,7 @@ public static Task currentTask() {
231230

232231
/**
233232
* Updates the progress of the current {@link Task}, pinging any interested
234-
* {@link ProgressListener}s.
233+
* {@link Consumer<Task>}s.
235234
*
236235
* @see Task#update(long)
237236
*/
@@ -241,7 +240,7 @@ public static void update() {
241240

242241
/**
243242
* Updates the progress of the current {@link Task}, pinging any interested
244-
* {@link ProgressListener}s.
243+
* {@link Consumer<Task>}s.
245244
*
246245
* @param elements the number of elements completed in the current stage.
247246
* @see Task#update(long)
@@ -252,7 +251,7 @@ public static void update(long elements) {
252251

253252
/**
254253
* Updates the progress of the provided {@link Task}, pinging any interested
255-
* {@link ProgressListener}s.
254+
* {@link Consumer<Task>}s.
256255
*
257256
* @param numElements the number of elements completed in the current stage.
258257
* @param task the {@link Task} to update
@@ -265,7 +264,7 @@ public static void update(final long numElements, final Task task) {
265264

266265
/**
267266
* Sets the status of the current {@link Task}, pinging any interested
268-
* {@link ProgressListener}s.
267+
* {@link Consumer<Task>}s.
269268
*
270269
* @see Task#setStatus(String)
271270
*/

scijava-progress/src/main/java/org/scijava/progress/ProgressListener.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

scijava-progress/src/main/java/org/scijava/progress/StandardOutputProgressLogger.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@
2929

3030
package org.scijava.progress;
3131

32+
import java.util.function.Consumer;
33+
3234
/**
33-
* Simple {@link ProgressListener} logging updates to standard output.
35+
* Simple {@link Consumer<Task>} logging updates to standard output.
3436
*
3537
* @author Gabriel Selzer
3638
*/
37-
public class StandardOutputProgressLogger implements ProgressListener {
39+
public class StandardOutputProgressLogger implements Consumer<Task> {
3840

3941
@Override
40-
public void acknowledgeUpdate(Task task) {
42+
public void accept(Task task) {
4143
if (task.isComplete()) {
4244
System.out.printf("Progress of %s: Complete\n", task.description());
4345
}

0 commit comments

Comments
 (0)