查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  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,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.core.future;
21  
22  import org.apache.mina.core.session.IoSession;
23  
24  /**
25   * A default implementation of {@link WriteFuture}.
26   *
27   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
28   */
29  public class DefaultWriteFuture extends DefaultIoFuture implements WriteFuture {
30      /**
31       * Returns a new {@link DefaultWriteFuture} which is already marked as 'written'.
32       * 
33       * @param session The associated session
34       * @return A new future for a written message
35       */
36      public static WriteFuture newWrittenFuture(IoSession session) {
37          DefaultWriteFuture writtenFuture = new DefaultWriteFuture(session);
38          writtenFuture.setWritten();
39          
40          return writtenFuture;
41      }
42  
43      /**
44       * Returns a new {@link DefaultWriteFuture} which is already marked as 'not written'.
45       * 
46       * @param session The associated session
47       * @param cause The reason why the message has not be written
48       * @return A new future for not written message
49       */
50      public static WriteFuture newNotWrittenFuture(IoSession session, Throwable cause) {
51          DefaultWriteFuture unwrittenFuture = new DefaultWriteFuture(session);
52          unwrittenFuture.setException(cause);
53          
54          return unwrittenFuture;
55      }
56  
57      /**
58       * Creates a new instance.
59       * 
60       * @param session The associated session
61       */
62      public DefaultWriteFuture(IoSession session) {
63          super(session);
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      public boolean isWritten() {
70          if (isDone()) {
71              Object v = getValue();
72              
73              if (v instanceof Boolean) {
74                  return ((Boolean) v).booleanValue();
75              }
76          }
77          
78          return false;
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      public Throwable getException() {
85          if (isDone()) {
86              Object v = getValue();
87              
88              if (v instanceof Throwable) {
89                  return (Throwable) v;
90              }
91          }
92          
93          return null;
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      public void setWritten() {
100         setValue(Boolean.TRUE);
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     public void setException(Throwable exception) {
107         if (exception == null) {
108             throw new IllegalArgumentException("exception");
109         }
110 
111         setValue(exception);
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public WriteFuture await() throws InterruptedException {
119         return (WriteFuture) super.await();
120     }
121 
122     /**
123      * {@inheritDoc}
124      */
125     @Override
126     public WriteFuture awaitUninterruptibly() {
127         return (WriteFuture) super.awaitUninterruptibly();
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public WriteFuture addListener(IoFutureListener<?> listener) {
135         return (WriteFuture) super.addListener(listener);
136     }
137 
138     /**
139      * {@inheritDoc}
140      */
141     @Override
142     public WriteFuture removeListener(IoFutureListener<?> listener) {
143         return (WriteFuture) super.removeListener(listener);
144     }
145 }