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 */ 020package org.apache.mina.core.future; 021 022import org.apache.mina.core.session.IoSession; 023 024/** 025 * A default implementation of {@link WriteFuture}. 026 * 027 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 028 */ 029public class DefaultWriteFuture extends DefaultIoFuture implements WriteFuture { 030 /** 031 * Returns a new {@link DefaultWriteFuture} which is already marked as 'written'. 032 * 033 * @param session The associated session 034 * @return A new future for a written message 035 */ 036 public static WriteFuture newWrittenFuture(IoSession session) { 037 DefaultWriteFuture writtenFuture = new DefaultWriteFuture(session); 038 writtenFuture.setWritten(); 039 040 return writtenFuture; 041 } 042 043 /** 044 * Returns a new {@link DefaultWriteFuture} which is already marked as 'not written'. 045 * 046 * @param session The associated session 047 * @param cause The reason why the message has not be written 048 * @return A new future for not written message 049 */ 050 public static WriteFuture newNotWrittenFuture(IoSession session, Throwable cause) { 051 DefaultWriteFuture unwrittenFuture = new DefaultWriteFuture(session); 052 unwrittenFuture.setException(cause); 053 054 return unwrittenFuture; 055 } 056 057 /** 058 * Creates a new instance. 059 * 060 * @param session The associated session 061 */ 062 public DefaultWriteFuture(IoSession session) { 063 super(session); 064 } 065 066 /** 067 * {@inheritDoc} 068 */ 069 public boolean isWritten() { 070 if (isDone()) { 071 Object v = getValue(); 072 073 if (v instanceof Boolean) { 074 return ((Boolean) v).booleanValue(); 075 } 076 } 077 078 return false; 079 } 080 081 /** 082 * {@inheritDoc} 083 */ 084 public Throwable getException() { 085 if (isDone()) { 086 Object v = getValue(); 087 088 if (v instanceof Throwable) { 089 return (Throwable) v; 090 } 091 } 092 093 return null; 094 } 095 096 /** 097 * {@inheritDoc} 098 */ 099 public void setWritten() { 100 setValue(Boolean.TRUE); 101 } 102 103 /** 104 * {@inheritDoc} 105 */ 106 public void setException(Throwable exception) { 107 if (exception == null) { 108 throw new IllegalArgumentException("exception"); 109 } 110 111 setValue(exception); 112 } 113 114 /** 115 * {@inheritDoc} 116 */ 117 @Override 118 public WriteFuture await() throws InterruptedException { 119 return (WriteFuture) super.await(); 120 } 121 122 /** 123 * {@inheritDoc} 124 */ 125 @Override 126 public WriteFuture awaitUninterruptibly() { 127 return (WriteFuture) super.awaitUninterruptibly(); 128 } 129 130 /** 131 * {@inheritDoc} 132 */ 133 @Override 134 public WriteFuture addListener(IoFutureListener<?> listener) { 135 return (WriteFuture) super.addListener(listener); 136 } 137 138 /** 139 * {@inheritDoc} 140 */ 141 @Override 142 public WriteFuture removeListener(IoFutureListener<?> listener) { 143 return (WriteFuture) super.removeListener(listener); 144 } 145}