| 26 | import java.util.function.BiConsumer; |
| 27 | |
| 28 | public abstract class AbstractTask implements Task { |
| 29 | |
| 30 | protected final int id; |
| 31 | |
| 32 | private final AtomicBoolean firstClose = new AtomicBoolean(false); |
| 33 | private final CompletableFuture<Void> future = new CompletableFuture<>(); |
| 34 | |
| 35 | protected AbstractTask(int id) { |
| 36 | this.id = id; |
| 37 | } |
| 38 | |
| 39 | public int id() { |
| 40 | return id; |
| 41 | } |
| 42 | |
| 43 | protected static BiConsumer<? super Object, Throwable> closeOrKill(AbstractTask t) { |
| 44 | return (result, err) -> { |
| 45 | if (err == null) { |
| 46 | t.close(); |
| 47 | } else { |
| 48 | t.kill(err); |
| 49 | } |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | protected CompletableFuture<Void> innerStart() { |
| 54 | return null; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public final CompletableFuture<Void> start() { |
| 59 | if (!firstClose.get()) { |
| 60 | try { |
| 61 | return innerStart(); |
| 62 | } catch (Throwable t) { |
| 63 | kill(t); |
| 64 | } |
| 65 | } |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | protected void innerClose() { |
| 70 | } |
| 71 | |
| 72 | protected void close() { |
| 73 | if (firstClose.compareAndSet(false, true)) { |
| 74 | try { |
| 75 | innerClose(); |
| 76 | future.complete(null); |
| 77 | } catch (Throwable t) { |
| 78 | future.completeExceptionally(t); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | protected void innerKill(Throwable t) { |
| 84 | } |
| 85 |
nothing calls this directly
no outgoing calls
no test coverage detected