001    // $HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/framework/concurrent/ExecutionFinishedEvent.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    package org.deegree.framework.concurrent;
037    
038    import java.util.concurrent.Callable;
039    import java.util.concurrent.CancellationException;
040    
041    /**
042     * Event that is sent when asynchronous task finished.
043     * <p>
044     * This can mean:
045     * <ul>
046     * <li>it finished successfully</li>
047     * <li>it terminated abnormally (with an exception or error)</li>
048     * <li>a time out occurred during the performing of the task (or it's thread has been cancelled)</li>
049     * </ul>
050     * </p>
051     * <p>
052     * If the task did not finish successfully, the thrown exception / error is rethrown when
053     * {@link #getResult()} is called.
054     * </p>
055     *
056     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
057     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
058     * @author last edited by: $Author: mschneider $
059     *
060     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
061     * @param <T>
062     *            type of return value
063     */
064    public class ExecutionFinishedEvent<T> {
065    
066        private Callable<T> task;
067    
068        private T result;
069    
070        private Throwable t;
071    
072        /**
073         * Constructs an <code>ExecutionFinishedEvent</code> for a task that finished successfully.
074         *
075         * @param task
076         * @param result
077         */
078        ExecutionFinishedEvent( Callable<T> task, T result ) {
079            this.task = task;
080            this.result = result;
081        }
082    
083        /**
084         * Constructs an <code>ExecutionFinishedEvent</code> for a task that terminated abnormally.
085         *
086         * @param t
087         *            Throwable that the terminated task threw
088         * @param task
089         */
090        ExecutionFinishedEvent( Throwable t, Callable<T> task ) {
091            this.task = task;
092            this.t = t;
093        }
094    
095        /**
096         * Returns the corresponding task instance.
097         *
098         * @return the corresponding task instance
099         */
100        public Callable<T> getTask() {
101            return this.task;
102        }
103    
104        /**
105         * Returns the result value that the finished task returned.
106         * <p>
107         * If the task produced an exception or error, it is rethrown here. If the task has been
108         * cancelled (usually this means that the time out occurred), a {@link CancellationException} is
109         * thrown.
110         *
111         * @return the result value that the task returned
112         * @throws CancellationException
113         *             if task timed out / has been cancelled
114         * @throws Throwable
115         *             if task terminated with an exception or error
116         */
117        public T getResult()
118                                throws CancellationException, Throwable {
119            if ( this.t != null ) {
120                throw t;
121            }
122            return this.result;
123        }
124    }