Fix FindBugs violations and enable enforcement in utils
[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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.util.concurrent.CancellationException;
16 import java.util.concurrent.ExecutionException;
17
18 /**
19  * Utility exception mapper which translates an Exception to a specified type of
20  * Exception.
21  *
22  * <p>
23  * This mapper is intended to be used with
24  * {@link com.google.common.util.concurrent.Futures#makeChecked(
25  * com.google.common.util.concurrent.ListenableFuture, Function)}
26  * <ul>
27  * <li>if exception is the specified type or one of its subclasses, it returns
28  * original exception.
29  * <li>if exception is {@link ExecutionException} and the cause is of the
30  * specified type, it returns the cause
31  * <li>otherwise returns an instance of the specified exception type with
32  * original exception as the cause.
33  * </ul>
34  *
35  * @author Thomas Pantelis
36  *
37  * @param <X>
38  *            the exception type
39  */
40 public abstract class ExceptionMapper<X extends Exception> implements Function<Exception, X> {
41
42     private final Class<X> exceptionType;
43     private final String opName;
44
45     /**
46      * Constructor.
47      *
48      * @param opName the String prefix for exception messages.
49      * @param exceptionType the exception type to which to translate.
50      */
51     public ExceptionMapper(final String opName, final Class<X> exceptionType) {
52         this.exceptionType = requireNonNull(exceptionType);
53         this.opName = requireNonNull(opName);
54     }
55
56     /**
57      * Return the exception class produced by this instance.
58      *
59      * @return Exception class.
60      */
61     protected final Class<X> getExceptionType() {
62         return exceptionType;
63     }
64
65     /**
66      * Invoked to create a new exception instance of the specified type.
67      *
68      * @param message the message for the new exception.
69      * @param cause the cause for the new exception.
70      *
71      * @return an instance of the exception type.
72      */
73     protected abstract X newWithCause(String message, Throwable cause);
74
75     @Override
76     @SuppressWarnings("unchecked")
77     @SuppressFBWarnings({"BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", "NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE"})
78     public X apply(final Exception input) {
79
80         // If exception is of the specified type,return it.
81         if (exceptionType.isAssignableFrom(input.getClass())) {
82             return (X) input;
83         }
84
85         // If exception is ExecutionException whose cause is of the specified
86         // type, return the cause.
87         if (input instanceof ExecutionException && input.getCause() != null) {
88             if (exceptionType.isAssignableFrom(input.getCause().getClass())) {
89                 return (X) input.getCause();
90             }
91             return newWithCause(opName + " execution failed", input.getCause());
92         }
93
94         // Otherwise return an instance of the specified type with the original
95         // cause.
96
97         if (input instanceof InterruptedException) {
98             return newWithCause(opName + " was interupted.", input);
99         }
100
101         if (input instanceof CancellationException) {
102             return newWithCause(opName + " was cancelled.", input);
103         }
104
105         // We really shouldn't get here but need to cover it anyway for completeness.
106         return newWithCause(opName + " encountered an unexpected failure", input);
107     }
108 }