17 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.utils.cc;
|
|
|
15 |
|
|
|
16 |
import org.openconcerto.utils.ExceptionUtils;
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* Transformer able to throw an exception.
|
|
|
20 |
*
|
|
|
21 |
* @author Sylvain
|
|
|
22 |
*
|
|
|
23 |
* @param <E> input type
|
|
|
24 |
* @param <T> return type
|
|
|
25 |
* @param <X> exception type
|
|
|
26 |
*/
|
180 |
ilm |
27 |
public abstract class ExnTransformer<E, T, X extends Exception> implements ITransformerExn<E, T, X> {
|
17 |
ilm |
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Execute this transformer, making sure that an exception of type <code>exnClass</code> is
|
|
|
31 |
* thrown.
|
|
|
32 |
*
|
|
|
33 |
* @param <Y> type of exception to throw.
|
|
|
34 |
* @param input the input.
|
|
|
35 |
* @param exnClass class exception to throw.
|
|
|
36 |
* @return the result of this transformer.
|
|
|
37 |
* @throws Y if {@link #transformChecked(Object)} throws an exception, it will be wrapped (if
|
|
|
38 |
* necessary) in an exception of class <code>exnClass</code>.
|
|
|
39 |
*/
|
|
|
40 |
public final <Y extends Exception> T transformCheckedWithExn(E input, Class<Y> exnClass) throws Y {
|
|
|
41 |
try {
|
|
|
42 |
return this.transformChecked(input);
|
|
|
43 |
} catch (Exception e) {
|
|
|
44 |
if (exnClass.isInstance(e))
|
|
|
45 |
throw exnClass.cast(e);
|
|
|
46 |
else
|
|
|
47 |
throw ExceptionUtils.createExn(exnClass, "executeChecked failed", e);
|
|
|
48 |
}
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Execute this transformer, wrapping exceptions thrown by {@link #transformChecked(Object)}
|
|
|
53 |
* into one of the passed exception classes.
|
|
|
54 |
*
|
|
|
55 |
* @param <Y> type of exception to throw.
|
|
|
56 |
* @param <Z> second type of exception to throw.
|
|
|
57 |
* @param <A> third type of exception to throw.
|
|
|
58 |
* @param input the input.
|
|
|
59 |
* @param wrapRT <code>true</code> so that even {@link RuntimeException} are wrapped into
|
|
|
60 |
* <code>Y</code>, <code>false</code> if this method should throw them as they are.
|
|
|
61 |
* @param exnClass class exception to throw.
|
|
|
62 |
* @param exnClass2 class exception to throw, can be <code>null</code>.
|
|
|
63 |
* @param exnClass3 class exception to throw, can be <code>null</code>.
|
|
|
64 |
* @return the result of this transformer.
|
|
|
65 |
* @throws Y if {@link #transformChecked(Object)} throws an exception, it will be wrapped (if
|
|
|
66 |
* necessary) in an exception of class <code>exnClass</code>.
|
|
|
67 |
* @throws Z if {@link #transformChecked(Object)} throws an exception of class Z.
|
|
|
68 |
* @throws A if {@link #transformChecked(Object)} throws an exception of class A.
|
|
|
69 |
*/
|
|
|
70 |
public final <Y extends Exception, Z extends Exception, A extends Exception> T transformCheckedWithExn(E input, final boolean wrapRT, Class<Y> exnClass, Class<Z> exnClass2, Class<A> exnClass3)
|
|
|
71 |
throws Y, Z, A {
|
|
|
72 |
try {
|
|
|
73 |
return this.transformChecked(input);
|
|
|
74 |
} catch (Exception e) {
|
|
|
75 |
if (!wrapRT && e instanceof RuntimeException)
|
|
|
76 |
throw (RuntimeException) e;
|
|
|
77 |
else if (exnClass.isInstance(e))
|
|
|
78 |
throw exnClass.cast(e);
|
|
|
79 |
else if (exnClass2 != null && exnClass2.isInstance(e))
|
|
|
80 |
throw exnClass2.cast(e);
|
|
|
81 |
else if (exnClass3 != null && exnClass3.isInstance(e))
|
|
|
82 |
throw exnClass3.cast(e);
|
|
|
83 |
else
|
|
|
84 |
throw ExceptionUtils.createExn(exnClass, "executeChecked failed", e);
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
|
63 |
ilm |
88 |
@Override
|
17 |
ilm |
89 |
public abstract T transformChecked(E input) throws X;
|
|
|
90 |
|
|
|
91 |
}
|