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