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