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 * 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 /**
56 * {@inheritDoc}
57 */
58 public boolean isWritten() {
59 if (isDone()) {
60 Object v = getValue();
61 if (v instanceof Boolean) {
62 return ((Boolean) v).booleanValue();
63 }
64 }
65 return false;
66 }
67
68 /**
69 * {@inheritDoc}
70 */
71 public Throwable getException() {
72 if (isDone()) {
73 Object v = getValue();
74 if (v instanceof Throwable) {
75 return (Throwable) v;
76 }
77 }
78 return null;
79 }
80
81 /**
82 * {@inheritDoc}
83 */
84 public void setWritten() {
85 setValue(Boolean.TRUE);
86 }
87
88 /**
89 * {@inheritDoc}
90 */
91 public void setException(Throwable exception) {
92 if (exception == null) {
93 throw new IllegalArgumentException("exception");
94 }
95
96 setValue(exception);
97 }
98
99 /**
100 * {@inheritDoc}
101 */
102 @Override
103 public WriteFuture await() throws InterruptedException {
104 return (WriteFuture) super.await();
105 }
106
107 /**
108 * {@inheritDoc}
109 */
110 @Override
111 public WriteFuture awaitUninterruptibly() {
112 return (WriteFuture) super.awaitUninterruptibly();
113 }
114
115 /**
116 * {@inheritDoc}
117 */
118 @Override
119 public WriteFuture addListener(IoFutureListener<?> listener) {
120 return (WriteFuture) super.addListener(listener);
121 }
122
123 /**
124 * {@inheritDoc}
125 */
126 @Override
127 public WriteFuture removeListener(IoFutureListener<?> listener) {
128 return (WriteFuture) super.removeListener(listener);
129 }
130 }