Package edu.cnu.mdi.sim.task
Class TaskContext
java.lang.Object
edu.cnu.mdi.sim.task.TaskContext
Services made available to a
BackgroundTask while it executes.
The context is owned by its TaskHandle and is valid for the
lifetime of that handle. Its methods may be called from the task's worker
thread. Progress, message, and refresh notifications are routed through the
underlying simulation engine and therefore reach listeners on Swing's EDT.
High-frequency progress, message, and refresh notifications retain the
engine's coalescing behavior.
-
Method Summary
Modifier and TypeMethodDescriptiondoubleReturns elapsed wall-clock time since the worker engine started.booleanTests whether cancellation has been requested.voidpostMessage(String message) Publishes transient user-facing status text.voidreportIndeterminateProgress(String message) Publishes indeterminate progress.voidreportProgress(double fraction, String message) Publishes determinate progress.voidRequests a coalesced UI refresh from registered listeners.voidAborts the current task cooperatively if cancellation was requested.
-
Method Details
-
isCancellationRequested
public boolean isCancellationRequested()Tests whether cancellation has been requested.- Returns:
trueafter the first call toTaskHandle.cancel()
-
throwIfCancellationRequested
public void throwIfCancellationRequested()Aborts the current task cooperatively if cancellation was requested.The resulting exception is intercepted by MDI and reported as cancellation, not failure.
- Throws:
TaskCancelledException- if cancellation was requested
-
reportProgress
Publishes determinate progress. Values outside[0,1]are clamped byProgressInfo.determinate(double, String).- Parameters:
fraction- completed fractionmessage- optional user-facing status text
-
reportIndeterminateProgress
Publishes indeterminate progress.- Parameters:
message- optional user-facing status text
-
postMessage
Publishes transient user-facing status text.- Parameters:
message- non-null message- Throws:
NullPointerException- ifmessageis null
-
requestRefresh
public void requestRefresh()Requests a coalesced UI refresh from registered listeners. -
getElapsedSeconds
public double getElapsedSeconds()Returns elapsed wall-clock time since the worker engine started.- Returns:
- elapsed seconds, or zero before startup bookkeeping completes
-