Class OneTimeRunnable

  • All Implemented Interfaces:
    Cancellable, CancellableRunnable, java.lang.Runnable

    public class OneTimeRunnable
    extends AwtRunnable
    implements CancellableRunnable
    Used to create a task that can be run at most once and the run can be cancelled before it has run, in which case further attempts to run it will do nothing.

    Can also specify that it should run on the AWT thread, otherwise it will run on the application thread

    Useful for triggering actions after a delay that may need to be run before the delay triggers

    • Constructor Detail

      • OneTimeRunnable

        public OneTimeRunnable​(@NotNull
                               @NotNull java.lang.String id,
                               com.intellij.openapi.application.ModalityState modalityState,
                               java.lang.Runnable command)
      • OneTimeRunnable

        public OneTimeRunnable​(@NotNull
                               @NotNull java.lang.Runnable command)
      • OneTimeRunnable

        public OneTimeRunnable​(@NotNull
                               @NotNull java.lang.String id,
                               @NotNull
                               @NotNull java.lang.Runnable command)
      • OneTimeRunnable

        public OneTimeRunnable​(@NotNull
                               @NotNull java.lang.String id,
                               boolean awtThread,
                               java.lang.Runnable command)
      • OneTimeRunnable

        public OneTimeRunnable​(boolean awtThread,
                               java.lang.Runnable command)
      • OneTimeRunnable

        public OneTimeRunnable​(@NotNull
                               @NotNull java.lang.String id,
                               boolean awtThread,
                               @Nullable
                               @Nullable com.intellij.openapi.application.ModalityState modalityState,
                               java.lang.Runnable command)
    • Method Detail

      • cancel

        public boolean cancel()
        Cancels the scheduled task run if it has not run yet
        Specified by:
        cancel in interface Cancellable
        Returns:
        true if cancelled, false if it has already run
      • canRun

        public boolean canRun()
        Tests whether it has run or been cancelled
        Specified by:
        canRun in interface Cancellable
        Returns:
        false if cancelled or has run, true still can run later
      • run

        public void run()
        Specified by:
        run in interface java.lang.Runnable
        Overrides:
        run in class AwtRunnable
      • schedule

        public static OneTimeRunnable schedule​(@NotNull
                                               @NotNull CancelableJobScheduler scheduler,
                                               @NotNull
                                               @NotNull java.lang.String id,
                                               int delay,
                                               @NotNull
                                               @NotNull java.lang.Runnable command)
        Creates a one-shot runnable that will run after a delay, can be run early, or cancelled

        the given command will only be executed once, either by the delayed trigger or by the run method. if you want to execute the task early just invoke #run, it will do nothing if the task has already run.

        Parameters:
        scheduler - job scheduler to use
        id - id of the job
        delay - the time from now to delay execution
        command - the task to execute
        Returns:
        a OneTimeRunnable which will run after the given delay or if run() is invoked before cancel() is invoked
        Throws:
        java.lang.NullPointerException - if command is null
      • schedule

        public static OneTimeRunnable schedule​(CancelableJobScheduler scheduler,
                                               @NotNull
                                               @NotNull java.lang.String id,
                                               int delay,
                                               @Nullable
                                               @Nullable com.intellij.openapi.application.ModalityState modalityState,
                                               @NotNull
                                               @NotNull java.lang.Runnable command)
        Creates a one-shot runnable that will run after a delay, can be run early, or cancelled

        the given command will only be executed once, either by the delayed trigger or by the run method. if you want to execute the task early just invoke #run, it will do nothing if the task has already run.

        Parameters:
        scheduler - job scheduler to use
        id - id of the job
        delay - the time from now to delay execution
        modalityState - the modality state to run the job in
        command - the task to execute
        Returns:
        a CancellableRunnable which will run after the given delay on the AwtThread if run() is invoked before Cancellable.cancel()
        Throws:
        java.lang.NullPointerException - if command is null