org.apache.mina.util

## Class AbstractIoFuture<V>

- java.lang.Object
- - org.apache.mina.util.AbstractIoFuture<V>

- All Implemented Interfaces:
    Future<V>, IoFuture<V>
  Direct Known Subclasses:
    ConnectFuture, DefaultWriteFuture

      public abstract class AbstractIoFuture<V>
      extends Object
      implements IoFuture<V>

  An abstract implementation of `IoFuture`. Owners of this future must implement `cancelOwner(boolean)` to receive notifications of when the future should be canceled.

  Concrete implementations of this abstract class should consider overriding the two methods `scheduleResult(org.apache.mina.api.IoFutureListener, Object)` and `scheduleException(org.apache.mina.api.IoFutureListener, Throwable)` so that listeners are called in a separate thread. The default implementations may end up calling the listener in the same thread that is registering the listener, before the registration has completed.

  Author:
    Apache MINA Project

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `AbstractIoFuture()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `cancel(boolean mayInterruptIfRunning)`
    `protected abstract boolean` | `cancelOwner(boolean mayInterruptIfRunning)` Notify the owner of this future that a client is attempting to cancel.
    `V` | `get()`
    `V` | `get(long timeout, TimeUnit unit)`
    `boolean` | `isCancelled()`
    `boolean` | `isDone()`
    `IoFuture<V>` | `register(IoFutureListener<V> listener)` Register a listener to asynchronously receive the results of the future computation.
    `protected void` | `scheduleException(IoFutureListener<V> listener, Throwable throwable)` Default implementation to call a listener's `IoFutureListener.exception(Throwable)` method.
    `protected void` | `scheduleResult(IoFutureListener<V> listener, V result)` Default implementation to call a listener's `IoFutureListener.completed(Object)` method.
    `protected void` | `setException(Throwable t)` Set the future result as a `Throwable`, indicating that a throwable was thrown while executing the task.
    `protected void` | `setResult(V value)` Set the future result of the executing task.

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

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

- - ### Constructor Detail

    - #### AbstractIoFuture

          public AbstractIoFuture()

  - ### Method Detail

    - #### register

          public IoFuture<V> register(IoFutureListener<V> listener)

      Register a listener to asynchronously receive the results of the future computation.
      Specified by:
        `register` in interface `IoFuture<V>`
      Parameters:
        `listener` - the listener to asynchronously receive the results of the future computation
      Returns:
        the instance of the future to allow "chaining" of registrations

    - #### cancel

          public boolean cancel(boolean mayInterruptIfRunning)

      Specified by:
        `cancel` in interface `Future<V>`

    - #### isCancelled

          public boolean isCancelled()

      Specified by:
        `isCancelled` in interface `Future<V>`

    - #### isDone

          public boolean isDone()

      Specified by:
        `isDone` in interface `Future<V>`

    - #### get

          public V get()
                throws InterruptedException,
                       ExecutionException

      Specified by:
        `get` in interface `Future<V>`
      Throws:
        `InterruptedException`
        `ExecutionException`

    - #### get

          public V get(long timeout,
              TimeUnit unit)
                throws InterruptedException,
                       ExecutionException,
                       TimeoutException

      Specified by:
        `get` in interface `Future<V>`
      Throws:
        `InterruptedException`
        `ExecutionException`
        `TimeoutException`

    - #### cancelOwner

          protected abstract boolean cancelOwner(boolean mayInterruptIfRunning)

      Notify the owner of this future that a client is attempting to cancel. This attempt will fail if the task has already completed, has already been cancelled, or could not be cancelled for some other reason. If successful, and this task has not started when `cancel` is called, this task should never run. If the task has already started, then the `mayInterruptIfRunning` parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.

      After this method returns, subsequent calls to `isDone()` will always return `true`. Subsequent calls to `isCancelled()` will always return `true` if this method returned `true`.

      Note: implementations must never throw an exception.
      Parameters:
        `mayInterruptIfRunning` - `true` if the owner executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete
      Returns:
        `false` if the task could not be cancelled, typically because it has already completed normally; `true` otherwise

    - #### scheduleResult

          protected void scheduleResult(IoFutureListener<V> listener,
                            V result)

      Default implementation to call a listener's `IoFutureListener.completed(Object)` method. Owners may override this method so that the listener is called from a thread pool.
      Parameters:
        `listener` - the listener to call
        `result` - the result to pass to the listener

    - #### scheduleException

          protected void scheduleException(IoFutureListener<V> listener,
                               Throwable throwable)

      Default implementation to call a listener's `IoFutureListener.exception(Throwable)` method. Owners may override this method so that the listener is called from a thread pool.
      Parameters:
        `listener` - the listener to call
        `throwable` - the exception to pass to the listener

    - #### setResult

          protected final void setResult(V value)

      Set the future result of the executing task. Any `IoFutureListener`s are notified of the
      Parameters:
        `value` - the value returned by the executing task.

    - #### setException

          protected final void setException(Throwable t)

      Set the future result as a `Throwable`, indicating that a throwable was thrown while executing the task. This value is usually set by the future result owner.

      Any `IoFutureListener`s are notified of the exception.
      Parameters:
        `t` - the throwable that was thrown while executing the task.

Copyright © 2009–2013 Apache MINA Project. All rights reserved.