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