OSDN Git Service

WorkerをSwingWorker風に
[coroid/inqubus.git] / frontend / src / saccubus / worker / Worker.java
1 package saccubus.worker;
2
3 import java.util.concurrent.Callable;
4
5 /**
6  * 途中経過を報告できるCallableです.
7  *
8  * @author yuki
9  */
10 public abstract class Worker<T, V> implements Callable<T> {
11
12     private static int serialNumber = 0;
13     private final int id;
14     private final WorkerListener<V> listener;
15
16     public Worker(WorkerListener<V> listener) {
17         this.id = ++serialNumber;
18         this.listener = listener;
19     }
20
21     @Override
22     public final T call() throws Exception {
23         try {
24             final T result = doInBackground();
25             // TODO
26             publish(null);
27             return result;
28         } catch (Throwable th) {
29             // TODO
30             publish(null);
31             throw th;
32         }
33     }
34
35     protected abstract T doInBackground() throws Exception;
36
37     public final int getId() {
38         return id;
39     }
40
41     protected final void publish(V value) {
42         if (listener != null) {
43             listener.process(value);
44         }
45     }
46 }