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 * Creates a new instance.
32 *
33 * @param session The associated session
34 */
35 public DefaultWriteFuture(IoSession session) {
36 super(session);
37 }
38
39 /**
40 * Returns a new {@link DefaultWriteFuture} which is already marked as 'written'.
41 *
42 * @param session The associated session
43 * @return A new future for a written message
44 */
45 public static WriteFuture newWrittenFuture(IoSession session) {
46 DefaultWriteFuturere.html#DefaultWriteFuture">DefaultWriteFuture writtenFuture = new DefaultWriteFuture(session);
47 writtenFuture.setWritten();
48
49 return writtenFuture;
50 }
51
52 /**
53 * Returns a new {@link DefaultWriteFuture} which is already marked as 'not written'.
54 *
55 * @param session The associated session
56 * @param cause The reason why the message has not be written
57 * @return A new future for not written message
58 */
59 public static WriteFuture newNotWrittenFuture(IoSession session, Throwable cause) {
60 DefaultWriteFuture.html#DefaultWriteFuture">DefaultWriteFuture unwrittenFuture = new DefaultWriteFuture(session);
61 unwrittenFuture.setException(cause);
62
63 return unwrittenFuture;
64 }
65
66 /**
67 * {@inheritDoc}
68 */
69 @Override
70 public boolean isWritten() {
71 if (isDone()) {
72 Object v = getValue();
73
74 if (v instanceof Boolean) {
75 return ((Boolean) v).booleanValue();
76 }
77 }
78
79 return false;
80 }
81
82 /**
83 * {@inheritDoc}
84 */
85 @Override
86 public Throwable getException() {
87 if (isDone()) {
88 Object v = getValue();
89
90 if (v instanceof Throwable) {
91 return (Throwable) v;
92 }
93 }
94
95 return null;
96 }
97
98 /**
99 * {@inheritDoc}
100 */
101 @Override
102 public void setWritten() {
103 setValue(Boolean.TRUE);
104 }
105
106 /**
107 * {@inheritDoc}
108 */
109 @Override
110 public void setException(Throwable exception) {
111 if (exception == null) {
112 throw new IllegalArgumentException("exception");
113 }
114
115 setValue(exception);
116 }
117
118 /**
119 * {@inheritDoc}
120 */
121 @Override
122 public WriteFuture await() throws InterruptedException {
123 return (WriteFuture) super.await();
124 }
125
126 /**
127 * {@inheritDoc}
128 */
129 @Override
130 public WriteFuture awaitUninterruptibly() {
131 return (WriteFuture) super.awaitUninterruptibly();
132 }
133
134 /**
135 * {@inheritDoc}
136 */
137 @Override
138 public WriteFuture addListener(IoFutureListener<?> listener) {
139 return (WriteFuture) super.addListener(listener);
140 }
141
142 /**
143 * {@inheritDoc}
144 */
145 @Override
146 public WriteFuture removeListener(IoFutureListener<?> listener) {
147 return (WriteFuture) super.removeListener(listener);
148 }
149 }