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