53e0936214b4f3598be185ea3787154b93eca3f2
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / concurrent / ExceptionMapper.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.yangtools.util.concurrent;
10
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.Function;
14 import java.util.concurrent.CancellationException;
15 import java.util.concurrent.ExecutionException;
16
17 /**
18  * Utility exception mapper which translates an Exception to a specified type of
19  * Exception.
20  *
21  * <p>
22  * This mapper is intended to be used with
23  * {@link com.google.common.util.concurrent.Futures#makeChecked(
24  * com.google.common.util.concurrent.ListenableFuture, Function)}
25  * <ul>
26  * <li>if exception is the specified type or one of its subclasses, it returns
27  * original exception.
28  * <li>if exception is {@link ExecutionException} and the cause is of the
29  * specified type, it returns the cause
30  * <li>otherwise returns an instance of the specified exception type with
31  * original exception as the cause.
32  * </ul>
33  *
34  * @author Thomas Pantelis
35  *
36  * @param <X>
37  *            the exception type
38  */
39 public abstract class ExceptionMapper<X extends Exception> implements Function<Exception, X> {
40
41     private final Class<X> exceptionType;
42     private final String opName;
43
44     /**
45      * Constructor.
46      *
47      * @param opName the String prefix for exception messages.
48      * @param exceptionType the exception type to which to translate.
49      */
50     public ExceptionMapper(final String opName, final Class<X> exceptionType) {
51         this.exceptionType = requireNonNull(exceptionType);
52         this.opName = requireNonNull(opName);
53     }
54
55     /**
56      * Return the exception class produced by this instance.
57      *
58      * @return Exception class.
59      */
60     protected final Class<X> getExceptionType() {
61         return exceptionType;
62     }
63
64     /**
65      * Invoked to create a new exception instance of the specified type.
66      *
67      * @param message the message for the new exception.
68      * @param cause the cause for the new exception.
69      *
70      * @return an instance of the exception type.
71      */
72     protected abstract X newWithCause(String message, Throwable cause);
73
74     @SuppressWarnings("unchecked")
75     @Override
76     public X apply(final Exception input) {
77
78         // If exception is of the specified type,return it.
79         if (exceptionType.isAssignableFrom(input.getClass())) {
80             return (X) input;
81         }
82
83         // If exception is ExecutionException whose cause is of the specified
84         // type, return the cause.
85         if (input instanceof ExecutionException && input.getCause() != null) {
86             if (exceptionType.isAssignableFrom(input.getCause().getClass())) {
87                 return (X) input.getCause();
88             }
89             return newWithCause(opName + " execution failed", input.getCause());
90         }
91
92         // Otherwise return an instance of the specified type with the original
93         // cause.
94
95         if (input instanceof InterruptedException) {
96             return newWithCause(opName + " was interupted.", input);
97         }
98
99         if (input instanceof CancellationException) {
100             return newWithCause(opName + " was cancelled.", input);
101         }
102
103         // We really shouldn't get here but need to cover it anyway for completeness.
104         return newWithCause(opName + " encountered an unexpected failure", input);
105     }
106 }