001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.core.future;
021
022import java.util.concurrent.TimeUnit;
023
024import org.apache.mina.core.session.IoSession;
025
026/**
027 * Represents the completion of an asynchronous I/O operation on an 
028 * {@link IoSession}.
029 * Can be listened for completion using a {@link IoFutureListener}.
030 * 
031 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
032 */
033public interface IoFuture {
034    /**
035     * @return the {@link IoSession} which is associated with this future.
036     */
037    IoSession getSession();
038
039    /**
040     * Wait for the asynchronous operation to complete.
041     * The attached listeners will be notified when the operation is 
042     * completed.
043     * 
044     * @return The instance of IoFuture that we are waiting for
045     * @exception InterruptedException If the thread is interrupted while waiting
046     */
047    IoFuture await() throws InterruptedException;
048
049    /**
050     * Wait for the asynchronous operation to complete with the specified timeout.
051     *
052     * @param timeout The maximum delay to wait before getting out
053     * @param unit the type of unit for the delay (seconds, minutes...)
054     * @return <tt>true</tt> if the operation is completed. 
055     * @exception InterruptedException If the thread is interrupted while waiting
056     */
057    boolean await(long timeout, TimeUnit unit) throws InterruptedException;
058
059    /**
060     * Wait for the asynchronous operation to complete with the specified timeout.
061     *
062     * @param timeoutMillis The maximum milliseconds to wait before getting out
063     * @return <tt>true</tt> if the operation is completed.
064     * @exception InterruptedException If the thread is interrupted while waiting
065     */
066    boolean await(long timeoutMillis) throws InterruptedException;
067
068    /**
069     * Wait for the asynchronous operation to complete uninterruptibly.
070     * The attached listeners will be notified when the operation is 
071     * completed.
072     * 
073     * @return the current IoFuture
074     */
075    IoFuture awaitUninterruptibly();
076
077    /**
078     * Wait for the asynchronous operation to complete with the specified timeout
079     * uninterruptibly.
080     *
081     * @param timeout The maximum delay to wait before getting out
082     * @param unit the type of unit for the delay (seconds, minutes...)
083     * @return <tt>true</tt> if the operation is completed.
084     */
085    boolean awaitUninterruptibly(long timeout, TimeUnit unit);
086
087    /**
088     * Wait for the asynchronous operation to complete with the specified timeout
089     * uninterruptibly.
090     *
091     * @param timeoutMillis The maximum milliseconds to wait before getting out
092     * @return <tt>true</tt> if the operation is finished.
093     */
094    boolean awaitUninterruptibly(long timeoutMillis);
095
096    /**
097     * @deprecated Replaced with {@link #awaitUninterruptibly()}.
098     */
099    @Deprecated
100    void join();
101
102    /**
103     * @deprecated Replaced with {@link #awaitUninterruptibly(long)}.
104     * 
105     * @param timeoutMillis The time to wait for the join before bailing out
106     * @return <tt>true</tt> if the join was successful
107     */
108    @Deprecated
109    boolean join(long timeoutMillis);
110
111    /**
112     * @return <tt>true</tt> if the operation is completed.
113     */
114    boolean isDone();
115
116    /**
117     * Adds an event <tt>listener</tt> which is notified when
118     * this future is completed. If the listener is added
119     * after the completion, the listener is directly notified.
120     * 
121     * @param listener The listener to add
122     * @return the current IoFuture
123     */
124    IoFuture addListener(IoFutureListener<?> listener);
125
126    /**
127     * Removes an existing event <tt>listener</tt> so it won't be notified when
128     * the future is completed.
129     * 
130     * @param listener The listener to remove
131     * @return the current IoFuture
132     */
133    IoFuture removeListener(IoFutureListener<?> listener);
134}