View Javadoc

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  /**
26   * A default implementation of {@link WriteFuture}.
27   *
28   * @author The Apache MINA Project (dev@mina.apache.org)
29   */
30  public class DefaultWriteFuture extends DefaultIoFuture implements WriteFuture {
31      /**
32       * Returns a new {@link DefaultWriteFuture} which is already marked as 'written'.
33       */
34      public static WriteFuture newWrittenFuture(IoSession session) {
35          DefaultWriteFuture unwrittenFuture = new DefaultWriteFuture(session);
36          unwrittenFuture.setWritten();
37          return unwrittenFuture;
38      }
39  
40      /**
41       * Returns a new {@link DefaultWriteFuture} which is already marked as 'not written'.
42       */
43      public static WriteFuture newNotWrittenFuture(IoSession session, Throwable cause) {
44          DefaultWriteFuture unwrittenFuture = new DefaultWriteFuture(session);
45          unwrittenFuture.setException(cause);
46          return unwrittenFuture;
47      }
48  
49      /**
50       * Creates a new instance.
51       */
52      public DefaultWriteFuture(IoSession session) {
53          super(session);
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      public boolean isWritten() {
60          if (isDone()) {
61              Object v = getValue();
62              if (v instanceof Boolean) {
63                  return ((Boolean) v).booleanValue();
64              }
65          }
66          return false;
67      }
68      
69      /**
70       * {@inheritDoc}
71       */
72      public Throwable getException() {
73          if (isDone()) {
74              Object v = getValue();
75              if (v instanceof Throwable) {
76                  return (Throwable) v;
77              }
78          }
79          return null;
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      public void setWritten() {
86          setValue(Boolean.TRUE);
87      }
88      
89      /**
90       * {@inheritDoc}
91       */
92      public void setException(Throwable exception) {
93          if (exception == null) {
94              throw new NullPointerException("exception");
95          }
96          
97          setValue(exception);
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public WriteFuture await() throws InterruptedException {
105         return (WriteFuture) super.await();
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     public WriteFuture awaitUninterruptibly() {
113         return (WriteFuture) super.awaitUninterruptibly();
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     public WriteFuture addListener(IoFutureListener<?> listener) {
121         return (WriteFuture) super.addListener(listener);
122     }
123 
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     public WriteFuture removeListener(IoFutureListener<?> listener) {
129         return (WriteFuture) super.removeListener(listener);
130     }
131 }