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 java.io.IOException;
23  
24  import org.apache.mina.core.RuntimeIoException;
25  import org.apache.mina.core.session.IoSession;
26  
27  /**
28   * A default implementation of {@link WriteFuture}.
29   *
30   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
31   */
32  public class DefaultReadFuture extends DefaultIoFuture implements ReadFuture {
33      /** A static object used when the session is closed */
34      private static final Object CLOSED = new Object();
35  
36      /**
37       * Creates a new instance.
38       * 
39       * @param session The associated session
40       */
41      public DefaultReadFuture(IoSession session) {
42          super(session);
43      }
44  
45      /**
46       * {@inheritDoc}
47       */
48      @Override
49      public Object getMessage() {
50          if (isDone()) {
51              Object v = getValue();
52              
53              if (v == CLOSED) {
54                  return null;
55              }
56  
57              if (v instanceof RuntimeException) {
58                  throw (RuntimeException) v;
59              }
60              
61              if (v instanceof Error) {
62                  throw (Error) v;
63              }
64              
65              if (v instanceof IOException || v instanceof Exception) {
66                  throw new RuntimeIoException((Exception) v);
67              }
68  
69              return v;
70          }
71  
72          return null;
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public boolean isRead() {
80          if (isDone()) {
81              Object v = getValue();
82              
83              return v != CLOSED && !(v instanceof Throwable);
84          }
85          
86          return false;
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      @Override
93      public boolean isClosed() {
94          if (isDone()) {
95              return getValue() == CLOSED;
96          }
97          
98          return false;
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public Throwable getException() {
106         if (isDone()) {
107             Object v = getValue();
108             
109             if (v instanceof Throwable) {
110                 return (Throwable)v;
111             }
112         }
113         
114         return null;
115     }
116 
117     /**
118      * {@inheritDoc}
119      */
120     @Override
121     public void setClosed() {
122         setValue(CLOSED);
123     }
124 
125     /**
126      * {@inheritDoc}
127      */
128     @Override
129     public void setRead(Object message) {
130         if (message == null) {
131             throw new IllegalArgumentException("message");
132         }
133         
134         setValue(message);
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     @Override
141     public void setException(Throwable exception) {
142         if (exception == null) {
143             throw new IllegalArgumentException("exception");
144         }
145 
146         setValue(exception);
147     }
148 
149     /**
150      * {@inheritDoc}
151      */
152     @Override
153     public ReadFuture await() throws InterruptedException {
154         return (ReadFuture) super.await();
155     }
156 
157     /**
158      * {@inheritDoc}
159      */
160     @Override
161     public ReadFuture awaitUninterruptibly() {
162         return (ReadFuture) super.awaitUninterruptibly();
163     }
164 
165     /**
166      * {@inheritDoc}
167      */
168     @Override
169     public ReadFuture addListener(IoFutureListener<?> listener) {
170         return (ReadFuture) super.addListener(listener);
171     }
172 
173     /**
174      * {@inheritDoc}
175      */
176     @Override
177     public ReadFuture removeListener(IoFutureListener<?> listener) {
178         return (ReadFuture) super.removeListener(listener);
179     }
180 }