|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2017 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics, University of |
| 8 | + * Konstanz, and KNIME GmbH. |
| 9 | + * %% |
| 10 | + * Redistribution and use in source and binary forms, with or without |
| 11 | + * modification, are permitted provided that the following conditions are met: |
| 12 | + * |
| 13 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 14 | + * this list of conditions and the following disclaimer. |
| 15 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 16 | + * this list of conditions and the following disclaimer in the documentation |
| 17 | + * and/or other materials provided with the distribution. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 23 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | + * POSSIBILITY OF SUCH DAMAGE. |
| 30 | + * #L% |
| 31 | + */ |
| 32 | +package org.scijava.task; |
| 33 | + |
| 34 | +import java.util.concurrent.ExecutionException; |
| 35 | +import java.util.concurrent.Future; |
| 36 | + |
| 37 | +import org.scijava.event.EventService; |
| 38 | +import org.scijava.task.event.TaskEvent; |
| 39 | +import org.scijava.thread.ThreadService; |
| 40 | + |
| 41 | +/** |
| 42 | + * Default implementation of {@link Task}. It launches code via the linked |
| 43 | + * {@link ThreadService}, and reports status updates via the linked |
| 44 | + * {@link EventService}. |
| 45 | + * |
| 46 | + * @author Curtis Rueden |
| 47 | + */ |
| 48 | +public class DefaultTask implements Task { |
| 49 | + |
| 50 | + private final ThreadService threadService; |
| 51 | + private final EventService eventService; |
| 52 | + |
| 53 | + private Future<?> future; |
| 54 | + |
| 55 | + private boolean canceled; |
| 56 | + private String cancelReason; |
| 57 | + |
| 58 | + private String status; |
| 59 | + private long step; |
| 60 | + private long max; |
| 61 | + |
| 62 | + private String name; |
| 63 | + |
| 64 | + /** |
| 65 | + * Creates a new task. |
| 66 | + * |
| 67 | + * @param threadService Service to use for launching the task in its own |
| 68 | + * thread. Required. |
| 69 | + * @param eventService Service to use for reporting status updates as |
| 70 | + * {@link TaskEvent}s. May be null, in which case no events are |
| 71 | + * reported. |
| 72 | + */ |
| 73 | + public DefaultTask(final ThreadService threadService, |
| 74 | + final EventService eventService) |
| 75 | + { |
| 76 | + this.threadService = threadService; |
| 77 | + this.eventService = eventService; |
| 78 | + } |
| 79 | + |
| 80 | + // -- Task methods -- |
| 81 | + |
| 82 | + @Override |
| 83 | + public void run(final Runnable r) { |
| 84 | + if (r == null) throw new NullPointerException(); |
| 85 | + future(r); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void waitFor() throws InterruptedException, ExecutionException { |
| 90 | + future().get(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public boolean isDone() { |
| 95 | + return future != null && future.isDone(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public String getStatusMessage() { |
| 100 | + return status; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public long getProgressValue() { |
| 105 | + return step; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public long getProgressMaximum() { |
| 110 | + return max; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void setStatusMessage(final String status) { |
| 115 | + this.status = status; |
| 116 | + fireTaskEvent(); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void setProgressValue(final long step) { |
| 121 | + this.step = step; |
| 122 | + fireTaskEvent(); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void setProgressMaximum(final long max) { |
| 127 | + this.max = max; |
| 128 | + fireTaskEvent(); |
| 129 | + } |
| 130 | + |
| 131 | + // -- Cancelable methods -- |
| 132 | + |
| 133 | + @Override |
| 134 | + public boolean isCanceled() { |
| 135 | + return canceled; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void cancel(final String reason) { |
| 140 | + canceled = true; |
| 141 | + cancelReason = reason; |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public String getCancelReason() { |
| 146 | + return cancelReason; |
| 147 | + } |
| 148 | + |
| 149 | + // -- Named methods -- |
| 150 | + |
| 151 | + @Override |
| 152 | + public String getName() { |
| 153 | + return name; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void setName(final String name) { |
| 158 | + this.name = name; |
| 159 | + } |
| 160 | + |
| 161 | + // -- Helper methods -- |
| 162 | + |
| 163 | + private Future<?> future() { |
| 164 | + return future(null); |
| 165 | + } |
| 166 | + |
| 167 | + private Future<?> future(final Runnable r) { |
| 168 | + if (future == null) initFuture(r); |
| 169 | + return future; |
| 170 | + } |
| 171 | + |
| 172 | + private synchronized void initFuture(final Runnable r) { |
| 173 | + if (future != null) return; |
| 174 | + if (r == null) throw new IllegalArgumentException("Must call run first"); |
| 175 | + future = threadService.run(r); |
| 176 | + } |
| 177 | + |
| 178 | + private void fireTaskEvent() { |
| 179 | + if (eventService != null) eventService.publish(new TaskEvent(this)); |
| 180 | + } |
| 181 | +} |
0 commit comments