Package org.eclipse.emf.compare.ide.ui.internal.logical.resolver

## Class ResourceComputationScheduler<T>

- java.lang.Object
- - org.eclipse.emf.compare.ide.ui.internal.logical.resolver.ResourceComputationScheduler<T>

Type Parameters:
  `T` - type of keys used to keep track of currently ongoing computations.

    public class ResourceComputationScheduler<T>
    extends Object

Class providing facilities to schedule computations and hide as much as possible the multi-threaded complexity. Each computation is identified by a key. Each computation is supposed to possibly load resources, and the scheduler also provides a facility to unload resources that are no longer needed.

### Nested Class Summary

Nested Classes
`static class` `ResourceComputationScheduler.CallStatus`
`static class` `ResourceComputationScheduler.ComputationState`
`static class` `ResourceComputationScheduler.ShutdownState`
`static class` `ResourceComputationScheduler.ShutdownStatus`

### Constructor Summary

Constructors
`ResourceComputationScheduler()` Constructor, configured to wait for tasks completion for 5 seconds (will wait at most 10 seconds).
`ResourceComputationScheduler​(int shutdownWaitDuration, TimeUnit shutdownWaitUnit)` Constructor.
`ResourceComputationScheduler​(int shutdownWaitDuration, TimeUnit shutdownWaitUnit, com.google.common.eventbus.EventBus eventBus)` Constructor.

### Method Summary

All Methods Instance Methods Concrete Methods
`<U> U` `call​(Callable<U> callable, Runnable postTreatment)` Executes the given callable as soon as possible.
`void` `clearComputedElements()` Clears the set of computed keys.
`void` `computeAll​(Iterable<? extends IComputation<T>> computations)` Schedules all the given computations, which will only be run if no computation for the same key is in the `computedKeys` variable.
`void` `demandShutdown()` If `shutdown has not been requested before`, it submits a new task to `shut down` `computingPool` and `unloadingPool`.
`void` `dispose()` Disposes this scheduler, which shuts down its `terminator`.
`com.google.common.collect.ImmutableSet<T>` `getComputedElements()` Provides the set of keys of all the computations that have been run or are still running since its set of keys `computedKeys` was last set.
`void` `initialize()` Initializes this scheduler, which instantiates its `terminator`.
`boolean` `isInitialized()` Indicates whether this scheduler is initialized, i.e. it can run computations.
`boolean` `isScheduled​(T key)` Evaluates whether a computation with the given key has been run or is still running since its set of keys `computedKeys` was last set.
`void` `runAll​(Iterable<? extends Runnable> runnables)` Executes all the runnables in the given iterable, and returns when all computations possibly running or launched by the given runnables are finished.
`boolean` `scheduleComputation​(IComputation<T> computation)` Schedules a given computation to be performed as soon as possible, if its key is not present in the `computedKeys` or in the `currentlyComputing` keys, in which case the computation is ignored.
`void` `scheduleUnload​(Runnable runnable, com.google.common.util.concurrent.FutureCallback<Object> callback)` Schedule a job that is suppoed to unload resource(s) that are no longer needed.
`void` `setComputedElements​(Iterable<T> elements)` Sets the computed keys with all the values in the given iterable.

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

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

### Constructor Detail

#### ResourceComputationScheduler

    public ResourceComputationScheduler()

Constructor, configured to wait for tasks completion for 5 seconds (will wait at most 10 seconds).

#### ResourceComputationScheduler

    public ResourceComputationScheduler​(int shutdownWaitDuration,
                                        TimeUnit shutdownWaitUnit)

Constructor.

Parameters:
  `shutdownWaitDuration` - Time to wait for current tasks completion when shutting down the pools (will wait at most twice this amount of time).
  `shutdownWaitUnit` - Unit to use to interpret the other parameter.

#### ResourceComputationScheduler

    public ResourceComputationScheduler​(int shutdownWaitDuration,
                                        TimeUnit shutdownWaitUnit,
                                        com.google.common.eventbus.EventBus eventBus)

Constructor.

Parameters:
  `shutdownWaitDuration` - Time to wait for current tasks completion when shutting down the pools (will wait at most twice this amount of time).
  `shutdownWaitUnit` - Unit to use to interpret the other parameter.
  `eventBus` - The `EventBus` used to post events (shutdown events), can be `null`

### Method Detail

#### demandShutdown

    public void demandShutdown()

If `shutdown has not been requested before`, it submits a new task to `shut down` `computingPool` and `unloadingPool`. Do nothing if current thread already is interrupted. If a shutdown is actually started, events will be posted to the scheduler's eventBus if there is one. The events will be:

