KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > util > concurrent > executor > SimpleResult


1 /*
2  * Copyright (c) 2003, Rafael Steil
3  * All rights reserved.
4
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * This file creation date: Jan 28, 2004
40  * net.jforum.util.concurrent.executor.SimpleResult.java
41  * The JForum Project
42  * http://www.jforum.net
43  *
44  * $Id: SimpleResult.java,v 1.4 2005/07/26 03:05:59 rafaelsteil Exp $
45  */

46 package net.jforum.util.concurrent.executor;
47
48 import net.jforum.util.concurrent.Result;
49 import net.jforum.util.concurrent.Task;
50
51 /**
52  * @author Rodrigo Kumpera
53  */

54 class SimpleResult implements Result
55 {
56     private final Task task;
57     private Exception JavaDoc exception;
58     private Object JavaDoc result;
59     private boolean ready = false;
60     private final Object JavaDoc lock = new Object JavaDoc();
61     
62     SimpleResult(Task task)
63     {
64         this.task = task;
65     }
66
67     public boolean hasThrown() throws IllegalStateException JavaDoc
68     {
69         synchronized(lock) {
70             if(!ready) {
71                 throw new IllegalStateException JavaDoc("task has not completed");
72             }
73             
74             return exception != null;
75         }
76     }
77
78     public Object JavaDoc getResult() throws IllegalStateException JavaDoc
79     {
80         synchronized(lock) {
81             if(!ready) {
82                 throw new IllegalStateException JavaDoc("task has not completed");
83             }
84                 
85             if(exception != null) {
86                 throw new IllegalStateException JavaDoc("task has thrown an exception");
87             }
88             
89             return result;
90         }
91     }
92
93     public Exception JavaDoc getException() throws IllegalStateException JavaDoc
94     {
95         synchronized(lock) {
96             if(!ready) {
97                 throw new IllegalStateException JavaDoc("task has not completed");
98             }
99             
100             if(exception == null) {
101                 throw new IllegalStateException JavaDoc("task has not thrown an exception");
102             }
103             
104             return exception;
105         }
106     }
107
108     public synchronized void waitResult() throws InterruptedException JavaDoc
109     {
110         if(Thread.interrupted()) {
111             throw new InterruptedException JavaDoc();
112         }
113         
114         synchronized(lock) {
115             if(ready) {
116                 return;
117             }
118             
119             while(!ready) {
120                 lock.wait();
121             }
122         }
123     }
124
125     public synchronized boolean poolResult(long timeout) throws InterruptedException JavaDoc
126     {
127         if(Thread.interrupted()) {
128             throw new InterruptedException JavaDoc();
129         }
130         
131         synchronized(lock) {
132             if(ready) {
133                 return true;
134             }
135             
136             if(timeout <= 0) {
137                 return false;
138             }
139
140             long remaining = timeout;
141             long start = System.currentTimeMillis();
142             
143             for(;;) {
144                 lock.wait(remaining);
145                 if(ready) {
146                     return true;
147                 }
148                 
149                 remaining = timeout - (System.currentTimeMillis() - start);
150                 if(remaining <= 0) {
151                     return false;
152                 }
153             }
154         }
155     }
156
157     void setException(Exception JavaDoc exception)
158     {
159         synchronized(lock) {
160             if(ready) {
161                 throw new IllegalStateException JavaDoc("task allready completed");
162             }
163             
164             this.exception = exception;
165             ready = true;
166             lock.notifyAll();
167         }
168     }
169
170     void setResult(Object JavaDoc result)
171     {
172         synchronized(lock) {
173             if(ready) {
174                 throw new IllegalStateException JavaDoc("task allready completed");
175             }
176             
177             this.ready = true;
178             this.result = result;
179             lock.notifyAll();
180         }
181     }
182
183     Task getTask()
184     {
185         return task;
186     }
187
188 }
Popular Tags