001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.common;
021:
022: /**
023: * A default implementation of {@link WriteFuture}.
024: *
025: * @author The Apache MINA Project (dev@mina.apache.org)
026: * @version $Rev: 599745 $, $Date: 2007-11-30 02:04:47 -0700 (Fri, 30 Nov 2007) $
027: */
028: public class DefaultWriteFuture extends DefaultIoFuture implements
029: WriteFuture {
030: /**
031: * Returns a new {@link DefaultWriteFuture} which is already marked as 'written'.
032: */
033: public static WriteFuture newWrittenFuture(IoSession session) {
034: DefaultWriteFuture unwrittenFuture = new DefaultWriteFuture(
035: session);
036: unwrittenFuture.setWritten();
037: return unwrittenFuture;
038: }
039:
040: /**
041: * Returns a new {@link DefaultWriteFuture} which is already marked as 'not written'.
042: */
043: public static WriteFuture newNotWrittenFuture(IoSession session,
044: Throwable cause) {
045: DefaultWriteFuture unwrittenFuture = new DefaultWriteFuture(
046: session);
047: unwrittenFuture.setException(cause);
048: return unwrittenFuture;
049: }
050:
051: /**
052: * Creates a new instance.
053: */
054: public DefaultWriteFuture(IoSession session) {
055: super (session);
056: }
057:
058: public boolean isWritten() {
059: if (isReady()) {
060: Object v = getValue();
061: if (v instanceof Boolean) {
062: return ((Boolean) v).booleanValue();
063: }
064: }
065: return false;
066: }
067:
068: public Throwable getException() {
069: if (isReady()) {
070: Object v = getValue();
071: if (v instanceof Throwable) {
072: return (Throwable) v;
073: }
074: }
075: return null;
076: }
077:
078: public void setWritten() {
079: setValue(Boolean.TRUE);
080: }
081:
082: public void setException(Throwable exception) {
083: if (exception == null) {
084: throw new NullPointerException("exception");
085: }
086:
087: setValue(exception);
088: }
089:
090: @Override
091: public WriteFuture await() throws InterruptedException {
092: return (WriteFuture) super .await();
093: }
094:
095: @Override
096: public WriteFuture awaitUninterruptibly() {
097: return (WriteFuture) super .awaitUninterruptibly();
098: }
099:
100: @Override
101: public WriteFuture addListener(IoFutureListener<?> listener) {
102: return (WriteFuture) super .addListener(listener);
103: }
104:
105: @Override
106: public WriteFuture removeListener(IoFutureListener<?> listener) {
107: return (WriteFuture) super.removeListener(listener);
108: }
109: }
|