KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > mina > common > support > DefaultIoFuture


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.common.support;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.mina.common.IoFuture;
26 import org.apache.mina.common.IoFutureListener;
27 import org.apache.mina.common.IoSession;
28
29 /**
30  * A default implementation of {@link IoFuture}.
31  *
32  * @author The Apache MINA Project (dev@mina.apache.org)
33  * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13 7월 2007) $
34  */

35 public class DefaultIoFuture implements IoFuture {
36     private final IoSession session;
37
38     private final Object JavaDoc lock;
39
40     private IoFutureListener firstListener;
41
42     private List JavaDoc<IoFutureListener> otherListeners;
43
44     private Object JavaDoc result;
45
46     private boolean ready;
47
48     /**
49      * Creates a new instance.
50      *
51      * @param session an {@link IoSession} which is associated with this future
52      */

53     public DefaultIoFuture(IoSession session) {
54         this.session = session;
55         this.lock = this;
56     }
57
58     public IoSession getSession() {
59         return session;
60     }
61
62     public void join() {
63         synchronized (lock) {
64             while (!ready) {
65                 try {
66                     lock.wait();
67                 } catch (InterruptedException JavaDoc e) {
68                 }
69             }
70         }
71     }
72
73     public boolean join(long timeoutMillis) {
74         long startTime = (timeoutMillis <= 0) ? 0 : System.currentTimeMillis();
75         long waitTime = timeoutMillis;
76
77         synchronized (lock) {
78             if (ready) {
79                 return ready;
80             } else if (waitTime <= 0) {
81                 return ready;
82             }
83
84             for (;;) {
85                 try {
86                     lock.wait(waitTime);
87                 } catch (InterruptedException JavaDoc e) {
88                 }
89
90                 if (ready) {
91                     return true;
92                 } else {
93                     waitTime = timeoutMillis
94                             - (System.currentTimeMillis() - startTime);
95                     if (waitTime <= 0) {
96                         return ready;
97                     }
98                 }
99             }
100         }
101     }
102
103     public boolean isReady() {
104         synchronized (lock) {
105             return ready;
106         }
107     }
108
109     /**
110      * Sets the result of the asynchronous operation, and mark it as finished.
111      */

112     protected void setValue(Object JavaDoc newValue) {
113         synchronized (lock) {
114             // Allow only once.
115
if (ready) {
116                 return;
117             }
118
119             result = newValue;
120             ready = true;
121             lock.notifyAll();
122
123             notifyListeners();
124         }
125     }
126
127     /**
128      * Returns the result of the asynchronous operation.
129      */

130     protected Object JavaDoc getValue() {
131         synchronized (lock) {
132             return result;
133         }
134     }
135
136     public void addListener(IoFutureListener listener) {
137         if (listener == null) {
138             throw new NullPointerException JavaDoc("listener");
139         }
140
141         synchronized (lock) {
142             if (firstListener == null) {
143                 firstListener = listener;
144             } else {
145                 if (otherListeners == null) {
146                     otherListeners = new ArrayList JavaDoc<IoFutureListener>(1);
147                 }
148                 otherListeners.add(listener);
149             }
150             if (ready) {
151                 listener.operationComplete(this);
152             }
153         }
154     }
155
156     public void removeListener(IoFutureListener listener) {
157         if (listener == null) {
158             throw new NullPointerException JavaDoc("listener");
159         }
160
161         synchronized (lock) {
162             if (listener == firstListener) {
163                 if (otherListeners != null && !otherListeners.isEmpty()) {
164                     firstListener = otherListeners.remove(0);
165                 } else {
166                     firstListener = null;
167                 }
168             } else if (otherListeners != null) {
169                 otherListeners.remove(listener);
170             }
171         }
172     }
173
174     private void notifyListeners() {
175         synchronized (lock) {
176             if (firstListener != null) {
177                 firstListener.operationComplete(this);
178                 if (otherListeners != null) {
179                     for (IoFutureListener l : otherListeners) {
180                         l.operationComplete(this);
181                     }
182                 }
183             }
184         }
185     }
186 }
187
Popular Tags