1. STARTED
2. SUCCESS if shutdown has succeeded or FAILURE if shutdown has failed

Note that these events will be sent in the calling Thread.

#### initialize

    public void initialize()

Initializes this scheduler, which instantiates its `terminator`. Can be called several times with no problem.

#### isInitialized

    public boolean isInitialized()

Indicates whether this scheduler is initialized, i.e. it can run computations.

Returns:
  `true` if and only if the scheduler is initialized.

#### dispose

    public void dispose()

Disposes this scheduler, which shuts down its `terminator`.

#### call

    public <U> U call​(Callable<U> callable,
                      Runnable postTreatment)

Executes the given callable as soon as possible. Whatever happens, the given callback is run before returning (in a "finally" clause) and then the "notComputing" condition will be signalled and the lock released.

Pre-conditions:

- `initialize()` has been called
- `dispose()` has not been called

If the scheduler has an eventBus, it will post the following events:

1. SETTING_UP
2. SCHEDULED as soon as the set-up is finished
3. FAILURE if and only if the given callable throws an exception
4. FINISHING as soon as the given callable has finished running (successfully or not)
5. FINISHED as soon as the tear-down is finished

Note that these events will be sent in the Thread that ran the computation, NOT in the calling Thread.

Type Parameters:
  `U` - the type of the return value.
Parameters:
  `callable` - will be executed as soon as this instance is no longer computing anything. Must not be `null`.
  `postTreatment` - will be called in a finally clause, whatever the outcome of the computation. Can be `null`.
Returns:
  The result returned by the given callable execution.

#### computeAll

    public void computeAll​(Iterable<? extends IComputation<T>> computations)

Schedules all the given computations, which will only be run if no computation for the same key is in the `computedKeys` variable. It is up to the caller to make sure that the semantics of computations previously run is the same as thos they are submitting, otherwise computations completely unrelated to what is being submitted may have marked a key as already computed. Returns after all the currently running plus submitted computations have finished.

Parameters:
  `computations` - An iterable over the computations to schedule. `null` entries are silently ignored.

#### scheduleComputation

    public boolean scheduleComputation​(IComputation<T> computation)

Schedules a given computation to be performed as soon as possible, if its key is not present in the `computedKeys` or in the `currentlyComputing` keys, in which case the computation is ignored. It is up to the caller to make sure that they submit homogeneous computations, in order for the filtering of computations by key to be meaningful.

WARNING! In a multi-threaded execution, this method may return before the computation is run. It is up to callers to make sure they only invoke that inside of a more general call to `call(Callable, Runnable)`, `computeAll(Iterable)`, or `runAll(Iterable)`

Parameters:
  `computation` - The computation to run. Cannot be `null`.
Returns:
  `true` if and only if the given key is not already among either the computed elements or the currently computing elements.

#### runAll

    public void runAll​(Iterable<? extends Runnable> runnables)

Executes all the runnables in the given iterable, and returns when all computations possibly running or launched by the given runnables are finished. This must be used when some treatment will possibly schedule computations but the exact list of computations cannot be computed beforehand.

Parameters:
  `runnables` - An iterable over the runnables to execute, must not be `null`. `null` entries are silently ignored.

#### scheduleUnload

    public void scheduleUnload​(Runnable runnable,
                               com.google.common.util.concurrent.FutureCallback<Object> callback)

Schedule a job that is suppoed to unload resource(s) that are no longer needed. This implementation uses a dedicated thread pool to perform these unloads.

Parameters:
  `runnable` - Runnable to run, must not be `null`
  `callback` - Callback to call upon completion, can be `null`

#### getComputedElements

    public com.google.common.collect.ImmutableSet<T> getComputedElements()

Provides the set of keys of all the computations that have been run or are still running since its set of keys `computedKeys` was last set.

Returns:
  The set of keys of all the computations that have been run or are still running since its set of keys `computedKeys` was last set.

#### isScheduled

    public boolean isScheduled​(T key)

Evaluates whether a computation with the given key has been run or is still running since its set of keys `computedKeys` was last set.

Parameters:
  `key` - The key of the computation to check.
Returns:
  true, if a computation of the given key has been run or is still running.

#### clearComputedElements

    public void clearComputedElements()

Clears the set of computed keys.

#### setComputedElements

    public void setComputedElements​(Iterable<T> elements)

Sets the computed keys with all the values in the given iterable.

Parameters:
  `elements` - An iterable over the elements to set as computed, must not be `null` but can be empty.