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.common;
21  
22  
23  /**
24   * A default implementation of {@link WriteFuture}.
25   *
26   * @author The Apache MINA Project (dev@mina.apache.org)
27   * @version $Rev: 599745 $, $Date: 2007-11-30 02:04:47 -0700 (Fri, 30 Nov 2007) $
28   */
29  public class DefaultWriteFuture extends DefaultIoFuture implements WriteFuture {
30      /**
31       * Returns a new {@link DefaultWriteFuture} which is already marked as 'written'.
32       */
33      public static WriteFuture newWrittenFuture(IoSession session) {
34          DefaultWriteFuture unwrittenFuture = new DefaultWriteFuture(session);
35          unwrittenFuture.setWritten();
36          return unwrittenFuture;
37      }
38  
39      /**
40       * Returns a new {@link DefaultWriteFuture} which is already marked as 'not written'.
41       */
42      public static WriteFuture newNotWrittenFuture(IoSession session, Throwable cause) {
43          DefaultWriteFuture unwrittenFuture = new DefaultWriteFuture(session);
44          unwrittenFuture.setException(cause);
45          return unwrittenFuture;
46      }
47  
48      /**
49       * Creates a new instance.
50       */
51      public DefaultWriteFuture(IoSession session) {
52          super(session);
53      }
54  
55      public boolean isWritten() {
56          if (isReady()) {
57              Object v = getValue();
58              if (v instanceof Boolean) {
59                  return ((Boolean) v).booleanValue();
60              }
61          }
62          return false;
63      }
64      
65      public Throwable getException() {
66          if (isReady()) {
67              Object v = getValue();
68              if (v instanceof Throwable) {
69                  return (Throwable) v;
70              }
71          }
72          return null;
73      }
74  
75      public void setWritten() {
76          setValue(Boolean.TRUE);
77      }
78      
79      public void setException(Throwable exception) {
80          if (exception == null) {
81              throw new NullPointerException("exception");
82          }
83          
84          setValue(exception);
85      }
86  
87      @Override
88      public WriteFuture await() throws InterruptedException {
89          return (WriteFuture) super.await();
90      }
91  
92      @Override
93      public WriteFuture awaitUninterruptibly() {
94          return (WriteFuture) super.awaitUninterruptibly();
95      }
96  
97      @Override
98      public WriteFuture addListener(IoFutureListener<?> listener) {
99          return (WriteFuture) super.addListener(listener);
100     }
101 
102     @Override
103     public WriteFuture removeListener(IoFutureListener<?> listener) {
104         return (WriteFuture) super.removeListener(listener);
105     }
106 }