Skip to content

Commit d069997

Browse files
committed
Fix up the scijava-progress README
1 parent c04b39c commit d069997

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

scijava-progress/README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,23 @@ The `Progress` class is designed to be the middle-man between an operation that
1111
Suppose we have a `Function` that does some heavy calculation on a `List`, and wants to notify callers when it finishes one element, before it moves to the next:
1212

1313
```java
14-
public class Foo implements Function<List<Integer>, List<Integer>> {
14+
public class HeavyCalculator implements Function<List<Integer>, List<Integer>> {
1515

1616
@Override
17-
public Integer apply(List<Integer> in) {
17+
public List<Integer> apply(List<Integer> in) {
1818
List<Integer> out = new ArrayList<>();
1919
for(int i = 0; i < in.size(); i++) {
20-
out.set(i, doTheComputation(in.get(i)));
20+
out.add(doTheComputation(in.get(i)));
2121
}
22+
return out;
2223
}
2324

2425
}
2526

2627
```
2728

2829
Firstly, we add the bookkeeping steps:
29-
1. Notify `Progress` that this Object wants to record its progress by calling `Progress.register(Object progressible)`. This can either be called within our `Function` (by passing `this`), or before the `apply` method is called (by passing our `Foo` instance).
30+
1. Notify `Progress` that this Object wants to record its progress by calling `Progress.register(Object progressible)`. This can either be called within our `Function` (by passing `this`), or before the `apply` method is called (by passing our `HeavyCalculator` instance).
3031
2. Define what total progress means using `Progress.defineTotalProgress(int numStages, int numSubTasks)`. We make the distinction between `numStages` and `numSubtasks`:
3132

3233
* `numStages` lets `Progress` know how many stages of computation will be performed within the current task
@@ -37,22 +38,23 @@ Firstly, we add the bookkeeping steps:
3738
Once these bookkeeping stages are added, we can then call `Progress.update`. Our `Function`, updating progress, would then look like:
3839

3940
```java
40-
public class Foo implements Function<List<Integer>, List<Integer>> {
41+
public class HeavyCalculator implements Function<List<Integer>, List<Integer>> {
4142

4243
@Override
43-
public Integer apply(List<Integer> in) {
44+
public List<Integer> apply(List<Integer> in) {
4445
Progress.register(this);
4546
Progress.defineTotalProgress(1, 0);
4647
Progress.setStageMax(in.size());
47-
48+
4849
// compute
4950
List<Integer> out = new ArrayList<>();
5051
for(int i = 0; i < in.size(); i++) {
51-
out.set(i, doTheComputation(in.get(i)));
52+
out.add(doTheComputation(in.get(i)));
5253
Progress.update();
5354
}
5455

5556
Progress.complete();
57+
return out;
5658
}
5759

5860
}
@@ -65,14 +67,14 @@ Ops can also set their `status` through the `Progress` framework, using the meth
6567

6668
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`.
6769

68-
`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 `Foo`:
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`:
6971

7072
```java
71-
Foo f = new Foo();
73+
HeavyCalculator calc = new HeavyCalculator();
7274

7375
// register a listener that prints progress to console
74-
Progress.addListener(f, (t) -> System.out.println(t.progress()));
76+
Progress.addListener(calc, t -> System.out.println(t.progress()));
7577

76-
// call Foo
77-
f.apply(Arrays.asList(1, 2, 3, 4);
78+
// call HeavyCalculator
79+
calc.apply(Arrays.asList(1, 2, 3, 4);
7880
```

0 commit comments

Comments
 (0)