de.caff.util

## Class Worker

- java.lang.Object
- - de.caff.util.Worker

- All Implemented Interfaces:
    java.lang.Runnable

      public abstract class Worker
      extends java.lang.Object
      implements java.lang.Runnable

  A Runnable which knows of exceptions.
  Author:
    Rammi

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `Worker()` Default constructor.
    `Worker(KnockOffListener listener)` Constructor taking an initial knock off listener.

  - ### Method Summary

    All Methods Static Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addKnockOffListener(KnockOffListener listener)` Add a knock off listener.
    `protected abstract void` | `execute()` Implement this in extending classes to do the work.
    `java.lang.Throwable` | `getCaughtThrowable()` Get the throwable caught during work, if there happened one.
    `protected void` | `informKnockOffListeners()` Call all knockoff listeners.
    `static void` | `invokeInEventDispatchThread(java.lang.Runnable run)` Invoke in the AWT thread.
    `boolean` | `isStopped()` Was the worker stopped?
    `void` | `removeKnockOffListener(KnockOffListener listener)` Remove a knock off listener.
    `void` | `rethrow()` This throws the caught exception if there occurred one.
    `void` | `run()` When an object implementing interface `Runnable` is used to create a thread, starting the thread causes the object's `run` method to be called in that separately executing thread.
    `void` | `stop()` Removes all KnockOffListeners and sets the internal state to stopped.
    `void` | `stop(boolean removeListeners)` Sets the internal state to stopped.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### Worker

          public Worker()

      Default constructor.

    - #### Worker

          public Worker(@NotNull
                        KnockOffListener listener)

      Constructor taking an initial knock off listener.
      Parameters:
        `listener` - knock off listener

  - ### Method Detail

    - #### run

          public void run()

      When an object implementing interface `Runnable` is used to create a thread, starting the thread causes the object's `run` method to be called in that separately executing thread.

      The general contract of the method `run` is that it may take any action whatsoever.

      Specified by:
        `run` in interface `java.lang.Runnable`
      See Also:
        `Thread.run()`

    - #### execute

          protected abstract void execute()
                                   throws java.lang.Throwable

      Implement this in extending classes to do the work.
      Throws:
        `java.lang.Throwable` - any exception thrown during work

    - #### getCaughtThrowable

          @Nullable
          public java.lang.Throwable getCaughtThrowable()

      Get the throwable caught during work, if there happened one.
      Returns:
        the throwable or `null` if there didn't occur any

    - #### rethrow

          public void rethrow()
                       throws java.lang.Throwable

      This throws the caught exception if there occurred one.
      Throws:
        `java.lang.Throwable` - exception caught during execute of work

    - #### addKnockOffListener

          public void addKnockOffListener(@NotNull
                                          KnockOffListener listener)

      Add a knock off listener. The knock off listener will be called in the AWT event thread after the worker has finished.
      Parameters:
        `listener` - new listener

    - #### removeKnockOffListener

          public void removeKnockOffListener(@NotNull
                                             KnockOffListener listener)

      Remove a knock off listener.
      Parameters:
        `listener` - listener to remove

    - #### informKnockOffListeners

          protected void informKnockOffListeners()

      Call all knockoff listeners.

    - #### stop

          public void stop()

      Removes all KnockOffListeners and sets the internal state to stopped. Implementing classes may check the `isStopped()` method regularly and stop executing when it returns true.

      This is a workaround for the broken behavior of the Thread.stop() method.

    - #### stop

          public void stop(boolean removeListeners)

      Sets the internal state to stopped. Implementing classes may check the `isStopped()` method regularly and stop executing when the stop state is true.

      This is a workaround for the broken behavior of the Thread.stop() method.

      Parameters:
        `removeListeners` - remove the listeners before stopping so they are not called when the worker finishes? This is usually a good idea.

    - #### isStopped

          public boolean isStopped()

      Was the worker stopped? Implementing classes may check this method regularly and stop executing when it returns `true`.

      This is a workaround for the broken behavior of the Thread.stop() method.

      Returns:
        was on of the stop methods called?
      See Also:
        `stop()`, `stop(boolean)`

    - #### invokeInEventDispatchThread

          public static void invokeInEventDispatchThread(@NotNull
                                                         java.lang.Runnable run)

      Invoke in the AWT thread.
      Parameters:
        `run` - runnable which has to be invoked in the AWT event thread