Dépôt officiel du code source de l'ERP OpenConcerto
Blame | Last modification | View Log | RSS feed
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU General Public License Version 3
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*/
package org.openconcerto.utils.cc;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class ExceptionFuture<V, X extends Exception, X2 extends Exception> extends FutureTask<V> {
static public class OneExceptionFuture<V, X extends Exception> extends ExceptionFuture<V, X, RuntimeException> {
public <X2 extends RuntimeException> OneExceptionFuture(final I2ExnFactory<? extends V, ? extends X, ? extends RuntimeException> cl) {
super(cl, RuntimeException.class);
}
}
private final Class<X2> exnClass;
public ExceptionFuture(final I2ExnFactory<? extends V, ? extends X, ? extends X2> cl, final Class<X2> exnClass) {
super(cl::createChecked);
this.exnClass = exnClass;
}
public final V getWithOriginalException() throws InterruptedException, X, X2 {
try {
return this.get();
} catch (ExecutionException e) {
final Throwable cause = e.getCause();
if (cause instanceof Error) {
throw (Error) cause;
} else if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
// Not strictly necessary but it feels more robust than to cast "cause" to X even
// when it's really X2
} else if (this.exnClass.isInstance(cause)) {
throw this.exnClass.cast(cause);
} else {
// our Callable throws X or X2 but the latter is already handled above.
@SuppressWarnings("unchecked")
final X casted = (X) cause;
throw casted;
}
}
}
}