View Javadoc

1   /*
2    *   @(#) $Id: WriteFuture.java 355016 2005-12-08 07:00:30Z trustin $
3    *
4    *   Copyright 2004 The Apache Software Foundation
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   *
18   */
19  package org.apache.mina.common;
20  
21  /***
22   * An {@link IoFuture} for asynchronous write requests.
23   *
24   * <h3>Example</h3>
25   * <pre>
26   * IoSession session = ...;
27   * WriteFuture future = session.write(...);
28   * // Wait until the message is completely written out to the O/S buffer.
29   * future.join();
30   * if( future.isWritten() )
31   * {
32   *     // The message has been written successfully.
33   * }
34   * else
35   * {
36   *     // The messsage couldn't be written out completely for some reason.
37   *     // (e.g. Connection is closed)
38   * }
39   * </pre>
40   * 
41   * @author The Apache Directory Project
42   * @version $Rev: 355016 $, $Date: 2005-12-08 16:00:30 +0900 (Thu, 08 Dec 2005) $
43   */
44  public class WriteFuture extends IoFuture
45  {
46      /***
47       * Returns a new {@link WriteFuture} which is already marked as 'written'.
48       */
49      public static WriteFuture newWrittenFuture()
50      {
51          WriteFuture unwrittenFuture = new WriteFuture();
52          unwrittenFuture.setWritten( true );
53          return unwrittenFuture;
54      }
55  
56      /***
57       * Returns a new {@link WriteFuture} which is already marked as 'not written'.
58       */
59      public static WriteFuture newNotWrittenFuture()
60      {
61          WriteFuture unwrittenFuture = new WriteFuture();
62          unwrittenFuture.setWritten( false );
63          return unwrittenFuture;
64      }
65      
66      /***
67       * Creates a new instance.
68       */
69      public WriteFuture()
70      {
71      }
72      
73      /***
74       * Creates a new instance which uses the specified object as a lock.
75       */
76      public WriteFuture( Object lock )
77      {
78          super( lock );
79      }
80      
81      /***
82       * Returns <tt>true</tt> if the write operation is finished successfully.
83       */
84      public boolean isWritten()
85      {
86          if( isReady() )
87          {
88              return ( ( Boolean ) getValue() ).booleanValue();
89          }
90          else
91          {
92              return false;
93          }
94      }
95  
96      /***
97       * This method is invoked by MINA internally.  Please do not call this method
98       * directly.
99       */
100     public void setWritten( boolean written )
101     {
102         setValue( written? Boolean.TRUE : Boolean.FALSE );
103     }
104 }